#!/usr/bin/env python3
# Serves poc.html / popup.html and an /auth endpoint that always returns
# 401 WWW-Authenticate: Basic so Chrome shows the HTTP-auth sheet
# (a ui::mojom::ModalType::kWindow child of the requesting window on macOS).

import http.server
import socketserver
import os

PORT = 8080

class Handler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        if self.path.startswith('/auth'):
            self.send_response(401)
            self.send_header('WWW-Authenticate', 'Basic realm="poc"')
            self.send_header('Content-Type', 'text/plain')
            self.send_header('Cache-Control', 'no-store')
            self.end_headers()
            self.wfile.write(b'auth required')
            return
        return super().do_GET()

    def log_message(self, fmt, *args):
        pass

if __name__ == '__main__':
    os.chdir(os.path.dirname(os.path.abspath(__file__)))
    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')
        httpd.serve_forever()
