]> git.ipfire.org Git - thirdparty/Python/cpython.git/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/http/client.py
Lib/test/test_httplib.py
Misc/ACKS
Misc/NEWS

index 97a71559dcd427d4e5a62bf16df78044a9ba7ec1..4d93b93ff012d460b9ebab87ad13d17e23dbb1fe 100644 (file)
@@ -511,6 +511,10 @@ class HTTPResponse(io.RawIOBase):
             self.length -= len(s)
             if not self.length:
                 self.close()
+        else:
+            if not s:
+                self.close()
+
         return s
 
     def _read_chunked(self, amt):
index 24c72ff37c9e7fd01617a7ff252bc7b1ccb2a7c1..b0777d42867c4f88dd644f17f61c43f0a5dc9150 100644 (file)
@@ -175,7 +175,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)
@@ -186,6 +186,19 @@ class BasicTest(TestCase):
         self.assertEqual(resp.read(2), 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_host_port(self):
         # Check invalid host_port
 
index 4fc365900819e5d2802eb37d273a1c9528e89034..424e72a7dc5119bd82ec99e63a04fb0b8cfc8842 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -917,6 +917,7 @@ Paul Rubin
 Sam Ruby
 Demur Rumed
 Audun S. Runde
+Eran Rundstein
 Rauli Ruohonen
 Jeff Rush
 Sam Rushing
index d6f575463dc08afdee4c79b910f58db93d5e3c8a..57cab2d77e566bb4afcb63dfd09375fd8e5bfbdc 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -179,6 +179,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.