]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix Issue14721: Send Content-length: 0 for empty body () in the http.client requests
authorSenthil Kumaran <senthil@uthcode.com>
Sat, 19 May 2012 08:58:09 +0000 (16:58 +0800)
committerSenthil Kumaran <senthil@uthcode.com>
Sat, 19 May 2012 08:58:09 +0000 (16:58 +0800)
Lib/http/client.py
Lib/test/test_httplib.py
Misc/NEWS

index d226f63ac4ad52f40d59aa3af66690889e1d0e1f..97a71559dcd427d4e5a62bf16df78044a9ba7ec1 100644 (file)
@@ -997,7 +997,7 @@ class HTTPConnection:
 
         self.putrequest(method, url, **skips)
 
-        if body and ('content-length' not in header_names):
+        if body is not None and ('content-length' not in header_names):
             self._set_content_length(body)
         for hdr, value in headers.items():
             self.putheader(hdr, value)
index 7c6c5bccf49f5370594836bdd9aa0995b8f2a7de..24c72ff37c9e7fd01617a7ff252bc7b1ccb2a7c1 100644 (file)
@@ -99,6 +99,34 @@ class HeaderTests(TestCase):
                 conn.request('POST', '/', body, headers)
                 self.assertEqual(conn._buffer.count[header.lower()], 1)
 
+    def test_content_length_0(self):
+
+        class ContentLengthChecker(list):
+            def __init__(self):
+                list.__init__(self)
+                self.content_length = None
+            def append(self, item):
+                kv = item.split(b':', 1)
+                if len(kv) > 1 and kv[0].lower() == b'content-length':
+                    self.content_length = kv[1].strip()
+                list.append(self, item)
+
+        # POST with empty body
+        conn = client.HTTPConnection('example.com')
+        conn.sock = FakeSocket(None)
+        conn._buffer = ContentLengthChecker()
+        conn.request('POST', '/', '')
+        self.assertEqual(conn._buffer.content_length, b'0',
+                        'Header Content-Length not set')
+
+        # PUT request with empty body
+        conn = client.HTTPConnection('example.com')
+        conn.sock = FakeSocket(None)
+        conn._buffer = ContentLengthChecker()
+        conn.request('PUT', '/', '')
+        self.assertEqual(conn._buffer.content_length, b'0',
+                        'Header Content-Length not set')
+
     def test_putheader(self):
         conn = client.HTTPConnection('example.com')
         conn.sock = FakeSocket(None)
index f42031248397ad46d4aa3fdfb1b7d787650792be..4cbea2463a9a6bdb17cde86aca55d5721ff269fa 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -63,6 +63,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #14721: Send the correct 'Content-length: 0' header when the body is an
+  empty string ''. Initial Patch contributed by Arve Knudsen.
+
 - Issue #9374: Generic parsing of query and fragment portions of url for any
   scheme. Supported both by RFC3986 and RFC2396.