diff -r 8215aaccc9dd -r 9b13e82da2d3 Lib/httplib.py --- a/Lib/httplib.py Fri May 04 16:33:30 2012 +0200 +++ b/Lib/httplib.py Fri May 04 20:55:24 2012 +0200 @@ -963,7 +963,7 @@ def _set_content_length(self, body): # Set the content-length based on the body. - thelen = None + thelen = '0' try: thelen = str(len(body)) except TypeError, te: @@ -972,11 +972,9 @@ try: thelen = str(os.fstat(body.fileno()).st_size) except (AttributeError, OSError): - # Don't send a length if this failed if self.debuglevel> 0: print "Cannot stat!!" - if thelen is not None: - self.putheader('Content-Length', thelen) + self.putheader('Content-Length', thelen) def _send_request(self, method, url, body, headers): # Honor explicitly requested Host: and Accept-Encoding: headers. @@ -989,7 +987,9 @@ self.putrequest(method, url, **skips) - if body and ('content-length' not in header_names): + want_content_length = body or method.lower() in ('post', 'put', + 'patch', 'delete', 'head') + if want_content_length and 'content-length' not in header_names: self._set_content_length(body) for hdr, value in headers.iteritems(): self.putheader(hdr, value) diff -r 8215aaccc9dd -r 9b13e82da2d3 Lib/test/test_httplib.py --- a/Lib/test/test_httplib.py Fri May 04 16:33:30 2012 +0200 +++ b/Lib/test/test_httplib.py Fri May 04 20:55:24 2012 +0200 @@ -59,6 +59,16 @@ raise AssertionError('caller tried to read past EOF') return data +class ContentLengthChecker(list): + def __init__(self): + list.__init__(self) + self.content_length = [] + def append(self, item): + kv = item.split(':', 1) + if len(kv)> 1 and kv[0].lower() == 'content-length': + self.content_length.append(kv[1].strip()) + list.append(self, item) + class HeaderTests(TestCase): def test_auto_headers(self): @@ -90,6 +100,32 @@ conn.request('POST', '/', body, headers) self.assertEqual(conn._buffer.count[header.lower()], 1) + def test_content_length_0(self): + # Header content-length should be 0 when posting without content + for method in 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD': + for content in '', None: + conn = httplib.HTTPConnection('example.com') + conn.sock = FakeSocket('blahblahblah') + conn._buffer = ContentLengthChecker() + conn.request(method, '/', content) + + self.assertEqual(conn._buffer.content_length, ['0'], + 'Header Content-Length not set for method {0}, content \'{1}\''.format( + method, content)) + + def test_content_length_omitted(self): + # We shouldn't define content-length for certain methods upon empty content + for method in 'GET', 'TRACE', 'OPTIONS', 'CONNECT': + for content in '', None: + conn = httplib.HTTPConnection('example.com') + conn.sock = FakeSocket('blahblahblah') + conn._buffer = ContentLengthChecker() + conn.request(method, '/', content) + + self.assertEqual(conn._buffer.content_length, [], + 'Header Content-Length set for method {0}, content \'{1}\''.format( + method, content)) + def test_putheader(self): conn = httplib.HTTPConnection('example.com') conn.sock = FakeSocket(None)

AltStyle によって変換されたページ (->オリジナル) /