Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52d2720

Browse files
committed
Issue #16088: BaseHTTPRequestHandler's send_error method includes a
Content-Length header. Patch by Antoine Pitrou.
1 parent ec7c16d commit 52d2720

4 files changed

Lines changed: 21 additions & 1 deletion

File tree

Doc/library/http.server.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ of which this module provides three different variants:
177177
complete set of headers is sent, followed by text composed using the
178178
:attr:`error_message_format` class variable.
179179

180+
.. versionchanged:: 3.4
181+
The error response includes a Content-Length header.
182+
180183
.. method:: send_response(code, message=None)
181184

182185
Adds a response header to the headers buffer and logs the accepted

Lib/http/server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,12 +425,14 @@ def send_error(self, code, message=None):
425425
# using _quote_html to prevent Cross Site Scripting attacks (see bug #1100201)
426426
content = (self.error_message_format %
427427
{'code': code, 'message': _quote_html(message), 'explain': explain})
428+
body = content.encode('UTF-8', 'replace')
428429
self.send_response(code, message)
429430
self.send_header("Content-Type", self.error_content_type)
430431
self.send_header('Connection', 'close')
432+
self.send_header('Content-Length', int(len(body)))
431433
self.end_headers()
432434
if self.command != 'HEAD' and code >= 200 and code not in (204, 304):
433-
self.wfile.write(content.encode('UTF-8', 'replace'))
435+
self.wfile.write(body)
434436

435437
def send_response(self, code, message=None):
436438
"""Add the response header to the headers buffer and log the

Lib/test/test_httpservers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ def do_KEEP(self):
9292
def do_KEYERROR(self):
9393
self.send_error(999)
9494

95+
def do_NOTFOUND(self):
96+
self.send_error(404)
97+
9598
def do_CUSTOM(self):
9699
self.send_response(999)
97100
self.send_header('Content-Type', 'text/html')
@@ -211,6 +214,15 @@ def test_latin1_header(self):
211214
self.assertEqual(res.getheader('X-Special'), 'Dängerous Mind')
212215
self.assertEqual(res.read(), 'Ärger mit Unicode'.encode('utf-8'))
213216

217+
def test_error_content_length(self):
218+
# Issue #16088: standard error responses should have a content-length
219+
self.con.request('NOTFOUND', '/')
220+
res = self.con.getresponse()
221+
self.assertEqual(res.status, 404)
222+
data = res.read()
223+
import pdb; pdb.set_trace()
224+
self.assertEqual(int(res.getheader('Content-Length')), len(data))
225+
214226

215227
class SimpleHTTPServerTestCase(BaseTestCase):
216228
class request_handler(NoLogRequestHandler, SimpleHTTPRequestHandler):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ Core and Builtins
4545
Library
4646
-------
4747

48+
- Issue #16088: BaseHTTPRequestHandler's send_error method includes a
49+
Content-Length header in it's response now. Patch by Antoine Pitrou.
50+
4851
- Issue #16114: The subprocess module no longer provides a misleading error
4952
message stating that args[0] did not exist when either the cwd or executable
5053
keyword arguments specified a path that did not exist.

0 commit comments

Comments
 (0)