@@ -102,11 +102,12 @@ class Response:
102
102
It is still necessary to ``close`` the response object for correct management of
103
103
sockets, including doing so implicitly via ``with requests.get(...) as response``."""
104
104
105
- def __init__ (self , sock : SocketType , session : "Session" ) -> None :
105
+ def __init__ (self , sock : SocketType , session : "Session" , method : str ) -> None :
106
106
self .socket = sock
107
107
self .encoding = "utf-8"
108
108
self ._cached = None
109
109
self ._headers = {}
110
+ self ._method = method
110
111
111
112
# _start_index and _receive_buffer are used when parsing headers.
112
113
# _receive_buffer will grow by 32 bytes everytime it is too small.
@@ -276,6 +277,15 @@ def _parse_headers(self) -> None:
276
277
else :
277
278
self ._headers [title ] = content
278
279
280
+ # does the body have a fixed length? (of zero)
281
+ if (
282
+ self .status_code == 204
283
+ or self .status_code == 304
284
+ or 100 <= self .status_code < 200 # 1xx codes
285
+ or self ._method == "HEAD"
286
+ ):
287
+ self ._remaining = 0
288
+
279
289
def _validate_not_gzip (self ) -> None :
280
290
"""gzip encoding is not supported. Raise an exception if found."""
281
291
if "content-encoding" in self .headers and self .headers ["content-encoding" ] == "gzip" :
@@ -670,7 +680,7 @@ def request( # noqa: PLR0912,PLR0913,PLR0915 Too many branches,Too many argumen
670
680
if not socket :
671
681
raise OutOfRetries ("Repeated socket failures" ) from last_exc
672
682
673
- resp = Response (socket , self ) # our response
683
+ resp = Response (socket , self , method ) # our response
674
684
if allow_redirects :
675
685
if "location" in resp .headers and 300 <= resp .status_code <= 399 :
676
686
# a naive handler for redirects
0 commit comments