#!/usr/bin/env python3 """HTTP server for tools/wpt-mse.sh: serves a WPT checkout, with Nacre's testharnessreport.js, the driver page and tests.json from tests/wpt-mse, and saves the driver's POST /results to the given file. Usage: server.py """ import http.server import os import sys WPT, TESTS, RESULTS, PORT = sys.argv[1], sys.argv[2], sys.argv[3], int(sys.argv[4]) HERE = os.path.dirname(os.path.abspath(__file__)) OVERLAY = { "/resources/testharnessreport.js": os.path.join(HERE, "testharnessreport.js"), "/driver.html": os.path.join(HERE, "driver.html"), "/tests.json": TESTS, } class Handler(http.server.SimpleHTTPRequestHandler): def __init__(self, *args, **kwargs): super().__init__(*args, directory=WPT, **kwargs) def translate_path(self, path): clean = path.split("?", 1)[0].split("#", 1)[0] return OVERLAY.get(clean) or super().translate_path(path) def do_POST(self): if self.path != "/results": self.send_error(404) return body = self.rfile.read(int(self.headers.get("Content-Length", 0))) with open(RESULTS, "wb") as f: f.write(body) self.send_response(204) self.end_headers() def log_message(self, *args): pass http.server.ThreadingHTTPServer(("127.0.0.1", PORT), Handler).serve_forever()