Takes the given response and tries digest-auth, if needed. :rtype: requests.Response
(self, r: Response, **kwargs: Any)
| 271 | self._thread_local.num_401_calls = 1 |
| 272 | |
| 273 | def handle_401(self, r: Response, **kwargs: Any) -> Response: |
| 274 | """ |
| 275 | Takes the given response and tries digest-auth, if needed. |
| 276 | |
| 277 | :rtype: requests.Response |
| 278 | """ |
| 279 | |
| 280 | # If response is not 4xx, do not auth |
| 281 | # See https://github.com/psf/requests/issues/3772 |
| 282 | if not 400 <= r.status_code < 500: |
| 283 | self._thread_local.num_401_calls = 1 |
| 284 | return r |
| 285 | |
| 286 | if self._thread_local.pos is not None: |
| 287 | # Rewind the file position indicator of the body to where |
| 288 | # it was to resend the request. |
| 289 | if (seek := getattr(r.request.body, "seek", None)) is not None: |
| 290 | seek(self._thread_local.pos) |
| 291 | s_auth = r.headers.get("www-authenticate", "") |
| 292 | |
| 293 | if "digest" in s_auth.lower() and self._thread_local.num_401_calls < 2: |
| 294 | self._thread_local.num_401_calls += 1 |
| 295 | pat = re.compile(r"digest ", flags=re.IGNORECASE) |
| 296 | self._thread_local.chal = parse_dict_header(pat.sub("", s_auth, count=1)) |
| 297 | |
| 298 | # Consume content and release the original connection |
| 299 | # to allow our new request to reuse the same one. |
| 300 | r.content |
| 301 | r.close() |
| 302 | prep = r.request.copy() |
| 303 | cookie_jar = cast("CookieJar", prep._cookies) # type: ignore[reportPrivateUsage] |
| 304 | extract_cookies_to_jar(cookie_jar, r.request, r.raw) |
| 305 | prep.prepare_cookies(cookie_jar) |
| 306 | |
| 307 | _digest_auth = self.build_digest_header( |
| 308 | cast(str, prep.method), cast(str, prep.url) |
| 309 | ) |
| 310 | if _digest_auth: |
| 311 | prep.headers["Authorization"] = _digest_auth |
| 312 | _r = r.connection.send(prep, **kwargs) |
| 313 | _r.history.append(r) |
| 314 | _r.request = prep |
| 315 | |
| 316 | return _r |
| 317 | |
| 318 | self._thread_local.num_401_calls = 1 |
| 319 | return r |
| 320 | |
| 321 | def __call__(self, r: PreparedRequest) -> PreparedRequest: |
| 322 | # Initialize per-thread state, if needed |
nothing calls this directly
no test coverage detected