@@ -86,11 +86,14 @@ class Response:
8686
8787 encoding = None
8888
89- def __init__ (self , sock : SocketType , session : "Session" ) -> None :
89+ def __init__ (
90+ self , sock : SocketType , session : "Session" , fast_close : bool = False
91+ ) -> None :
9092 self .socket = sock
9193 self .encoding = "utf-8"
9294 self ._cached = None
9395 self ._headers = {}
96+ self ._fast_close = fast_close
9497
9598 # _start_index and _receive_buffer are used when parsing headers.
9699 # _receive_buffer will grow by 32 bytes everytime it is too small.
@@ -231,17 +234,18 @@ def close(self) -> None:
231234 if not self .socket :
232235 return
233236 # Make sure we've read all of our response.
234- if self ._cached is None :
237+ if self ._cached is None and not self . _fast_close :
235238 if self ._remaining and self ._remaining > 0 :
236239 self ._throw_away (self ._remaining )
237240 elif self ._chunked :
238241 while True :
239242 chunk_header = bytes (self ._readto (b"\r \n " )).split (b";" , 1 )[0 ]
243+ if not chunk_header :
244+ break
240245 chunk_size = int (bytes (chunk_header ), 16 )
241246 if chunk_size == 0 :
242247 break
243248 self ._throw_away (chunk_size + 2 )
244- self ._parse_headers ()
245249 if self ._session :
246250 # pylint: disable=protected-access
247251 self ._session ._connection_manager .free_socket (self .socket )
@@ -361,11 +365,13 @@ def __init__(
361365 socket_pool : SocketpoolModuleType ,
362366 ssl_context : Optional [SSLContextType ] = None ,
363367 session_id : Optional [str ] = None ,
368+ fast_close : Optional [bool ] = False ,
364369 ) -> None :
365370 self ._connection_manager = get_connection_manager (socket_pool )
366371 self ._ssl_context = ssl_context
367372 self ._session_id = session_id
368373 self ._last_response = None
374+ self ._fast_close = fast_close
369375
370376 @staticmethod
371377 def _check_headers (headers : Dict [str , str ]):
@@ -560,7 +566,7 @@ def request(
560566 if not socket :
561567 raise OutOfRetries ("Repeated socket failures" ) from last_exc
562568
563- resp = Response (socket , self ) # our response
569+ resp = Response (socket , self , fast_close = self . _fast_close ) # our response
564570 if allow_redirects :
565571 if "location" in resp .headers and 300 <= resp .status_code <= 399 :
566572 # a naive handler for redirects
0 commit comments