*** Python-2.7-orig/Lib/httplib.py 2010-06-04 09:32:14.000000000 -0700 --- Python-2.7/Lib/httplib.py 2012-11-06 17:04:16.432157776 -0800 *************** *** 554,559 **** --- 554,563 ---- self.length -= len(s) if not self.length: self.close() + else: + if not s: + self.close() + return s def _read_chunked(self, amt): *** Python-2.7-orig/Lib/test/test_httplib.py 2010-06-04 10:17:09.000000000 -0700 --- Python-2.7/Lib/test/test_httplib.py 2012-11-06 17:08:51.178598198 -0800 *************** *** 96,102 **** self.assertEquals(repr(exc), '''BadStatusLine("\'\'",)''') def test_partial_reads(self): ! # if we have a lenght, 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) --- 96,102 ---- self.assertEquals(repr(exc), '''BadStatusLine("\'\'",)''') def test_partial_reads(self): ! # 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) *************** *** 107,112 **** --- 107,125 ---- 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