From: Tom Christie Date: Thu, 13 Jun 2019 13:38:56 +0000 (+0100) Subject: Close redirect responses (#88) X-Git-Tag: 0.5.0~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3ca9a748134f3bdb980d444209d17d7d2a246113;p=thirdparty%2Fhttpx.git Close redirect responses (#88) --- diff --git a/http3/client.py b/http3/client.py index b07d55d4..fba9fc79 100644 --- a/http3/client.py +++ b/http3/client.py @@ -144,32 +144,37 @@ class BaseClient: response = await self.dispatch.send( request, verify=verify, cert=cert, timeout=timeout ) - assert isinstance(response, AsyncResponse) - response.history = list(history) - self.cookies.extract_cookies(response) - history = [response] + history - if not response.is_redirect: - break - - if allow_redirects: - request = self.build_redirect_request(request, response) - else: + should_close_response = True + try: + assert isinstance(response, AsyncResponse) + response.history = list(history) + self.cookies.extract_cookies(response) + history = [response] + history - async def send_next() -> AsyncResponse: - nonlocal request, response, verify, cert, allow_redirects, timeout, history + if allow_redirects and response.is_redirect: request = self.build_redirect_request(request, response) - response = await self.send_handling_redirects( - request, - allow_redirects=allow_redirects, - verify=verify, - cert=cert, - timeout=timeout, - history=history, - ) - return response - - response.next = send_next # type: ignore - break + else: + should_close_response = False + break + finally: + if should_close_response: + await response.close() + + if response.is_redirect: + async def send_next() -> AsyncResponse: + nonlocal request, response, verify, cert, allow_redirects, timeout, history + request = self.build_redirect_request(request, response) + response = await self.send_handling_redirects( + request, + allow_redirects=allow_redirects, + verify=verify, + cert=cert, + timeout=timeout, + history=history, + ) + return response + + response.next = send_next # type: ignore return response