]> granicus.if.org Git - python/commitdiff
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
authorSenthil Kumaran <senthil@uthcode.com>
Sat, 19 May 2012 08:52:21 +0000 (16:52 +0800)
committerSenthil Kumaran <senthil@uthcode.com>
Sat, 19 May 2012 08:52:21 +0000 (16:52 +0800)
Lib/httplib.py
Lib/test/test_httplib.py
Misc/NEWS

index 5d16e53c4c393b7c61c0438537af64dafe78dc2b..98296dc3c52285b7fbf66833af852dd7bd88f001 100644 (file)
@@ -989,7 +989,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.iteritems():
             self.putheader(hdr, value)
index 05df87513a2ef74bc19c17fccae9aae96861b72e..5b5ae2ef49af1977d5389001f2a31b5e75e3ed61 100644 (file)
@@ -90,6 +90,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(':', 1)
+                if len(kv) > 1 and kv[0].lower() == 'content-length':
+                    self.content_length = kv[1].strip()
+                list.append(self, item)
+
+        # POST with empty body
+        conn = httplib.HTTPConnection('example.com')
+        conn.sock = FakeSocket(None)
+        conn._buffer = ContentLengthChecker()
+        conn.request('POST', '/', '')
+        self.assertEqual(conn._buffer.content_length, '0',
+                        'Header Content-Length not set')
+
+        # PUT request with empty body
+        conn = httplib.HTTPConnection('example.com')
+        conn.sock = FakeSocket(None)
+        conn._buffer = ContentLengthChecker()
+        conn.request('PUT', '/', '')
+        self.assertEqual(conn._buffer.content_length, '0',
+                        'Header Content-Length not set')
+
     def test_putheader(self):
         conn = httplib.HTTPConnection('example.com')
         conn.sock = FakeSocket(None)
index 646b35155ae4eb2558fa0d05fdebc5a1e8c8db14..503e2f8052f861f15abcaf76d29fa4d807d81bcf 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -60,6 +60,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #14721: Send proper header, Content-length: 0 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.