#!/usr/bin/env python3
"""HTTP server that serves the POC with the required Document-Policy header."""
import http.server
import os
import socketserver
import sys

PORT = 8000
ROOT = os.path.dirname(os.path.abspath(__file__))

class Handler(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, directory=ROOT, **kwargs)

    def end_headers(self):
        # Required to enable the JS Self-Profiling API.
        self.send_header('Document-Policy', 'js-profiling')
        self.send_header('Cache-Control', 'no-store')
        http.server.SimpleHTTPRequestHandler.end_headers(self)

    def log_message(self, fmt, *args):
        sys.stderr.write("[server] %s\n" % (fmt % args))

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}/poc.html", flush=True)
        httpd.serve_forever()
