Home / Function/ handle_401() — requests Function Reference

handle_401() — requests Function Reference

Architecture documentation for the handle_401() function in auth.py from the requests codebase.

Entity Profile

Dependency Diagram

graph TD
  351a1e00_cb4e_af8e_1ed9_e33664efe05e["handle_401()"]
  aa32083c_418b_f1ff_9e66_cf1a12e8a8ee["HTTPDigestAuth"]
  351a1e00_cb4e_af8e_1ed9_e33664efe05e -->|defined in| aa32083c_418b_f1ff_9e66_cf1a12e8a8ee
  7a5e3fcd_3d53_bb5a_33e1_2ec03991bb2c["build_digest_header()"]
  351a1e00_cb4e_af8e_1ed9_e33664efe05e -->|calls| 7a5e3fcd_3d53_bb5a_33e1_2ec03991bb2c
  b59a42f3_d82f_81a3_c550_b1cea1222004["get()"]
  351a1e00_cb4e_af8e_1ed9_e33664efe05e -->|calls| b59a42f3_d82f_81a3_c550_b1cea1222004
  85f754cb_6190_2351_34cb_47f7d725e66b["parse_dict_header()"]
  351a1e00_cb4e_af8e_1ed9_e33664efe05e -->|calls| 85f754cb_6190_2351_34cb_47f7d725e66b
  9e1359c1_1379_2078_a638_b914c03e4f63["copy()"]
  351a1e00_cb4e_af8e_1ed9_e33664efe05e -->|calls| 9e1359c1_1379_2078_a638_b914c03e4f63
  55052edd_2a43_f298_b8d3_9fb1f2641e84["extract_cookies_to_jar()"]
  351a1e00_cb4e_af8e_1ed9_e33664efe05e -->|calls| 55052edd_2a43_f298_b8d3_9fb1f2641e84
  style 351a1e00_cb4e_af8e_1ed9_e33664efe05e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/requests/auth.py lines 241–283

    def handle_401(self, r, **kwargs):
        """
        Takes the given response and tries digest-auth, if needed.

        :rtype: requests.Response
        """

        # If response is not 4xx, do not auth
        # See https://github.com/psf/requests/issues/3772
        if not 400 <= r.status_code < 500:
            self._thread_local.num_401_calls = 1
            return r

        if self._thread_local.pos is not None:
            # Rewind the file position indicator of the body to where
            # it was to resend the request.
            r.request.body.seek(self._thread_local.pos)
        s_auth = r.headers.get("www-authenticate", "")

        if "digest" in s_auth.lower() and self._thread_local.num_401_calls < 2:
            self._thread_local.num_401_calls += 1
            pat = re.compile(r"digest ", flags=re.IGNORECASE)
            self._thread_local.chal = parse_dict_header(pat.sub("", s_auth, count=1))

            # Consume content and release the original connection
            # to allow our new request to reuse the same one.
            r.content
            r.close()
            prep = r.request.copy()
            extract_cookies_to_jar(prep._cookies, r.request, r.raw)
            prep.prepare_cookies(prep._cookies)

            prep.headers["Authorization"] = self.build_digest_header(
                prep.method, prep.url
            )
            _r = r.connection.send(prep, **kwargs)
            _r.history.append(r)
            _r.request = prep

            return _r

        self._thread_local.num_401_calls = 1
        return r

Domain

Subdomains

Frequently Asked Questions

What does handle_401() do?
handle_401() is a function in the requests codebase, defined in src/requests/auth.py.
Where is handle_401() defined?
handle_401() is defined in src/requests/auth.py at line 241.
What does handle_401() call?
handle_401() calls 5 function(s): build_digest_header, copy, extract_cookies_to_jar, get, parse_dict_header.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free