| 1 | #!/usr/bin/env python3 |
| 2 | import argparse |
| 3 | from http.client import HTTPConnection |
| 4 | from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer |
| 5 | import json |
| 6 | import os |
| 7 | from pathlib import Path |
| 8 | import socket |
| 9 | import subprocess |
| 10 | import tempfile |
| 11 | import threading |
| 12 | import time |
| 13 | from unittest.mock import patch |
| 14 | |
| 15 | import router |
| 16 | |
| 17 | |
| 18 | class Mirror(BaseHTTPRequestHandler): |
| 19 | requests = [] |
| 20 | |
| 21 | def do_GET(self): |
| 22 | type(self).requests.append((self.command, self.path, dict(self.headers))) |
| 23 | self.send_response(206 if self.headers.get('Range') else 200) |
| 24 | self.send_header('Content-Length', '5') |
| 25 | self.end_headers() |
| 26 | if self.command != 'HEAD': |
| 27 | self.wfile.write(b'hello') |
| 28 | |
| 29 | do_HEAD = do_GET |
| 30 | |
| 31 | def log_message(self, *_): |
| 32 | pass |
| 33 | |
| 34 | |
| 35 | def main(): |
| 36 | parser = argparse.ArgumentParser() |
| 37 | parser.add_argument('--caddy', type=Path, required=True) |
| 38 | args = parser.parse_args() |
| 39 | with tempfile.TemporaryDirectory(prefix='snowglobe-zig-route-') as temporary, ThreadingHTTPServer(('127.0.0.1', 0), Mirror) as backend: |
| 40 | root = Path(temporary) |
| 41 | stages = root / 'stages' |
| 42 | stages.mkdir() |
| 43 | paired = 'copyparty-preview-1234abcd' |
| 44 | mirror = 'zig-mirror-preview-1234abcd' |
| 45 | (stages / (paired + '.json')).write_text(json.dumps({'sourceId': 'copyparty', 'mirrorStage': mirror})) |
| 46 | (stages / (mirror + '.json')).write_text(json.dumps({'sourceId': 'zig-mirror'})) |
| 47 | routes = {(80, 'mirror.localhost'): {'services': {'zig-mirror'}, 'upstreams': {f'127.0.0.1:{backend.server_port}'}}, |
| 48 | (80, 'preview.localhost'): {'services': {mirror}, 'upstreams': {f'127.0.0.1:{backend.server_port}'}}} |
| 49 | with socket.socket() as listener: |
| 50 | listener.bind(('127.0.0.1', 0)) |
| 51 | port = listener.getsockname()[1] |
| 52 | lines = ['{', ' admin off', ' auto_https off', '}'] |
| 53 | with patch.dict(os.environ, {'SNOWGLOBE_STATE': temporary}): |
| 54 | for host, service in [('prod.localhost', 'copyparty'), ('paired.localhost', paired), ('unpaired.localhost', 'copyparty-preview-ffffffff'), ('missing.localhost', 'copyparty')]: |
| 55 | lines += [f'http://{host}:{port} {{', *router.zig_mirror_routes(service, {} if host == 'missing.localhost' else routes), ' handle {', ' respond 204', ' }', '}'] |
| 56 | conf = root / 'Caddyfile' |
| 57 | conf.write_text('\n'.join(lines)) |
| 58 | validated = subprocess.run([str(args.caddy), 'validate', '--config', str(conf), '--adapter', 'caddyfile'], capture_output=True, text=True) |
| 59 | assert validated.returncode == 0, validated.stderr |
| 60 | threading.Thread(target=backend.serve_forever, daemon=True).start() |
| 61 | with (root / 'caddy.log').open('w') as log: |
| 62 | caddy = subprocess.Popen([str(args.caddy), 'run', '--config', str(conf), '--adapter', 'caddyfile'], stdout=log, stderr=log) |
| 63 | try: |
| 64 | def request(host, path, method='GET', **headers): |
| 65 | connection = HTTPConnection('127.0.0.1', port, timeout=3) |
| 66 | connection.request(method, path, headers={'Host': host + ':' + str(port), **headers}) |
| 67 | response = connection.getresponse() |
| 68 | result = response.status, response.read() |
| 69 | connection.close() |
| 70 | return result |
| 71 | deadline = time.monotonic() + 10 |
| 72 | while True: |
| 73 | try: |
| 74 | request('prod.localhost', '/') |
| 75 | break |
| 76 | except OSError: |
| 77 | assert time.monotonic() < deadline and caddy.poll() is None |
| 78 | time.sleep(.05) |
| 79 | archive = '/mirrors/zig/zig-x86_64-linux-0.16.0-dev.123+abc123.tar.xz' |
| 80 | for host in ['prod.localhost', 'paired.localhost']: |
| 81 | assert request(host, archive + '?download=1', Range='bytes=0-4', Cookie='secret=do-not-forward', Authorization='private') == (206, b'hello') |
| 82 | method, path, headers = Mirror.requests[-1] |
| 83 | assert path == archive + '?download=1' |
| 84 | assert 'Cookie' not in headers and 'Authorization' not in headers |
| 85 | assert request(host, archive, method='HEAD') == (200, b'') |
| 86 | for name in ['zig-win64-0.1.1.zip', 'zig-win64-0.2.0.zip', 'zig-future-0.99.0.zip']: |
| 87 | assert request(host, '/mirrors/zig/' + name) == (200, b'hello') |
| 88 | assert request(host, '/mirrors/zig/' + name, method='HEAD') == (200, b'') |
| 89 | for path in ['/mirrors/zig/', '/mirrors/zig/releases/', '/mirrors/zig/dev/', '/mirrors/zig/releases/zig-0.0.0.tar.xz', |
| 90 | '/mirrors/zig/preadme.md', '/mirrors/zig/not-zig.zip']: |
| 91 | assert request(host, path) == (204, b'') |
| 92 | for path in ['/mirrors/zig/.hist/preadme.md', '/mirrors/zig/.mirror-locks/file', '/mirrors/.secret', |
| 93 | '/mirrors/zig/nested/.secret/file', '/mirrors/zig/%2ehist/file', '/mirrors/zig/%2Ehist/file', |
| 94 | '/mirrors/zig%2f.hist/file', '/mirrors/zig%2F.hist/file', '//mirrors///zig//.hist/file', |
| 95 | '/mirrors/zig/normal/../.hist/file', '/before/../mirrors/zig/.hist/file', |
| 96 | '/mirrors/zig/?doc=.secret', '/mirrors/zig/?DOC=%2esecret', |
| 97 | '/mirrors/zig/?doc=readme.md&DOC=.secret', '/mirrors/zig/?DOC=.secret&doc=readme.md', |
| 98 | '/mirrors/zig/?doc=%2Esecret', '/mirrors/zig/?x=1&DoC=.secret']: |
| 99 | count = len(Mirror.requests) |
| 100 | assert request(host, path) == (404, b''), path |
| 101 | assert request(host, path, method='HEAD') == (404, b''), path |
| 102 | assert len(Mirror.requests) == count |
| 103 | for path in ['/media/zig/.hist/preadme.md', '/media/?doc=.secret', '/.static/copyparty.css', |
| 104 | '/mirrors/zig/?download=1', '/mirrors/zig/?x=doc=readme.md', |
| 105 | '/mirrors/zig/?doc=preadme.md', '/mirrors/zig/?doc=regular.zip', |
| 106 | '/mirrors/zig/?DOC=visible.md', '/mirrors/zig/?doc', '/mirrors/zig/?x=1&DoC=visible.md']: |
| 107 | assert request(host, path) == (204, b''), path |
| 108 | assert request(host, archive, method='POST') == (204, b'') |
| 109 | count = len(Mirror.requests) |
| 110 | for host in ['unpaired.localhost', 'missing.localhost']: |
| 111 | assert request(host, archive) == (503, b'') |
| 112 | assert request(host, '/mirrors/zig/.hist/file') == (404, b'') |
| 113 | assert request(host, '/mirrors/zig/?DOC=.secret') == (404, b'') |
| 114 | assert len(Mirror.requests) == count |
| 115 | finally: |
| 116 | caddy.terminate() |
| 117 | caddy.wait(timeout=10) |
| 118 | backend.shutdown() |
| 119 | print('Zig archive routes: production, explicitly paired preview, absent pairing, methods, ranges, query, and nested browse passed') |
| 120 | |
| 121 | |
| 122 | if __name__ == '__main__': |
| 123 | main() |