response.next()
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 2b134a6 · Browse files · 5 files·+53 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+53 added−17 removed
- tests:+23 added−1 removed
- other:+30 added−16 removed
Provenance
- Commit
d652479- Tree
5422ec6- Author
Tom Christie <tom@tomchristie.com>- Committer
Tom Christie <tom@tomchristie.com>- Parent
2b134a6
5 files changed+53 added−17 removed
modified:httpcore/adapters/redirects.py+26 added-12 removed
1-import functools1 import typing2 from urllib.parse import urljoin, urlparse3 4 from ..config import DEFAULT_MAX_REDIRECTS12 unmodified lines17 def prepare_request(self, request: Request) -> None:18 self.dispatch.prepare_request(request)19 20 async def send(self, request: Request, **options: typing.Any) -> Response:22- allow_redirects = options.pop("allow_redirects", True)21+ allow_redirects = options.pop("allow_redirects", True) # type: bool22+23+ # The following will not typically be specified by the end-user developer,24+ # but are included in `response.next()` calls.25 history = options.pop("history", []) # type: typing.List[Response]26 seen_urls = options.pop("seen_urls", set()) # type: typing.Set[URL]25- seen_urls.add(request.url)27 28 while True:29+ # We perform these checks here, so that calls to `response.next()`30+ # will raise redirect errors if appropriate.31+ if len(history) > self.max_redirects:32+ raise TooManyRedirects()33+ if request.url in seen_urls:34+ raise RedirectLoop()35+36 response = await self.dispatch.send(request, **options)37 response.history = list(history)38 if not response.is_redirect:39 break32- history.append(response)33- request = self.build_redirect_request(request, response)34- if not allow_redirects:40+41+ history.insert(0, response)42+ seen_urls.add(request.url)43+44+ if allow_redirects:45+ request = self.build_redirect_request(request, response)46+ else:47 next_options = dict(options)48 next_options["seen_urls"] = seen_urls49 next_options["history"] = history38- response.next = functools.partial(self.send, request=request, **next_options)50+51+ async def send_next() -> Response:52+ nonlocal request, response, next_options53+ request = self.build_redirect_request(request, response)54+ response = await self.send(request, **next_options)55+ return response56+57+ response.next = send_next # type: ignore58 break40- if len(history) > self.max_redirects:41- raise TooManyRedirects()42- if request.url in seen_urls:43- raise RedirectLoop()44- seen_urls.add(request.url)59 60 return response61 62 async def close(self) -> None:77 unmodified linesmodified:httpcore/decoders.py+2 added-3 removed
4 unmodified lines5 """6 import typing7 import zlib8 9+import httpcore.exceptions10+11 try:12 import brotli13 except ImportError: # pragma: nocover14 brotli = None15 16 15-import httpcore.exceptions16-17-17 class Decoder:18 def decode(self, data: bytes) -> bytes:19 raise NotImplementedError() # pragma: nocover20 126 unmodified linesmodified:httpcore/exceptions.py+1 added-1 removed
76 unmodified lines77 """78 Decoding of the response failed.79 """80 81- 81+82 class InvalidURL(Exception):83 """84 URL was missing a hostname, or was not one of HTTP/HTTPS.85 """modified:httpcore/models.py+1 added
340 unmodified lines341 self.body_aiter = body342 343 self.request = request344 self.history = [] if history is None else list(history)345+ self.next = None # typing.Optional[typing.Callable]346 347 @property348 def url(self) -> typing.Optional[URL]:349 return None if self.request is None else self.request.url52 unmodified linesmodified:tests/adapters/test_redirects.py+23 added-1 removed
109 unmodified lines110 111 @pytest.mark.asyncio112 async def test_disallow_redirects():113 client = RedirectAdapter(MockDispatch())114- response = await client.request("POST", "https://example.org/redirect_303", allow_redirects=False)114+ response = await client.request(115+ "POST", "https://example.org/redirect_303", allow_redirects=False116+ )117 assert response.status_code == codes.see_other118 assert response.url == URL("https://example.org/redirect_303")119 assert len(response.history) == 0120 48 unmodified lines169 await client.request("GET", "https://example.org/multiple_redirects?count=21")170 171 172 @pytest.mark.asyncio173+async def test_too_many_redirects_calling_next():174+ client = RedirectAdapter(MockDispatch())175+ url = "https://example.org/multiple_redirects?count=21"176+ response = await client.request("GET", url, allow_redirects=False)177+ with pytest.raises(TooManyRedirects):178+ while response.is_redirect:179+ response = await response.next()180+181+182+@pytest.mark.asyncio183 async def test_redirect_loop():184 client = RedirectAdapter(MockDispatch())185 with pytest.raises(RedirectLoop):186 await client.request("GET", "https://example.org/redirect_loop")187 188 189 @pytest.mark.asyncio190+async def test_redirect_loop_calling_next():191+ client = RedirectAdapter(MockDispatch())192+ url = "https://example.org/redirect_loop"193+ response = await client.request("GET", url, allow_redirects=False)194+ with pytest.raises(RedirectLoop):195+ while response.is_redirect:196+ response = await response.next()197+198+199+@pytest.mark.asyncio200 async def test_cross_domain_redirect():201 client = RedirectAdapter(MockDispatch())202 url = "https://example.com/cross_domain"203 headers = {"Authorization": "abc"}38 unmodified lines