[3.13] gh-70765: avoid waiting for HTTP headers when parsing HTTP/0.9 requests (GH-139514) (#139602)

(cherry picked from commit 13dc2fde8c)
(cherry picked from commit 1fe89d324e)
This commit is contained in:
Bénédikt Tran 2025-10-08 12:32:45 +02:00 committed by GitHub
parent b7bc9776e8
commit 0c91d6848a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 51 additions and 0 deletions

View file

@ -8,6 +8,7 @@
SimpleHTTPRequestHandler, CGIHTTPRequestHandler
from http import server, HTTPStatus
import contextlib
import os
import socket
import sys
@ -316,6 +317,44 @@ def test_head_via_send_error(self):
self.assertEqual(b'', data)
class HTTP09ServerTestCase(BaseTestCase):
class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler):
"""Request handler for HTTP/0.9 server."""
def do_GET(self):
self.wfile.write(f'OK: here is {self.path}\r\n'.encode())
def setUp(self):
super().setUp()
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock = self.enterContext(self.sock)
self.sock.connect((self.HOST, self.PORT))
def test_simple_get(self):
self.sock.send(b'GET /index.html\r\n')
res = self.sock.recv(1024)
self.assertEqual(res, b"OK: here is /index.html\r\n")
def test_invalid_request(self):
self.sock.send(b'POST /index.html\r\n')
res = self.sock.recv(1024)
self.assertIn(b"Bad HTTP/0.9 request type ('POST')", res)
def test_single_request(self):
self.sock.send(b'GET /foo.html\r\n')
res = self.sock.recv(1024)
self.assertEqual(res, b"OK: here is /foo.html\r\n")
# Ignore errors if the connection is already closed,
# as this is the expected behavior of HTTP/0.9.
with contextlib.suppress(OSError):
self.sock.send(b'GET /bar.html\r\n')
res = self.sock.recv(1024)
# The server should not process our request.
self.assertEqual(res, b'')
class RequestHandlerLoggingTestCase(BaseTestCase):
class request_handler(BaseHTTPRequestHandler):
protocol_version = 'HTTP/1.1'