gh-112064: Fix incorrect handling of negative read sizes in HTTPResponse.read() (#128270)

The parameter `amt` of `HTTPResponse.read()`, which could be a negative integer,
has not been handled before and led to waiting for the connection to close
for `keep-alive connections`. Now, this has been fixed, and passing negative values
to `HTTPResponse().read()` works the same as passing `None` value.
This commit is contained in:
Yury Manushkin 2025-01-28 12:37:32 +01:00 committed by GitHub
parent 8e57877e3f
commit 4d0d24f6e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 1 deletions

View file

@ -472,7 +472,7 @@ class HTTPResponse(io.BufferedIOBase):
if self.chunked:
return self._read_chunked(amt)
if amt is not None:
if amt is not None and amt >= 0:
if self.length is not None and amt > self.length:
# clip the read to the "end of response"
amt = self.length
@ -590,6 +590,8 @@ class HTTPResponse(io.BufferedIOBase):
def _read_chunked(self, amt=None):
assert self.chunked != _UNKNOWN
if amt is not None and amt < 0:
amt = None
value = []
try:
while (chunk_left := self._get_chunk_left()) is not None: