]> granicus.if.org Git - python/commitdiff
Issue #16298: In HTTPResponse.read(), close the socket when there is no Content-Lengt...
authorAntoine Pitrou <solipsis@pitrou.net>
Sat, 15 Dec 2012 18:11:54 +0000 (19:11 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Sat, 15 Dec 2012 18:11:54 +0000 (19:11 +0100)
Patch by Eran Rundstein.

Lib/httplib.py
Lib/test/test_httplib.py
Misc/ACKS
Misc/NEWS

index 4c8b0fe2091a88f49457c8878c7c279a4b4974c7..39a388e8051bb65c6ad45aee7f9a52b1eed4953b 100644 (file)
@@ -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):
index 5b5ae2ef49af1977d5389001f2a31b5e75e3ed61..bd48fea24123fa390aa50272adf8baaa707a859b 100644 (file)
@@ -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
 
index d9ea742d7da5b6af64c92b03deb39532fe734920..8e6fa2a076d4db1954a74d0cc9b8c35bdb11d2e0 100644 (file)
--- 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
index 0db76a962ab7bed8262daad1e3809430c893f3c9..de2f3ba51041a140531fa9854f048d5336bce7f1 100644 (file)
--- 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.