Skip to content

HTTP/3 API

H3Connection translates between QUIC events and HTTP/3 events. It relies on a QuicConnection for transport and performs no I/O itself.

from qh3 import H3Connection, quic_events

h3 = H3Connection(quic)

while (event := quic.next_event()) is not None:
    for http_event in h3.handle_event(event):
        handle_http_event(http_event)

stream_id = quic.get_next_available_stream_id()
h3.send_headers(
    stream_id,
    [
        (b":method", b"GET"),
        (b":scheme", b"https"),
        (b":authority", b"example.com"),
        (b":path", b"/"),
    ],
    end_stream=True,
)

H3Connection

A low-level HTTP/3 connection object.

Parameters:

Name Type Description Default
quic QuicConnection

A :class:~qh3.connection.QuicConnection instance.

required

received_settings property

received_settings

Return the received SETTINGS frame, or None.

sent_settings property

sent_settings

Return the sent SETTINGS frame, or None.

create_webtransport_stream

create_webtransport_stream(session_id, is_unidirectional=False)

Create a WebTransport stream and return the stream ID.

Parameters:

Name Type Description Default
session_id int

The WebTransport session identifier.

required
is_unidirectional bool

Whether to create a unidirectional stream.

False

handle_event

handle_event(event)

Handle a QUIC event and return a list of HTTP events.

Parameters:

Name Type Description Default
event QuicEvent

The QUIC event to handle.

required

send_datagram

send_datagram(flow_id, data)

Send a datagram for the specified flow.

Parameters:

Name Type Description Default
flow_id int

The flow ID.

required
data bytes

The HTTP/3 datagram payload.

required

send_push_promise

send_push_promise(stream_id, headers)

Send a push promise related to the specified stream.

Returns the stream ID on which headers and data can be sent.

Parameters:

Name Type Description Default
stream_id int

The stream ID on which to send the data.

required
headers Headers

The HTTP request headers for this push.

required

send_data

send_data(stream_id, data, end_stream)

Send data on the given stream.

To retrieve datagram which need to be sent over the network call the QUIC connection's :meth:~qh3.connection.QuicConnection.datagrams_to_send method.

Parameters:

Name Type Description Default
stream_id int

The stream ID on which to send the data.

required
data bytes

The data to send.

required
end_stream bool

Whether to end the stream.

required

send_headers

send_headers(stream_id, headers, end_stream=False)

Send headers on the given stream.

To retrieve datagram which need to be sent over the network call the QUIC connection's :meth:~qh3.connection.QuicConnection.datagrams_to_send method.

Parameters:

Name Type Description Default
stream_id int

The stream ID on which to send the headers.

required
headers Headers

The HTTP headers to send.

required
end_stream bool

Whether to end the stream.

False

Exceptions

H3Error

Bases: Exception

Base class for HTTP/3 exceptions.

NoAvailablePushIDError

Bases: H3Error

There are no available push IDs left.

HTTP/3 parser exceptions and wire-level constants are implementation details. Protocol errors encountered by handle_event() close the QUIC connection and are not raised to the application.