HTTP/3 - QUIC¶
Overview¶
Upgrading to HTTP/3 is straightforward. It requires:
- an ASGI server which supports is (e.g. Hypercorn)
- TLS incryption
There is currently very little browser support for this. I was able to demonstrate that it was working with a nightly build of FireFox on Ubuntu 20.04 following these instructions.
Example¶
The following code launches a web server which supports:
- http on port 80
- https on port 443
- https using http/3 on port 4433
A browser that is capable of switching to http/3 will automatically upgrade the https connection.
As this example uses "standard" ports you will need to run the server with sudo.
Note the quic_bind
line.
"""Example server"""
import asyncio
import logging
import os
from bareasgi import Application, HttpResponse, text_writer
import bareutils.response_code as response_code
from hypercorn.asyncio import serve
from hypercorn.config import Config
logging.basicConfig(level=logging.DEBUG)
async def http_request_callback(request):
text = """
<!doctype html>
<html>
<head>
<title>Test</title>
</head>
<body>
<p>This is not a test</p>
</body>
</html>
"""
headers = [
(b'content-type', b'text/html'),
(b'content-length', str(len(text)).encode())
]
return HttpResponse(response_code.OK, headers, text_writer(text))
app = Application()
app.http_router.add({'GET'}, '/{path}', http_request_callback)
config = Config()
config.insecure_bind = [ '0.0.0.0:80' ]
config.bind = [ '0.0.0.0:443' ]
config.quic_bind = [ '0.0.0.0:4433' ]
config.keyfile = os.path.expanduser('~/.keys/server.key')
config.certfile = os.path.expanduser('~/.keys/server.crt')
asyncio.run(serve(app, config))