"""A local test site that records every request: method, path, all headers, and time.
robots.txt disallows /private/ for every user agent, and /private/page.html is the page each tool is asked to fetch.
Usage: python3 capture.py <port> <log.jsonl>"""
import json, sys, time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

PORT, LOG = int(sys.argv[1]), sys.argv[2]
ROBOTS = b"User-agent: *\nDisallow: /private/\n"
PAGE = (b"<!doctype html><html><head><title>Test page</title></head><body><h1>Private test page</h1>"
        b"<p>This page is disallowed in robots.txt.</p></body></html>")


class H(BaseHTTPRequestHandler):
    def log_message(self, *a):
        pass

    def do_GET(self):
        with open(LOG, "a") as f:
            f.write(json.dumps({"t": round(time.time(), 3), "method": "GET", "path": self.path,
                                "headers": dict(self.headers.items())}) + "\n")
        if self.path.startswith("/robots.txt"):
            body, ctype = ROBOTS, "text/plain"
        elif self.path.startswith("/favicon"):
            self.send_response(404); self.end_headers(); return
        else:
            body, ctype = PAGE, "text/html; charset=utf-8"
        self.send_response(200)
        self.send_header("Content-Type", ctype)
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)

    do_HEAD = do_GET


ThreadingHTTPServer(("127.0.0.1", PORT), H).serve_forever()
