cpython: 6da1ab5f777d (original) (raw)
Mercurial > cpython
changeset 77048:6da1ab5f777d 3.2
Fix Issue14721: Send Content-length: 0 for empty body () in the http.client requests
Senthil Kumaran senthil@uthcode.com | |
---|---|
date | Sat, 19 May 2012 16:58:09 +0800 |
parents | a9d43e21f7d8 |
children | 732d70746fc0 9f6b7576c08c |
files | Lib/http/client.py Lib/test/test_httplib.py Misc/NEWS |
diffstat | 3 files changed, 32 insertions(+), 1 deletions(-)[+] [-] Lib/http/client.py 2 Lib/test/test_httplib.py 28 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/http/client.py +++ b/Lib/http/client.py @@ -997,7 +997,7 @@ class HTTPConnection: self.putrequest(method, url, **skips)
if body and ('content-length' not in header_names):[](#l1.7)
if body is not None and ('content-length' not in header_names):[](#l1.8) self._set_content_length(body)[](#l1.9) for hdr, value in headers.items():[](#l1.10) self.putheader(hdr, value)[](#l1.11)
--- 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)
class ContentLengthChecker(list):[](#l2.9)
def __init__(self):[](#l2.10)
list.__init__(self)[](#l2.11)
self.content_length = None[](#l2.12)
def append(self, item):[](#l2.13)
kv = item.split(b':', 1)[](#l2.14)
if len(kv) > 1 and kv[0].lower() == b'content-length':[](#l2.15)
self.content_length = kv[1].strip()[](#l2.16)
list.append(self, item)[](#l2.17)
# POST with empty body[](#l2.19)
conn = client.HTTPConnection('example.com')[](#l2.20)
conn.sock = FakeSocket(None)[](#l2.21)
conn._buffer = ContentLengthChecker()[](#l2.22)
conn.request('POST', '/', '')[](#l2.23)
self.assertEqual(conn._buffer.content_length, b'0',[](#l2.24)
'Header Content-Length not set')[](#l2.25)
# PUT request with empty body[](#l2.27)
conn = client.HTTPConnection('example.com')[](#l2.28)
conn.sock = FakeSocket(None)[](#l2.29)
conn._buffer = ContentLengthChecker()[](#l2.30)
conn.request('PUT', '/', '')[](#l2.31)
self.assertEqual(conn._buffer.content_length, b'0',[](#l2.32)
'Header Content-Length not set')[](#l2.33)
+ def test_putheader(self): conn = client.HTTPConnection('example.com') conn.sock = FakeSocket(None)
--- 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