Files
javademos/http3/server/connect-proxy.py
T

26 lines
890 B
Python

"""A 25-line HTTP CONNECT proxy, just enough to show that the JDK will not send HTTP/3 through one."""
import asyncio, sys
async def pipe(r, w):
try:
while data := await r.read(65536):
w.write(data); await w.drain()
finally:
w.close()
async def handle(cr, cw):
line = (await cr.readline()).decode()
while (await cr.readline()) not in (b"\r\n", b""):
pass
method, target, _ = line.split()
print("proxy saw:", method, target, flush=True)
host, port = target.rsplit(":", 1)
ur, uw = await asyncio.open_connection(host, int(port))
cw.write(b"HTTP/1.1 200 Connection established\r\n\r\n"); await cw.drain()
await asyncio.gather(pipe(cr, uw), pipe(ur, cw))
async def main():
srv = await asyncio.start_server(handle, "127.0.0.1", int(sys.argv[1]))
async with srv: await srv.serve_forever()
asyncio.run(main())