[Python-checkins] r61034 - in python/trunk: Lib/httplib.py Lib/test/test_httplib.py Misc/NEWS

georg.brandl python-checkins at python.org
Sun Feb 24 01:03:23 CET 2008


Author: georg.brandl
Date: Sun Feb 24 01:03:22 2008
New Revision: 61034

Modified:
   python/trunk/Lib/httplib.py
   python/trunk/Lib/test/test_httplib.py
   python/trunk/Misc/NEWS
Log:
#900744: If an invalid chunked-encoding header is sent by a server,
httplib will now raise IncompleteRead and close the connection instead
of raising ValueError.


Modified: python/trunk/Lib/httplib.py
==============================================================================
--- python/trunk/Lib/httplib.py	(original)
+++ python/trunk/Lib/httplib.py	Sun Feb 24 01:03:22 2008
@@ -546,7 +546,13 @@
                 i = line.find(';')
                 if i >= 0:
                     line = line[:i] # strip chunk-extensions
-                chunk_left = int(line, 16)
+                try:
+                    chunk_left = int(line, 16)
+                except ValueError:
+                    # close the connection as protocol synchronisation is
+                    # probably lost
+                    self.close()
+                    raise IncompleteRead(value)
                 if chunk_left == 0:
                     break
             if amt is None:

Modified: python/trunk/Lib/test/test_httplib.py
==============================================================================
--- python/trunk/Lib/test/test_httplib.py	(original)
+++ python/trunk/Lib/test/test_httplib.py	Sun Feb 24 01:03:22 2008
@@ -156,6 +156,35 @@
         conn.request('GET', '/foo', body)
         self.assertTrue(sock.data.startswith(expected))
 
+    def test_chunked(self):
+        chunked_start = (
+            'HTTP/1.1 200 OK\r\n'
+            'Transfer-Encoding: chunked\r\n\r\n'
+            'a\r\n'
+            'hello worl\r\n'
+            '1\r\n'
+            'd\r\n'
+        )
+        sock = FakeSocket(chunked_start + '0\r\n')
+        resp = httplib.HTTPResponse(sock, method="GET")
+        resp.begin()
+        self.assertEquals(resp.read(), 'hello world')
+        resp.close()
+
+        for x in ('', 'foo\r\n'):
+            sock = FakeSocket(chunked_start + x)
+            resp = httplib.HTTPResponse(sock, method="GET")
+            resp.begin()
+            try:
+                resp.read()
+            except httplib.IncompleteRead, i:
+                self.assertEquals(i.partial, 'hello world')
+            else:
+                self.fail('IncompleteRead expected')
+            finally:
+                resp.close()
+
+
 class OfflineTest(TestCase):
     def test_responses(self):
         self.assertEquals(httplib.responses[httplib.NOT_FOUND], "Not Found")

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sun Feb 24 01:03:22 2008
@@ -441,6 +441,10 @@
 Library
 -------
 
+- #900744: If an invalid chunked-encoding header is sent by a server,
+  httplib will now raise IncompleteRead and close the connection instead
+  of raising ValueError.
+
 - #1492: The content type of BaseHTTPServer error messages can now be
   overridden.
 


More information about the Python-checkins mailing list