summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2012-12-15 19:11:54 +0100
committerAntoine Pitrou <solipsis@pitrou.net>2012-12-15 19:11:54 +0100
commit4113d2bd368480bc3f2cae36e5562cb601305737 (patch)
treeb1e35ce7f52475fb2deac6cd3de2440cc06c41da
parenteea058b0143e98ef902a3931895aa99252059ef0 (diff)
downloadcpython-git-4113d2bd368480bc3f2cae36e5562cb601305737.tar.gz
Issue #16298: In HTTPResponse.read(), close the socket when there is no Content-Length and the incoming stream is finished.
Patch by Eran Rundstein.
-rw-r--r--Lib/httplib.py4
-rw-r--r--Lib/test/test_httplib.py15
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS4
4 files changed, 23 insertions, 1 deletions
diff --git a/Lib/httplib.py b/Lib/httplib.py
index 4c8b0fe209..39a388e805 100644
--- a/Lib/httplib.py
+++ b/Lib/httplib.py
@@ -565,6 +565,10 @@ class HTTPResponse:
self.length -= len(s)
if not self.length:
self.close()
+ else:
+ if not s:
+ self.close()
+
return s
def _read_chunked(self, amt):
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
index 5b5ae2ef49..bd48fea241 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -166,7 +166,7 @@ class BasicTest(TestCase):
self.assertEqual(repr(exc), '''BadStatusLine("\'\'",)''')
def test_partial_reads(self):
- # if we have a lenght, the system knows when to close itself
+ # if we have a length, the system knows when to close itself
# same behaviour than when we read the whole thing with read()
body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText"
sock = FakeSocket(body)
@@ -177,6 +177,19 @@ class BasicTest(TestCase):
self.assertEqual(resp.read(2), 'xt')
self.assertTrue(resp.isclosed())
+ def test_partial_reads_no_content_length(self):
+ # when no length is present, the socket should be gracefully closed when
+ # all data was read
+ body = "HTTP/1.1 200 Ok\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):
# Check invalid host_port
diff --git a/Misc/ACKS b/Misc/ACKS
index d9ea742d7d..8e6fa2a076 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -849,6 +849,7 @@ Clinton Roy
Paul Rubin
Sam Ruby
Audun S. Runde
+Eran Rundstein
Rauli Ruohonen
Jeff Rush
Sam Rushing
diff --git a/Misc/NEWS b/Misc/NEWS
index 0db76a962a..de2f3ba510 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -160,6 +160,10 @@ Core and Builtins
Library
-------
+- Issue #16298: In HTTPResponse.read(), close the socket when there is no
+ Content-Length and the incoming stream is finished. Patch by Eran
+ Rundstein.
+
- Issue #16248: Disable code execution from the user's home directory by
tkinter when the -E flag is passed to Python. Patch by Zachary Ware.