feat: handle HEAD requests

This commit is contained in:
2026-05-11 17:26:21 +02:00
parent b6d751a97a
commit d3d98bd9b2
2 changed files with 24 additions and 6 deletions
+9 -4
View File
@@ -153,7 +153,8 @@ class BaseHandler(abc.ABC, http.server.BaseHTTPRequestHandler):
self.send_header("Content-Length", str(len(encoded))) self.send_header("Content-Length", str(len(encoded)))
self.send_header("Connection", "close") self.send_header("Connection", "close")
self.end_headers() self.end_headers()
self.wfile.write(encoded) if self.command != http.HTTPMethod.HEAD:
self.wfile.write(encoded)
self.close_connection = True self.close_connection = True
def send_status_only( def send_status_only(
@@ -222,7 +223,7 @@ class BaseHandler(abc.ABC, http.server.BaseHTTPRequestHandler):
self.send_header("Content-Length", str(out_size := len(response.content))) self.send_header("Content-Length", str(out_size := len(response.content)))
self.send_header("Connection", "close") self.send_header("Connection", "close")
self.end_headers() self.end_headers()
if out_size > 0: if out_size > 0 and self.command != http.HTTPMethod.HEAD:
self.wfile.write(response.content) self.wfile.write(response.content)
self.close_connection = True self.close_connection = True
@@ -387,9 +388,13 @@ class RequestHandler(http.server.SimpleHTTPRequestHandler, BaseHandler):
def do_HEAD(self) -> None: def do_HEAD(self) -> None:
with self.handle_errors(): with self.handle_errors():
self._pre_log_request() self._pre_log_request()
if not self._proxy_or_redirect(): if self._proxy_or_redirect():
super().do_HEAD() return None
if self.path == "/" and self.host == self.default_host:
return self.send_basic_body(self.server_signature())
super().do_HEAD()
self.close_connection = True self.close_connection = True
return None
@typing.override @typing.override
def do_GET(self) -> None: def do_GET(self) -> None:
+15 -2
View File
@@ -36,6 +36,7 @@ class BaseHandlerTestCase(BaseTestCase, abc.ABC):
code: int, code: int,
message: str | None = None, message: str | None = None,
headers: dict[str, str] | None = None, headers: dict[str, str] | None = None,
content_length: int = 0,
) -> typing.Iterator[None]: ) -> typing.Iterator[None]:
if headers is None: if headers is None:
headers = {} headers = {}
@@ -46,7 +47,7 @@ class BaseHandlerTestCase(BaseTestCase, abc.ABC):
send_response_mock.assert_called_once_with(code, message) send_response_mock.assert_called_once_with(code, message)
send_header_mock.assert_has_calls( send_header_mock.assert_has_calls(
[ [
unittest.mock.call("Content-Length", "0"), unittest.mock.call("Content-Length", str(content_length)),
] ]
+ [unittest.mock.call(header, value) for header, value in headers.items()], + [unittest.mock.call(header, value) for header, value in headers.items()],
any_order=True, any_order=True,
@@ -192,9 +193,21 @@ class TestRequestHandler(BaseHandlerTestCase):
token_manager=self.token_manager, token_manager=self.token_manager,
) )
def test_do_head_forward(self) -> None: def test_do_head_index(self) -> None:
handler = self._get_handler() handler = self._get_handler()
with ( with (
self.expects_status_only(
handler, 200, content_length=len(handler.server_signature())
),
self.patch("http.server.SimpleHTTPRequestHandler.do_HEAD", count=0),
self.seal_mocks(),
):
handler.do_HEAD()
def test_do_head_forward(self) -> None:
handler = self._get_handler("/file")
with (
self.mock_call(self.registry.get_from_path, ["file"], Page("file")),
self.patch("http.server.SimpleHTTPRequestHandler.do_HEAD"), self.patch("http.server.SimpleHTTPRequestHandler.do_HEAD"),
self.seal_mocks(), self.seal_mocks(),
): ):