Issue #16298: In HTTPResponse.read(), close the socket when there is no Content-Length and the incoming stream is finished.
Patch by Eran Rundstein.
diff --git a/Lib/httplib.py b/Lib/httplib.py
index 4c8b0fe..39a388e 100644
--- a/Lib/httplib.py
+++ b/Lib/httplib.py
@@ -565,6 +565,10 @@
             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 5b5ae2e..bd48fea 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -166,7 +166,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)
@@ -177,6 +177,19 @@
         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