summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/http/client.py2
-rw-r--r--Lib/test/test_httplib.py28
-rw-r--r--Misc/NEWS3
3 files changed, 32 insertions, 1 deletions
diff --git a/Lib/http/client.py b/Lib/http/client.py
index 6089192..9b01704 100644
--- a/Lib/http/client.py
+++ b/Lib/http/client.py
@@ -1076,7 +1076,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)
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
index cc6cd07..cf61552 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -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)
diff --git a/Misc/NEWS b/Misc/NEWS
index 2794e16..013c8f1 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -38,6 +38,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.