[Python-checkins] cpython (merge 3.3 -> default): Issue #15633: httplib.HTTPResponse is now mark closed when the server sends
antoine.pitrou
python-checkins at python.org
Sat Feb 2 23:10:35 CET 2013
http://hg.python.org/cpython/rev/9f9287357af9
changeset: 81952:9f9287357af9
parent: 81948:12ef5a4bba63
parent: 81951:7d504068bc58
user: Antoine Pitrou <solipsis at pitrou.net>
date: Sat Feb 02 23:06:22 2013 +0100
summary:
Issue #15633: httplib.HTTPResponse is now mark closed when the server sends less than the advertised Content-Length.
files:
Lib/http/client.py | 15 +++++++---
Lib/test/test_httplib.py | 37 +++++++++++++++++++++++++--
Misc/NEWS | 3 ++
3 files changed, 47 insertions(+), 8 deletions(-)
diff --git a/Lib/http/client.py b/Lib/http/client.py
--- a/Lib/http/client.py
+++ b/Lib/http/client.py
@@ -507,7 +507,11 @@
if self.length is None:
s = self.fp.read()
else:
- s = self._safe_read(self.length)
+ try:
+ s = self._safe_read(self.length)
+ except IncompleteRead:
+ self.close()
+ raise
self.length = 0
self.close() # we read everything
return s
@@ -532,13 +536,14 @@
# connection, and the user is reading more bytes than will be provided
# (for example, reading in 1k chunks)
n = self.fp.readinto(b)
- if self.length is not None:
+ if not n:
+ # Ideally, we would raise IncompleteRead if the content-length
+ # wasn't satisfied, but it might break compatibility.
+ self.close()
+ elif self.length is not None:
self.length -= n
if not self.length:
self.close()
- else:
- if not n:
- self.close()
return n
def _read_next_chunk_size(self):
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -237,6 +237,38 @@
self.assertEqual(n, 0)
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 = client.HTTPResponse(sock)
+ resp.begin()
+ self.assertEqual(resp.read(2), b'Te')
+ self.assertFalse(resp.isclosed())
+ self.assertEqual(resp.read(2), b'xt')
+ self.assertEqual(resp.read(1), b'')
+ self.assertTrue(resp.isclosed())
+
+ def test_partial_readintos_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 = client.HTTPResponse(sock)
+ resp.begin()
+ b = bytearray(2)
+ n = resp.readinto(b)
+ self.assertEqual(n, 2)
+ self.assertEqual(bytes(b), b'Te')
+ self.assertFalse(resp.isclosed())
+ n = resp.readinto(b)
+ self.assertEqual(n, 2)
+ self.assertEqual(bytes(b), b'xt')
+ n = resp.readinto(b)
+ self.assertEqual(n, 0)
+ self.assertTrue(resp.isclosed())
+
def test_host_port(self):
# Check invalid host_port
@@ -490,7 +522,7 @@
resp = client.HTTPResponse(sock, method="GET")
resp.begin()
self.assertEqual(resp.read(), b'Hello\r\n')
- resp.close()
+ self.assertTrue(resp.isclosed())
def test_incomplete_read(self):
sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello\r\n')
@@ -504,10 +536,9 @@
"IncompleteRead(7 bytes read, 3 more expected)")
self.assertEqual(str(i),
"IncompleteRead(7 bytes read, 3 more expected)")
+ self.assertTrue(resp.isclosed())
else:
self.fail('IncompleteRead expected')
- finally:
- resp.close()
def test_epipe(self):
sock = EPipeSocket(
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -235,6 +235,9 @@
Library
-------
+- Issue #15633: httplib.HTTPResponse is now mark closed when the server
+ sends less than the advertised Content-Length.
+
- Issue #6972: The zipfile module no longer overwrites files outside of
its destination path when extracting malicious zip files.
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list