Skip to content

Commit 0c91d68

Browse files
authored
[3.13] gh-70765: avoid waiting for HTTP headers when parsing HTTP/0.9 requests (GH-139514) (#139602)
(cherry picked from commit 13dc2fd) (cherry picked from commit 1fe89d3)
1 parent b7bc977 commit 0c91d68

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

Lib/http/server.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ def parse_request(self):
275275
error response has already been sent back.
276276
277277
"""
278+
is_http_0_9 = False
278279
self.command = None # set in case of error on the first line
279280
self.request_version = version = self.default_request_version
280281
self.close_connection = True
@@ -332,6 +333,7 @@ def parse_request(self):
332333
HTTPStatus.BAD_REQUEST,
333334
"Bad HTTP/0.9 request type (%r)" % command)
334335
return False
336+
is_http_0_9 = True
335337
self.command, self.path = command, path
336338

337339
# gh-87389: The purpose of replacing '//' with '/' is to protect
@@ -341,6 +343,11 @@ def parse_request(self):
341343
if self.path.startswith('//'):
342344
self.path = '/' + self.path.lstrip('/') # Reduce to a single /
343345

346+
# For HTTP/0.9, headers are not expected at all.
347+
if is_http_0_9:
348+
self.headers = {}
349+
return True
350+
344351
# Examine the headers and look for a Connection directive.
345352
try:
346353
self.headers = http.client.parse_headers(self.rfile,

Lib/test/test_httpservers.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
SimpleHTTPRequestHandler, CGIHTTPRequestHandler
99
from http import server, HTTPStatus
1010

11+
import contextlib
1112
import os
1213
import socket
1314
import sys
@@ -316,6 +317,44 @@ def test_head_via_send_error(self):
316317
self.assertEqual(b'', data)
317318

318319

320+
class HTTP09ServerTestCase(BaseTestCase):
321+
322+
class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler):
323+
"""Request handler for HTTP/0.9 server."""
324+
325+
def do_GET(self):
326+
self.wfile.write(f'OK: here is {self.path}\r\n'.encode())
327+
328+
def setUp(self):
329+
super().setUp()
330+
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
331+
self.sock = self.enterContext(self.sock)
332+
self.sock.connect((self.HOST, self.PORT))
333+
334+
def test_simple_get(self):
335+
self.sock.send(b'GET /index.html\r\n')
336+
res = self.sock.recv(1024)
337+
self.assertEqual(res, b"OK: here is /index.html\r\n")
338+
339+
def test_invalid_request(self):
340+
self.sock.send(b'POST /index.html\r\n')
341+
res = self.sock.recv(1024)
342+
self.assertIn(b"Bad HTTP/0.9 request type ('POST')", res)
343+
344+
def test_single_request(self):
345+
self.sock.send(b'GET /foo.html\r\n')
346+
res = self.sock.recv(1024)
347+
self.assertEqual(res, b"OK: here is /foo.html\r\n")
348+
349+
# Ignore errors if the connection is already closed,
350+
# as this is the expected behavior of HTTP/0.9.
351+
with contextlib.suppress(OSError):
352+
self.sock.send(b'GET /bar.html\r\n')
353+
res = self.sock.recv(1024)
354+
# The server should not process our request.
355+
self.assertEqual(res, b'')
356+
357+
319358
class RequestHandlerLoggingTestCase(BaseTestCase):
320359
class request_handler(BaseHTTPRequestHandler):
321360
protocol_version = 'HTTP/1.1'
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)