Skip to content
Myr

Add await response.next() interface

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by Tom Christie · parent b392b7e · Browse files · 2 files+27 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+27 added−4 removed

  • tests:+5 added−0 removed
  • other:+22 added−4 removed

2 files+27 added−4 removed

modified:httpcore/adapters/redirects.py+22 added-4 removed
1import functools2import typing3from urllib.parse import urljoin, urlparse45from ..config import DEFAULT_MAX_REDIRECTS
13 unmodified lines
19 self.dispatch.prepare_request(request)2021 async def send(self, request: Request, **options: typing.Any) -> Response:22 allow_redirects = options.pop("allow_redirects", True)22 history = [] # type: typing.List[Response]23 seen_urls = set((request.url,))23 history = options.pop("history", []) # type: typing.List[Response]24 seen_urls = options.pop("seen_urls", set()) # type: typing.Set[URL]25 seen_urls.add(request.url)2627 while True:28 response = await self.dispatch.send(request, **options)29 response.history = list(history)28 if not allow_redirects or not response.is_redirect:30 if not response.is_redirect:31 break32 history.append(response)33 request = self.build_redirect_request(request, response)34 if not allow_redirects:35 next_options = dict(options)36 next_options["seen_urls"] = seen_urls37 next_options["history"] = history38 response.next = functools.partial(self.send, request=request, **next_options)39 break40 if len(history) > self.max_redirects:41 raise TooManyRedirects()33 request = self.build_redirect_request(request, response)42 if request.url in seen_urls:43 raise RedirectLoop()44 seen_urls.add(request.url)45
32 unmodified lines
7879 return method8081 def redirect_url(self, request: Request, response: Response) -> URL:82 """83 Return the URL for the redirect to follow.84 """85 location = response.headers["Location"]8687 # Handle redirection without scheme (see: RFC 1808 Section 4)88 if location.startswith("//"):
15 unmodified lines
104105 return URL(url)106107 def redirect_headers(self, request: Request, url: URL) -> Headers:108 """109 Strip Authorization headers when responses are redirected away from110 the origin.111 """112 headers = Headers(request.headers)113 if url.origin != request.url.origin:114 del headers["Authorization"]115 return headers116117 def redirect_body(self, request: Request, method: str) -> bytes:118 """119 Return the body that should be used for the redirect request.120 """121 if method != request.method and method == "GET":122 return b""123 if request.is_streaming:124 raise RedirectBodyUnavailable()
1 unmodified line
modified:tests/adapters/test_redirects.py+5 added
114 unmodified lines
115 assert response.status_code == codes.see_other116 assert response.url == URL("https://example.org/redirect_303")117 assert len(response.history) == 0118119 response = await response.next()120 assert response.status_code == codes.ok121 assert response.url == URL("https://example.org/")122 assert len(response.history) == 1123124125@pytest.mark.asyncio126async def test_relative_redirect():127 client = RedirectAdapter(MockDispatch())128 response = await client.request("GET", "https://example.org/relative_redirect")
91 unmodified lines