@@ -102,11 +102,12 @@ class Response:
102102 It is still necessary to ``close`` the response object for correct management of
103103 sockets, including doing so implicitly via ``with requests.get(...) as response``."""
104104
105- def __init__ (self , sock : SocketType , session : "Session" ) -> None :
105+ def __init__ (self , sock : SocketType , session : "Session" , method : str ) -> None :
106106 self .socket = sock
107107 self .encoding = "utf-8"
108108 self ._cached = None
109109 self ._headers = {}
110+ self ._method = method
110111
111112 # _start_index and _receive_buffer are used when parsing headers.
112113 # _receive_buffer will grow by 32 bytes everytime it is too small.
@@ -276,6 +277,15 @@ def _parse_headers(self) -> None:
276277 else :
277278 self ._headers [title ] = content
278279
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+
279289 def _validate_not_gzip (self ) -> None :
280290 """gzip encoding is not supported. Raise an exception if found."""
281291 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
670680 if not socket :
671681 raise OutOfRetries ("Repeated socket failures" ) from last_exc
672682
673- resp = Response (socket , self ) # our response
683+ resp = Response (socket , self , method ) # our response
674684 if allow_redirects :
675685 if "location" in resp .headers and 300 <= resp .status_code <= 399 :
676686 # a naive handler for redirects
0 commit comments