Skip to content

Commit 6c9dd44

Browse files
picnixzmiss-islington
authored andcommitted
gh-70765: avoid waiting for HTTP headers when parsing HTTP/0.9 requests (GH-139514)
(cherry picked from commit 13dc2fd) Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 1ffd62b commit 6c9dd44

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

Lib/http/server.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ def parse_request(self):
324324
error response has already been sent back.
325325
326326
"""
327+
is_http_0_9 = False
327328
self.command = None # set in case of error on the first line
328329
self.request_version = version = self.default_request_version
329330
self.close_connection = True
@@ -381,6 +382,7 @@ def parse_request(self):
381382
HTTPStatus.BAD_REQUEST,
382383
"Bad HTTP/0.9 request type (%r)" % command)
383384
return False
385+
is_http_0_9 = True
384386
self.command, self.path = command, path
385387

386388
# gh-87389: The purpose of replacing '//' with '/' is to protect
@@ -390,6 +392,11 @@ def parse_request(self):
390392
if self.path.startswith('//'):
391393
self.path = '/' + self.path.lstrip('/') # Reduce to a single /
392394

395+
# For HTTP/0.9, headers are not expected at all.
396+
if is_http_0_9:
397+
self.headers = {}
398+
return True
399+
393400
# Examine the headers and look for a Connection directive.
394401
try:
395402
self.headers = http.client.parse_headers(self.rfile,

Lib/test/test_httpservers.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,43 @@ def test_head_via_send_error(self):
359359
self.assertEqual(b'', data)
360360

361361

362+
class HTTP09ServerTestCase(BaseTestCase):
363+
364+
class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler):
365+
"""Request handler for HTTP/0.9 server."""
366+
367+
def do_GET(self):
368+
self.wfile.write(f'OK: here is {self.path}\r\n'.encode())
369+
370+
def setUp(self):
371+
super().setUp()
372+
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
373+
self.sock = self.enterContext(self.sock)
374+
self.sock.connect((self.HOST, self.PORT))
375+
376+
def test_simple_get(self):
377+
self.sock.send(b'GET /index.html\r\n')
378+
res = self.sock.recv(1024)
379+
self.assertEqual(res, b"OK: here is /index.html\r\n")
380+
381+
def test_invalid_request(self):
382+
self.sock.send(b'POST /index.html\r\n')
383+
res = self.sock.recv(1024)
384+
self.assertIn(b"Bad HTTP/0.9 request type ('POST')", res)
385+
386+
def test_single_request(self):
387+
self.sock.send(b'GET /foo.html\r\n')
388+
res = self.sock.recv(1024)
389+
self.assertEqual(res, b"OK: here is /foo.html\r\n")
390+
391+
self.sock.send(b'GET /bar.html\r\n')
392+
res = self.sock.recv(1024)
393+
# The server will not parse more input as it closed the connection.
394+
# Note that the socket connection itself is still opened since the
395+
# client is responsible for also closing it on their side.
396+
self.assertEqual(res, b'')
397+
398+
362399
def certdata_file(*path):
363400
return os.path.join(os.path.dirname(__file__), "certdata", *path)
364401

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:mod:`http.server`: fix default handling of HTTP/0.9 requests in
2+
:class:`~http.server.BaseHTTPRequestHandler`. Previously,
3+
:meth:`!BaseHTTPRequestHandler.parse_request`` incorrectly
4+
waited for headers in the request although those are not
5+
supported in HTTP/0.9. Patch by Bénédikt Tran.

0 commit comments

Comments
 (0)