#!/usr/bin/env python3
"""Serves poc.html and a recognisable payload.pdf for the drag-download POC."""
import http.server
import socketserver

PORT = 8080

# Minimal valid-looking PDF so the bytes Chrome writes to <name>-1.pdf are
# obviously distinguishable from whatever the pre-existing collision file
# contains.
PAYLOAD = (b"%PDF-1.1\n"
           b"%\xe2\xe3\xcf\xd3\n"
           b"% ATTACKER-CONTROLLED BYTES FROM poc server\n"
           b"1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\n"
           b"2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj\n"
           b"3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 100 100]>>endobj\n"
           b"xref\n0 4\n0000000000 65535 f \n0000000017 00000 n \n"
           b"0000000060 00000 n \n0000000110 00000 n \n"
           b"trailer<</Size 4/Root 1 0 R>>\nstartxref\n170\n%%EOF\n")


class Handler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):  # noqa: N802
        if self.path.startswith('/payload.pdf'):
            self.send_response(200)
            self.send_header('Content-Type', 'application/pdf')
            self.send_header('Content-Length', str(len(PAYLOAD)))
            self.end_headers()
            self.wfile.write(PAYLOAD)
            return
        return super().do_GET()


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