mirror of
https://github.com/python/cpython.git
synced 2025-09-25 09:50:37 +00:00
Issue #15633: httplib.HTTPResponse is now mark closed when the server sends less than the advertised Content-Length.
This commit is contained in:
parent
1efd9824d8
commit
d66c0ee76e
3 changed files with 27 additions and 7 deletions
|
@ -547,7 +547,11 @@ class HTTPResponse:
|
||||||
if self.length is None:
|
if self.length is None:
|
||||||
s = self.fp.read()
|
s = self.fp.read()
|
||||||
else:
|
else:
|
||||||
s = self._safe_read(self.length)
|
try:
|
||||||
|
s = self._safe_read(self.length)
|
||||||
|
except IncompleteRead:
|
||||||
|
self.close()
|
||||||
|
raise
|
||||||
self.length = 0
|
self.length = 0
|
||||||
self.close() # we read everything
|
self.close() # we read everything
|
||||||
return s
|
return s
|
||||||
|
@ -561,13 +565,14 @@ class HTTPResponse:
|
||||||
# connection, and the user is reading more bytes than will be provided
|
# connection, and the user is reading more bytes than will be provided
|
||||||
# (for example, reading in 1k chunks)
|
# (for example, reading in 1k chunks)
|
||||||
s = self.fp.read(amt)
|
s = self.fp.read(amt)
|
||||||
|
if not s:
|
||||||
|
# Ideally, we would raise IncompleteRead if the content-length
|
||||||
|
# wasn't satisfied, but it might break compatibility.
|
||||||
|
self.close()
|
||||||
if self.length is not None:
|
if self.length is not None:
|
||||||
self.length -= len(s)
|
self.length -= len(s)
|
||||||
if not self.length:
|
if not self.length:
|
||||||
self.close()
|
self.close()
|
||||||
else:
|
|
||||||
if not s:
|
|
||||||
self.close()
|
|
||||||
|
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
|
@ -190,6 +190,19 @@ class BasicTest(TestCase):
|
||||||
self.assertEqual(resp.read(1), '')
|
self.assertEqual(resp.read(1), '')
|
||||||
self.assertTrue(resp.isclosed())
|
self.assertTrue(resp.isclosed())
|
||||||
|
|
||||||
|
def test_partial_reads_incomplete_body(self):
|
||||||
|
# if the server shuts down the connection before the whole
|
||||||
|
# content-length is delivered, the socket is gracefully closed
|
||||||
|
body = "HTTP/1.1 200 Ok\r\nContent-Length: 10\r\n\r\nText"
|
||||||
|
sock = FakeSocket(body)
|
||||||
|
resp = httplib.HTTPResponse(sock)
|
||||||
|
resp.begin()
|
||||||
|
self.assertEqual(resp.read(2), 'Te')
|
||||||
|
self.assertFalse(resp.isclosed())
|
||||||
|
self.assertEqual(resp.read(2), 'xt')
|
||||||
|
self.assertEqual(resp.read(1), '')
|
||||||
|
self.assertTrue(resp.isclosed())
|
||||||
|
|
||||||
def test_host_port(self):
|
def test_host_port(self):
|
||||||
# Check invalid host_port
|
# Check invalid host_port
|
||||||
|
|
||||||
|
@ -320,7 +333,7 @@ class BasicTest(TestCase):
|
||||||
resp = httplib.HTTPResponse(sock, method="GET")
|
resp = httplib.HTTPResponse(sock, method="GET")
|
||||||
resp.begin()
|
resp.begin()
|
||||||
self.assertEqual(resp.read(), 'Hello\r\n')
|
self.assertEqual(resp.read(), 'Hello\r\n')
|
||||||
resp.close()
|
self.assertTrue(resp.isclosed())
|
||||||
|
|
||||||
def test_incomplete_read(self):
|
def test_incomplete_read(self):
|
||||||
sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello\r\n')
|
sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello\r\n')
|
||||||
|
@ -334,10 +347,9 @@ class BasicTest(TestCase):
|
||||||
"IncompleteRead(7 bytes read, 3 more expected)")
|
"IncompleteRead(7 bytes read, 3 more expected)")
|
||||||
self.assertEqual(str(i),
|
self.assertEqual(str(i),
|
||||||
"IncompleteRead(7 bytes read, 3 more expected)")
|
"IncompleteRead(7 bytes read, 3 more expected)")
|
||||||
|
self.assertTrue(resp.isclosed())
|
||||||
else:
|
else:
|
||||||
self.fail('IncompleteRead expected')
|
self.fail('IncompleteRead expected')
|
||||||
finally:
|
|
||||||
resp.close()
|
|
||||||
|
|
||||||
def test_epipe(self):
|
def test_epipe(self):
|
||||||
sock = EPipeSocket(
|
sock = EPipeSocket(
|
||||||
|
|
|
@ -199,6 +199,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #15633: httplib.HTTPResponse is now mark closed when the server
|
||||||
|
sends less than the advertised Content-Length.
|
||||||
|
|
||||||
- Issue #15881: Fixed atexit hook in multiprocessing.
|
- Issue #15881: Fixed atexit hook in multiprocessing.
|
||||||
|
|
||||||
- Issue #14340: Upgrade the embedded expat library to version 2.1.0.
|
- Issue #14340: Upgrade the embedded expat library to version 2.1.0.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue