[Python-checkins] cpython (merge 3.3 -> default): Issue #16298: In HTTPResponse.read(), close the socket when there is no

antoine.pitrou python-checkins at python.org
Sat Dec 15 19:28:28 CET 2012


http://hg.python.org/cpython/rev/5d6c2c7bc5d4
changeset:   80861:5d6c2c7bc5d4
parent:      80856:16b1fde2275c
parent:      80860:59358f991c00
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sat Dec 15 19:23:34 2012 +0100
summary:
  Issue #16298: In HTTPResponse.read(), close the socket when there is no Content-Length and the incoming stream is finished.
Patch by Eran Rundstein.

files:
  Lib/http/client.py       |   3 ++
  Lib/test/test_httplib.py |  36 ++++++++++++++++++++++++++-
  Misc/ACKS                |   1 +
  Misc/NEWS                |   4 +++
  4 files changed, 42 insertions(+), 2 deletions(-)


diff --git a/Lib/http/client.py b/Lib/http/client.py
--- a/Lib/http/client.py
+++ b/Lib/http/client.py
@@ -536,6 +536,9 @@
             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
@@ -175,7 +175,7 @@
         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)
@@ -187,7 +187,7 @@
         self.assertTrue(resp.isclosed())
 
     def test_partial_readintos(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)
@@ -203,6 +203,38 @@
         self.assertEqual(bytes(b), b'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 = 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_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 = 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
 
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1022,6 +1022,7 @@
 Sam Ruby
 Demur Rumed
 Audun S. Runde
+Eran Rundstein
 Rauli Ruohonen
 Jeff Rush
 Sam Rushing
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -167,6 +167,10 @@
 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.
+
 - Add abc.ABC class to use inheritance rather than a direct invocation of
   ABCMeta metaclass. Patch by Bruno Dupuis.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list