Tests/LibWeb: Let .headers override SimpleHTTPRequestHandler headers

The test HTTP server used to append headers from .headers files after
SimpleHTTPRequestHandler had already emitted its own headers.

For headers such as Last-Modified, this produced duplicate response
headers. LibHTTP combines duplicate header values with ", ", which made
Document::lastModified unable to parse the configured Last-Modified
value and fall back to the current time.

Suppress SimpleHTTPServer headers whose names are provided by a .headers
file, while still allowing the explicit .headers values through.
This commit is contained in:
Shannon Booth 2026-05-16 12:54:27 +02:00 committed by Tim Flynn
parent 5d9641dd11
commit 8a85146e35

View file

@ -70,11 +70,24 @@ class TestHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
if hasattr(self, "_extra_headers"):
self._sending_extra_headers = True
for key, value in self._extra_headers:
self.send_header(key, value)
self._sending_extra_headers = False
del self._extra_headers
del self._extra_header_names
super().end_headers()
def send_header(self, keyword, value):
# Headers from .headers files override headers created by SimpleHTTPRequestHandler.
if (
hasattr(self, "_extra_header_names")
and not getattr(self, "_sending_extra_headers", False)
and keyword.lower() in self._extra_header_names
):
return
super().send_header(keyword, value)
def _serve_static_request(self):
if self.path.startswith("/static/"):
# Explicit /static/ URLs continue to serve files from the general test root.
@ -90,12 +103,14 @@ class TestHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
if os.path.isfile(headers_path):
self._extra_headers = []
self._extra_header_names = set()
with open(headers_path) as f:
for line in f:
line = line.strip()
if ":" in line:
key, _, value = line.partition(":")
self._extra_headers.append((key.strip(), value.strip()))
self._extra_header_names.add(key.strip().lower())
super().do_GET()