#!/usr/bin/env python3
"""Minimal HTTP server for the ANGLE BufferGL stale-shadow POC.

Serves poc.html on http://127.0.0.1:8080/poc.html and logs anything POSTed
to /result on stderr (so the renderer can report results without --dump-dom).
"""
import http.server
import os
import sys


class Handler(http.server.SimpleHTTPRequestHandler):
    def do_POST(self):  # noqa: N802
        n = int(self.headers.get('Content-Length', 0))
        body = self.rfile.read(n).decode('utf-8', errors='replace')
        sys.stderr.write('=== POC RESULT (renderer) ===\n')
        sys.stderr.write(body + '\n')
        sys.stderr.write('=============================\n')
        sys.stderr.flush()
        self.send_response(200)
        self.end_headers()

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


if __name__ == '__main__':
    os.chdir(os.path.dirname(os.path.abspath(__file__)))
    port = int(sys.argv[1]) if len(sys.argv) > 1 else 8080
    addr = ('127.0.0.1', port)
    http.server.HTTPServer.allow_reuse_address = True
    sys.stderr.write(f'Serving POC on http://{addr[0]}:{addr[1]}/poc.html\n')
    http.server.HTTPServer(addr, Handler).serve_forever()
