diff -r 0ddba0abab49 Doc/library/http.server.rst --- a/Doc/library/http.server.rst Thu Jun 02 10:13:47 2016 +0000 +++ b/Doc/library/http.server.rst Sat Jun 04 19:12:07 2016 -0700 @@ -191,7 +191,9 @@ a complete set of headers, as the response body. The :attr:`responses` attribute holds the default values for *message* and *explain* that will be used if no value is provided; for unknown codes the default value - for both is the string ``???``. + for both is the string ``???``. The body will be empty if the method is + HEAD or the response code is one of the following: ``1xx``, + ``204 No Content``, ``205 Reset Content``, ``304 Not Modified``. .. versionchanged:: 3.4 The error response includes a Content-Length header. diff -r 0ddba0abab49 Lib/http/server.py --- a/Lib/http/server.py Thu Jun 02 10:13:47 2016 +0000 +++ b/Lib/http/server.py Sat Jun 04 19:12:07 2016 -0700 @@ -454,15 +454,22 @@ }) body = content.encode('UTF-8', 'replace') self.send_response(code, message) + + send_body = (self.command != 'HEAD' and + code >= 200 and + code not in (HTTPStatus.NO_CONTENT, + HTTPStatus.RESET_CONTENT, + HTTPStatus.NOT_MODIFIED)) self.send_header("Content-Type", self.error_content_type) self.send_header('Connection', 'close') - self.send_header('Content-Length', int(len(body))) + if self.command == 'HEAD' or send_body: + # Content-Length will not be sent for cases mentioned in below RFCs. + # - RFC7230: 3.3.2. 1xx, 204(No Content), 304(Not Modified) + # - RFC7231: 6.3.6. 205(Reset Content) + self.send_header('Content-Length', int(len(body))) self.end_headers() - if (self.command != 'HEAD' and - code >= 200 and - code not in ( - HTTPStatus.NO_CONTENT, HTTPStatus.NOT_MODIFIED)): + if send_body: self.wfile.write(body) def send_response(self, code, message=None): diff -r 0ddba0abab49 Lib/test/test_httpservers.py --- a/Lib/test/test_httpservers.py Thu Jun 02 10:13:47 2016 +0000 +++ b/Lib/test/test_httpservers.py Sat Jun 04 19:12:07 2016 -0700 @@ -116,6 +116,24 @@ body = self.headers['x-special-incoming'].encode('utf-8') self.wfile.write(body) + def do_NOCONTENT_VIA_SEND_ERROR(self): + self.send_error(HTTPStatus.NO_CONTENT) + + def do_RESETCONTENT_VIA_SEND_ERROR(self): + self.send_error(HTTPStatus.RESET_CONTENT) + + def do_NOTMODIFIED_VIA_SEND_ERROR(self): + self.send_error(HTTPStatus.NOT_MODIFIED) + + def do_SWITCHING_PROTOCOLS_VIA_SEND_ERROR(self): + self.send_error(HTTPStatus.SWITCHING_PROTOCOLS) + + def do_PROCESSING_VIA_SEND_ERROR(self): + self.send_error(HTTPStatus.PROCESSING) + + def do_HEAD(self): + self.send_error(HTTPStatus.OK) + def setUp(self): BaseTestCase.setUp(self) self.con = http.client.HTTPConnection(self.HOST, self.PORT) @@ -237,6 +255,63 @@ data = res.read() self.assertEqual(int(res.getheader('Content-Length')), len(data)) + def test_no_content_via_send_error(self): + self.con.request('NOCONTENT_VIA_SEND_ERROR', '/') + res = self.con.getresponse() + self.assertEqual(HTTPStatus.NO_CONTENT, res.status) + self.assertEqual(None, res.getheader('Content-Length')) + + data = res.read() + self.assertEqual(b'', data) + + def test_reset_content_via_send_error(self): + self.con.request('RESETCONTENT_VIA_SEND_ERROR', '/') + res = self.con.getresponse() + self.assertEqual(HTTPStatus.RESET_CONTENT, res.status) + self.assertEqual(None, res.getheader('Content-Length')) + + data = res.read() + self.assertEqual(b'', data) + + def test_not_modified_via_send_error(self): + self.con.request('NOTMODIFIED_VIA_SEND_ERROR', '/') + res = self.con.getresponse() + self.assertEqual(HTTPStatus.NOT_MODIFIED, res.status) + self.assertEqual(None, res.getheader('Content-Length')) + self.assertEqual(None, res.getheader('Transfer-Encoding')) + + data = res.read() + self.assertEqual(b'', data) + + def test_switching_protocols_via_send_error(self): + self.con.request('SWITCHING_PROTOCOLS_VIA_SEND_ERROR', '/') + res = self.con.getresponse() + self.assertEqual(HTTPStatus.SWITCHING_PROTOCOLS, res.status) + self.assertEqual(None, res.getheader('Content-Length')) + self.assertEqual(None, res.getheader('Transfer-Encoding')) + + data = res.read() + self.assertEqual(b'', data) + + def test_processing_via_send_error(self): + self.con.request('PROCESSING_VIA_SEND_ERROR', '/') + res = self.con.getresponse() + self.assertEqual(HTTPStatus.PROCESSING, res.status) + self.assertEqual(None, res.getheader('Content-Length')) + self.assertEqual(None, res.getheader('Transfer-Encoding')) + + data = res.read() + self.assertEqual(b'', data) + + def test_head_via_send_error(self): + self.con.request('HEAD', '/') + res = self.con.getresponse() + self.assertEqual(HTTPStatus.OK, res.status) + self.assertTrue(int(res.getheader('Content-Length')) > 0) + + data = res.read() + self.assertEqual(b'', data) + class RequestHandlerLoggingTestCase(BaseTestCase): class request_handler(BaseHTTPRequestHandler):