Skip to content

Commit e4f4a89

Browse files
picnixzdanigm
authored andcommitted
pythongh-70765: avoid waiting for HTTP headers when parsing HTTP/0.9 requests (python#139514)
1 parent 2613b57 commit e4f4a89

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
@@ -302,6 +302,7 @@ def parse_request(self):
302302
error response has already been sent back.
303303
304304
"""
305+
is_http_0_9 = False
305306
self.command = None # set in case of error on the first line
306307
self.request_version = version = self.default_request_version
307308
self.close_connection = True
@@ -359,6 +360,7 @@ def parse_request(self):
359360
HTTPStatus.BAD_REQUEST,
360361
"Bad HTTP/0.9 request type (%r)" % command)
361362
return False
363+
is_http_0_9 = True
362364
self.command, self.path = command, path
363365

364366
# gh-87389: The purpose of replacing '//' with '/' is to protect
@@ -368,6 +370,11 @@ def parse_request(self):
368370
if self.path.startswith('//'):
369371
self.path = '/' + self.path.lstrip('/') # Reduce to a single /
370372

373+
# For HTTP/0.9, headers are not expected at all.
374+
if is_http_0_9:
375+
self.headers = {}
376+
return True
377+
371378
# Examine the headers and look for a Connection directive.
372379
try:
373380
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
@@ -362,6 +362,43 @@ def test_head_via_send_error(self):
362362
self.assertEqual(b'', data)
363363

364364

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

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)