Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-131715 Fix Error calling BaseHTTPRequestHandler.end_headers() #131718

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,9 @@ def send_header(self, keyword, value):
def end_headers(self):
"""Send the blank line ending the MIME headers."""
if self.request_version != 'HTTP/0.9':
self._headers_buffer.append(b"\r\n")
self.flush_headers()
if hasattr(self, '_headers_buffer'):
self._headers_buffer.append(b"\r\n")
self.flush_headers()

def flush_headers(self):
if hasattr(self, '_headers_buffer'):
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ def do_TEST(self):
self.send_header('Connection', 'close')
self.end_headers()

def do_NOHEADERS(self):
try:
self.end_headers()
self.send_error(HTTPStatus.OK, "OK")
except Exception as e:
self.send_error(HTTPStatus.INTERNAL_SERVER_ERROR, str(e))

def do_KEEP(self):
self.send_response(HTTPStatus.NO_CONTENT)
self.send_header('Content-Type', 'text/html')
Expand Down Expand Up @@ -236,6 +243,11 @@ def test_handler(self):
res = self.con.getresponse()
self.assertEqual(res.status, HTTPStatus.NO_CONTENT)

def test_end_no_headers(self):
self.con.request('NOHEADERS', '/')
res = self.con.getresponse()
self.assertEqual(res.status, HTTPStatus.OK)

def test_return_header_keep_alive(self):
self.con.request('KEEP', '/')
res = self.con.getresponse()
Expand Down
Loading