@@ -86,11 +86,14 @@ class Response:
86
86
87
87
encoding = None
88
88
89
- def __init__ (self , sock : SocketType , session : "Session" ) -> None :
89
+ def __init__ (
90
+ self , sock : SocketType , session : "Session" , fast_close : bool = False
91
+ ) -> None :
90
92
self .socket = sock
91
93
self .encoding = "utf-8"
92
94
self ._cached = None
93
95
self ._headers = {}
96
+ self ._fast_close = fast_close
94
97
95
98
# _start_index and _receive_buffer are used when parsing headers.
96
99
# _receive_buffer will grow by 32 bytes everytime it is too small.
@@ -231,17 +234,18 @@ def close(self) -> None:
231
234
if not self .socket :
232
235
return
233
236
# 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 :
235
238
if self ._remaining and self ._remaining > 0 :
236
239
self ._throw_away (self ._remaining )
237
240
elif self ._chunked :
238
241
while True :
239
242
chunk_header = bytes (self ._readto (b"\r \n " )).split (b";" , 1 )[0 ]
243
+ if not chunk_header :
244
+ break
240
245
chunk_size = int (bytes (chunk_header ), 16 )
241
246
if chunk_size == 0 :
242
247
break
243
248
self ._throw_away (chunk_size + 2 )
244
- self ._parse_headers ()
245
249
if self ._session :
246
250
# pylint: disable=protected-access
247
251
self ._session ._connection_manager .free_socket (self .socket )
@@ -361,11 +365,13 @@ def __init__(
361
365
socket_pool : SocketpoolModuleType ,
362
366
ssl_context : Optional [SSLContextType ] = None ,
363
367
session_id : Optional [str ] = None ,
368
+ fast_close : Optional [bool ] = False ,
364
369
) -> None :
365
370
self ._connection_manager = get_connection_manager (socket_pool )
366
371
self ._ssl_context = ssl_context
367
372
self ._session_id = session_id
368
373
self ._last_response = None
374
+ self ._fast_close = fast_close
369
375
370
376
@staticmethod
371
377
def _check_headers (headers : Dict [str , str ]):
@@ -560,7 +566,7 @@ def request(
560
566
if not socket :
561
567
raise OutOfRetries ("Repeated socket failures" ) from last_exc
562
568
563
- resp = Response (socket , self ) # our response
569
+ resp = Response (socket , self , fast_close = self . _fast_close ) # our response
564
570
if allow_redirects :
565
571
if "location" in resp .headers and 300 <= resp .status_code <= 399 :
566
572
# a naive handler for redirects
0 commit comments