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
Provenance
- Commit
450ea25- Tree
e4d8220- Author
Tom Christie <tom@tomchristie.com>- Committer
Tom Christie <tom@tomchristie.com>- Parent
b392b7e
2 files changed+27 added−4 removed
modified:httpcore/adapters/redirects.py+22 added-4 removed
1+import functools2 import typing3 from urllib.parse import urljoin, urlparse4 5 from ..config import DEFAULT_MAX_REDIRECTS13 unmodified lines19 self.dispatch.prepare_request(request)20 21 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)26 27 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 lines78 79 return method80 81 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"]86 87 # Handle redirection without scheme (see: RFC 1808 Section 4)88 if location.startswith("//"):15 unmodified lines104 105 return URL(url)106 107 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 headers116 117 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 linemodified:tests/adapters/test_redirects.py+5 added
114 unmodified lines115 assert response.status_code == codes.see_other116 assert response.url == URL("https://example.org/redirect_303")117 assert len(response.history) == 0118 119+ response = await response.next()120+ assert response.status_code == codes.ok121+ assert response.url == URL("https://example.org/")122+ assert len(response.history) == 1123 124+125 @pytest.mark.asyncio126 async def test_relative_redirect():127 client = RedirectAdapter(MockDispatch())128 response = await client.request("GET", "https://example.org/relative_redirect")91 unmodified lines