#!/usr/bin/env python3
"""Minimal HTTP server for the sandwich-iframe POC.

Serves files from the current directory on 0.0.0.0:8000. Chrome maps both
victim.com:8000 and evil.com:8000 to this server via --host-resolver-rules.
"""
import http.server
import socketserver
import os
import sys

PORT = 8000


class Handler(http.server.SimpleHTTPRequestHandler):
    def end_headers(self):
        # No X-Frame-Options / CSP so the sandwich embedding is allowed.
        self.send_header("Cache-Control", "no-store")
        super().end_headers()

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


if __name__ == "__main__":
    os.chdir(os.path.dirname(os.path.abspath(__file__)))
    socketserver.TCPServer.allow_reuse_address = True
    with socketserver.TCPServer(("0.0.0.0", PORT), Handler) as httpd:
        print(f"[server] listening on 0.0.0.0:{PORT}", flush=True)
        httpd.serve_forever()
