import http.server
import ssl

class UnityWebGLHandler(http.server.SimpleHTTPRequestHandler):
    def end_headers(self):
        path = self.translate_path(self.path)

        # CORS
        self.send_header('Access-Control-Allow-Origin', '*')

        # Brotli compression headers
        if path.endswith('.br'):
            self.send_header('Content-Encoding', 'br')
        elif path.endswith('.gz'):
            self.send_header('Content-Encoding', 'gzip')

        # Content types
        if path.endswith(('.js.br', '.js.gz')):
            self.send_header('Content-Type', 'application/javascript')
        elif path.endswith(('.wasm.br', '.wasm.gz')):
            self.send_header('Content-Type', 'application/wasm')
        elif path.endswith(('.data.br', '.data.gz')):
            self.send_header('Content-Type', 'application/octet-stream')

        super().end_headers()

    def do_OPTIONS(self):
        self.send_response(204)
        self.send_header('Access-Control-Allow-Origin', '*')
        self.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS')
        self.send_header('Access-Control-Allow-Headers', '*')
        self.end_headers()

port = 8454
server = http.server.HTTPServer(('0.0.0.0', port), UnityWebGLHandler)
print(f"Serving on http://localhost:{port}")
server.serve_forever()