#!/usr/bin/env python3
"""Minimal HTTP server for the histogram-shmem invalid-free POC.

Serves poc.html (and any sibling files) on http://localhost:8080/.
The path component "trigger-histogram-poc" in the request URL is what
the patched renderer keys on to run its forged-shmem payload.
"""
import http.server
import os
import socketserver

PORT = 18080

class Handler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, directory=os.path.dirname(os.path.abspath(__file__)), **kwargs)

    def end_headers(self):
        self.send_header("Cache-Control", "no-store")
        super().end_headers()

    def do_GET(self):
        # Map the trigger URL to poc.html so the renderer loads a real document
        # whose URL still contains the "trigger-histogram-poc" token.
        if self.path.startswith("/trigger-histogram-poc"):
            self.path = "/poc.html"
        return super().do_GET()


if __name__ == "__main__":
    socketserver.TCPServer.allow_reuse_address = True
    with socketserver.TCPServer(("127.0.0.1", PORT), Handler) as httpd:
        print(f"serving on http://127.0.0.1:{PORT}/trigger-histogram-poc")
        httpd.serve_forever()
