From f9b1eaf5f07796dc6eadf35350f8ccd3a249618d Mon Sep 17 00:00:00 2001 From: Joren Vrancken Date: Sat, 9 Sep 2023 15:09:35 +0200 Subject: [PATCH 1/2] Pass arguments from instance dict to urlopen in HTTPAdapter.send --- src/requests/adapters.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/requests/adapters.py b/src/requests/adapters.py index eb240fa954..a264464524 100644 --- a/src/requests/adapters.py +++ b/src/requests/adapters.py @@ -130,6 +130,7 @@ class HTTPAdapter(BaseAdapter): "_pool_connections", "_pool_maxsize", "_pool_block", + "urllib3_response_options", ] def __init__( @@ -154,6 +155,8 @@ def __init__( self.init_poolmanager(pool_connections, pool_maxsize, block=pool_block) + self.urllib3_response_options = {} + def __getstate__(self): return {attr: getattr(self, attr, None) for attr in self.__attrs__} @@ -481,20 +484,25 @@ def send( else: timeout = TimeoutSauce(connect=timeout, read=timeout) + urlopen_kwargs = self.urllib3_response_options.copy() + urlopen_kwargs.update( + { + "method": request.method, + "url": url, + "body": request.body, + "headers": request.headers, + "redirect": False, + "assert_same_host": False, + "preload_content": False, + "decode_content": False, + "retries": self.max_retries, + "timeout": timeout, + "chunked": chunked, + } + ) + try: - resp = conn.urlopen( - method=request.method, - url=url, - body=request.body, - headers=request.headers, - redirect=False, - assert_same_host=False, - preload_content=False, - decode_content=False, - retries=self.max_retries, - timeout=timeout, - chunked=chunked, - ) + resp = conn.urlopen(**urlopen_kwargs) except (ProtocolError, OSError) as err: raise ConnectionError(err, request=request) From fa37b4c6e84f0838231de318d0f9a91d48ac70ef Mon Sep 17 00:00:00 2001 From: Joren Vrancken Date: Tue, 12 Sep 2023 22:39:42 +0200 Subject: [PATCH 2/2] Add line of documentation about urllib3_response_options --- src/requests/adapters.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/requests/adapters.py b/src/requests/adapters.py index a264464524..56a247477b 100644 --- a/src/requests/adapters.py +++ b/src/requests/adapters.py @@ -438,6 +438,9 @@ def send( ): """Sends PreparedRequest object. Returns Response object. + It is possible to pass additional arguments to :meth:`urllib3.poolmanager.PoolManager.urlopen` + (e.g. `enforce_content_length`) by populating :attr:`HTTPAdapter.urllib3_response_options`. + :param request: The :class:`PreparedRequest ` being sent. :param stream: (optional) Whether to stream the request content. :param timeout: (optional) How long to wait for the server to send