httpcore
httpcore copied to clipboard
Add HTTP3 support
This pull request tries to add HTTP/3 support.
As we know, the HTTP/2 and HTTP/3 protocols are very similar, except for the protocol they use. This PR simply follows the steps described below.
- Add the
connect_udp
method to "httpcore._backends.base.NetworkBackend". - Implement
connect_udp
only for the synchronous backend (only for now). - Add http3 extra into
pyproject.toml
- Create
httpcore/_http3.py
file - Implement
HTTP/3
in that file, keeping the logic and flow maximum similar to the logic that we are using in _http2.py.
To support the HTTP/3
protocol, we need the aioquic package, which is a well-tested and well-designed implementation for the HTTP/3
and QUIC
protocols.
For more details, see the issue in HTTPX, where the author of aioquic provides basic HTTP/3 integration for httpx.
There is a very basic example of how you can use HTTP/3 with the httpcore.
from httpcore import Origin
from httpcore import Request
from httpcore import HTTP3Connection
from httpcore import SyncBackend
host = "www.youtube.com"
port = 443
stream = SyncBackend().connect_udp(host=host, port=port)
conn = HTTP3Connection(
origin=Origin(b"https", host.encode(), port), stream=stream
)
request = Request(
method=b"GET",
url=f"https://{host}",
headers=[("host", host)],
extensions={"timeout": {"read": 5, "write": 5}},
)
response = conn.handle_request(request=request)
print(response) # <Response [200]>
print(response.extensions["http_version"]) # b'HTTP/3'
print(response.read()) # ...
Or with the high-level API:
import httpcore
pool = httpcore.ConnectionPool(http1=False, http3=True)
response = pool.request(
"GET", "https://www.youtube.com", extensions={"timeout": {"read": 5, "write": 5}}
)
print(response) # <Response [200]>
print(response.extensions["http_version"]) # b'HTTP/3'
print(response.read()) # ...