Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(wpt): serve as-is fixtures as raw HTTP responses

ldm08b09e71d15a1
diff --git a/moli-benchmark/moli_benchmark/wpt_cross/server.py b/moli-benchmark/moli_benchmark/wpt_cross/server.py
index 9e1c9d5ec..8566afbe0 100644
--- a/moli-benchmark/moli_benchmark/wpt_cross/server.py
+++ b/moli-benchmark/moli_benchmark/wpt_cross/server.py
@@ -1155,43 +1155,6 @@ def _url_host_literal(hostname: str) -> str:
return hostname
-def _asis_response_parts(body: bytes) -> tuple[int, bytes, list[tuple[str, str]]] | None:
- """Parse the small raw HTTP ``.asis`` fixtures used by legacy WPT cases."""
-
- separator = b"\r\n\r\n" if b"\r\n\r\n" in body else b"\n\n"
- if separator not in body:
- return None
- header_block, response_body = body.split(separator, 1)
- lines = header_block.replace(b"\r\n", b"\n").split(b"\n")
- if not lines:
- return None
- status_line = lines[0].decode("ascii", errors="replace").strip()
- status_parts = status_line.split(None, 2)
- if len(status_parts) < 2 or not status_parts[0].startswith("HTTP/"):
- return None
- try:
- status_code = int(status_parts[1])
- except ValueError:
- return None
- if not 100 <= status_code <= 599:
- return None
- headers: list[tuple[str, str]] = []
- for raw_line in lines[1:]:
- line = raw_line.decode("latin-1").strip()
- if not line:
- continue
- name, separator, value = line.partition(":")
- if not separator:
- continue
- header_name = name.strip()
- header_value = value.strip()
- if header_name.lower() in {"content-length", "transfer-encoding"}:
- continue
- if _valid_static_response_header(header_name, header_value):
- headers.append((header_name, header_value))
- return status_code, response_body, headers
-
-
def _static_response_headers(
file_path: Path,
query: str,
@@ -1917,7 +1880,8 @@ def _make_handler(
if path in FETCH_ABORT_RESOURCE_PATHS:
self._serve_fetch_abort_resource(path, parsed.query, emit_body=emit_body)
return
- pipe_status_code = _pipe_response_status(parsed.query)
+ # wptserve writes .asis files directly before pipelines run.
+ pipe_status_code = None if path.endswith(".asis") else _pipe_response_status(parsed.query)
if path == "/reporting/resources/report.py":
self._serve_csp_report(parsed.query, emit_body=emit_body)
return
@@ -2054,6 +2018,17 @@ def _make_handler(
except OSError:
self.send_error(500)
return
+ if file_path.suffix == ".asis":
+ # Preserve the entire wire response, including missing header
+ # terminators, duplicate framing fields and malformed bytes.
+ # Like wptserve's AsIsHandler, bypass generated headers, HEAD
+ # body suppression, sidecars, substitution and response pipes.
+ self.close_connection = True
+ try:
+ self.wfile.write(body)
+ except (BrokenPipeError, ConnectionResetError):
+ pass
+ return
if is_any_js_window_wrapper:
self._send_bytes(
"text/html; charset=utf-8",
@@ -2230,21 +2205,6 @@ def _make_handler(
status_code=pipe_status_code or 200,
)
return
- if file_path.suffix == ".asis":
- parts = _asis_response_parts(body)
- if parts is not None:
- status_code, body, headers = parts
- self._send_bytes(
- "application/octet-stream",
- body,
- emit_body=emit_body,
- extra_headers=[
- *headers,
- *static_headers(),
- ],
- status_code=pipe_status_code or status_code,
- )
- return
mime, _ = mimetypes.guess_type(str(file_path))
if mime is None:
mime = "application/octet-stream"
diff --git a/moli-benchmark/tests/test_wpt_asis_server.py b/moli-benchmark/tests/test_wpt_asis_server.py
new file mode 100644
index 000000000..ed9c7275f
--- /dev/null
+++ b/moli-benchmark/tests/test_wpt_asis_server.py
@@ -0,0 +1,63 @@
+from __future__ import annotations
+
+import socket
+import tempfile
+import unittest
+from contextlib import ExitStack
+from pathlib import Path
+from unittest.mock import patch
+
+from moli_benchmark.wpt_cross.server import WptFixtureServer
+
+
+class WptAsIsFixtureTests(unittest.TestCase):
+ def setUp(self) -> None:
+ self.stack = ExitStack()
+ self.addCleanup(self.stack.close)
+ self.root = Path(self.stack.enter_context(tempfile.TemporaryDirectory()))
+ (self.root / "resources").mkdir()
+ (self.root / "resources/testharness.js").write_text("// testharness")
+ self.stack.enter_context(patch(
+ "moli_benchmark.wpt_cross.server._global_ipv6_address", return_value=None
+ ))
+ self.server = self.stack.enter_context(WptFixtureServer(self.root))
+
+ def raw_response(self, target: str, method: str = "GET") -> bytes:
+ with socket.create_connection(("127.0.0.1", self.server.port), timeout=2) as connection:
+ connection.sendall(
+ f"{method} {target} HTTP/1.1\r\nHost: localhost\r\n\r\n".encode("ascii")
+ )
+ parts = []
+ while chunk := connection.recv(4096):
+ parts.append(chunk)
+ return b"".join(parts)
+
+ def test_preserves_header_only_responses_and_framing_bytes(self) -> None:
+ responses = [
+ b"HTTP/1.1 200 YAYAYAYA\nfoo-TEST: 1\nFOO-test: 2\n__Custom: token\n",
+ b"HTTP/1.0 200 NANANA\nCONTENT-LENGTH: 0\ncontent-length:\t 0\n",
+ b"HTTP/1.1 202 Giraffe\r\nContent-Length: 999\r\n\r\nContent",
+ b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n1\r\nx\r\n0\r\n\r\n",
+ b"HTTP/1.1 699 \xff\nX-Bytes: \xa0\xc3\xbf\x85 \t\nHEYA: \x0b\x0c\n\n\x00\xff\x80",
+ b"not an HTTP response\x00\xff",
+ ]
+ for index, response in enumerate(responses):
+ with self.subTest(index=index):
+ (self.root / "response.asis").write_bytes(response)
+ self.assertEqual(self.raw_response("/response.asis"), response)
+
+ def test_bypasses_headers_substitution_and_response_pipes(self) -> None:
+ response = b"HTTP/1.1 202 Giraffe\nX-Test: PASS\n\n{{host}}\xff"
+ (self.root / "response.sub.asis").write_bytes(response)
+ (self.root / "response.sub.asis.headers").write_text("X-Sidecar: ignored\n")
+ for query in (
+ "", "?pipe=header(X-Test,FAIL)", "?pipe=status(201)",
+ "?pipe=slice(null,2)", "?pipe=sub", "?pipe=trickle(1:d2:r2)",
+ ):
+ with self.subTest(query=query):
+ self.assertEqual(self.raw_response("/response.sub.asis" + query), response)
+
+ def test_raw_writer_also_bypasses_head_body_suppression(self) -> None:
+ response = b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nbody"
+ (self.root / "response.asis").write_bytes(response)
+ self.assertEqual(self.raw_response("/response.asis", "HEAD"), response)
diff --git a/moli-benchmark/tests/test_wpt_cross.py b/moli-benchmark/tests/test_wpt_cross.py
index c7837631a..ce759332e 100644
--- a/moli-benchmark/tests/test_wpt_cross.py
+++ b/moli-benchmark/tests/test_wpt_cross.py
@@ -117,7 +117,6 @@ from moli_benchmark.wpt_cross.server import (
WptFixtureServer,
ResultsStore,
_apply_header_operations,
- _asis_response_parts,
_any_js_window_wrapper,
_bench_report_bridge,
_content_security_policy_resource_response,
@@ -5046,40 +5045,6 @@ test(() => {}, "ok");
self.assertIn(("Content-Type", "text/javascript"), headers)
self.assertIn(("Access-Control-Allow-Origin", "*"), headers)
- def test_fixture_server_parses_asis_response_parts(self) -> None:
- self.assertEqual(
- _asis_response_parts(
- b"HTTP/1.1 200 OK\n"
- b"Content-Type: text/plain\n"
- b"Access-Control-Allow-Origin: *\n"
- b"Content-Length: 999\n"
- b"\n"
- b"FAIL"
- ),
- (
- 200,
- b"FAIL",
- [
- ("Content-Type", "text/plain"),
- ("Access-Control-Allow-Origin", "*"),
- ],
- ),
- )
-
- def test_fixture_server_preserves_raw_asis_header_bytes(self) -> None:
- parts = _asis_response_parts(
- b"HTTP/1.1 200 OK\n"
- b"X-Custom-Header-Bytes: \xe2\x80\xa6\n"
- b"\n"
- b"OK"
- )
-
- self.assertIsNotNone(parts)
- _status, _body, headers = parts
- self.assertIn(("X-Custom-Header-Bytes", "\xe2\x80\xa6"), headers)
- for _name, value in headers:
- value.encode("latin-1")
-
def test_fixture_server_preserves_raw_sidecar_header_bytes(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)