webtransport_h2 (webtransport v0.2.3)

View Source

HTTP/2 WebTransport runtime wrapper.

WebTransport over HTTP/2 uses a single CONNECT stream to multiplex all WebTransport data through capsules. This module manages the CONNECT stream and handles capsule encoding/decoding.

Key differences from HTTP/3 transport:

  • All streams are multiplexed over one HTTP/2 stream using capsules
  • Flow control is managed via capsules, not native QUIC flow control
  • Datagrams use the DATAGRAM capsule type

See also: wt_h2_capsule.

Summary

Functions

Send a CLOSE_WEBTRANSPORT_SESSION capsule and FIN the CONNECT stream.

Establish an HTTP/2 WebTransport session to the given host, port, and path.

Return the HTTP/2 CONNECT stream ID used for this session.

Decode a single capsule from the binary buffer, returning any remainder.

Decode all complete capsules from the binary buffer, returning the unparsed tail.

Send a DRAIN_WEBTRANSPORT_SESSION capsule to signal graceful shutdown.

Return the HTTP/2 connection pid.

Return true if the response headers contain a 2xx status code.

Create a new H2 transport state for the given CONNECT stream.

No-op: H2 streams are implicitly opened when first used via capsules.

Return the peer's advertised WebTransport settings.

Build CONNECT request headers for an H2 WebTransport session.

Build CONNECT request headers with additional custom headers.

Reset a multiplexed stream by sending a WT_RESET_STREAM capsule.

Send data on a multiplexed stream via a WT_STREAM capsule.

Write a pre-built capsule on the CONNECT stream (e.g. for flow-control signalling).

Send an unreliable datagram via the DATAGRAM capsule type.

Request the peer stop sending on a stream via a WT_STOP_SENDING capsule.

Return a new state with updated peer settings.

Types

state/0

-opaque state()

Functions

close_session(State, ErrorCode, Reason)

-spec close_session(state(), non_neg_integer(), binary()) -> ok | {error, term()}.

Send a CLOSE_WEBTRANSPORT_SESSION capsule and FIN the CONNECT stream.

connect(Host, Port, Path, Opts)

-spec connect(string() | binary(), inet:port_number(), binary(), map()) ->
                 {ok, state()} | {error, term()}.

Establish an HTTP/2 WebTransport session to the given host, port, and path.

connect_stream_id(State)

-spec connect_stream_id(state()) -> non_neg_integer().

Return the HTTP/2 CONNECT stream ID used for this session.

decode_capsule(Bin)

-spec decode_capsule(binary()) ->
                        {ok, wt_h2_capsule:capsule(), binary()} |
                        {more, pos_integer()} |
                        {error, term()}.

Decode a single capsule from the binary buffer, returning any remainder.

decode_capsules(Bin)

-spec decode_capsules(binary()) -> {ok, [wt_h2_capsule:capsule()], binary()} | {error, term()}.

Decode all complete capsules from the binary buffer, returning the unparsed tail.

drain_session(State)

-spec drain_session(state()) -> ok | {error, term()}.

Send a DRAIN_WEBTRANSPORT_SESSION capsule to signal graceful shutdown.

h2_conn(State)

-spec h2_conn(state()) -> pid().

Return the HTTP/2 connection pid.

is_success_response(Headers)

-spec is_success_response([{binary(), binary()}]) -> boolean().

Return true if the response headers contain a 2xx status code.

new(H2Conn, ConnectStreamId)

-spec new(pid(), non_neg_integer()) -> state().

Create a new H2 transport state for the given CONNECT stream.

open_stream(State, StreamId, Type)

-spec open_stream(state(), non_neg_integer(), bidi | uni) -> ok | {error, term()}.

No-op: H2 streams are implicitly opened when first used via capsules.

peer_settings(State)

-spec peer_settings(state()) -> map().

Return the peer's advertised WebTransport settings.

request_headers(Authority, Path)

-spec request_headers(binary(), binary()) -> [{binary(), binary()}].

Build CONNECT request headers for an H2 WebTransport session.

request_headers(Authority, Path, ExtraHeaders)

-spec request_headers(binary(), binary(), [{binary(), binary()}]) -> [{binary(), binary()}].

Build CONNECT request headers with additional custom headers.

reset_stream(State, StreamId, ErrorCode)

-spec reset_stream(state(), non_neg_integer(), non_neg_integer()) -> ok | {error, term()}.

Reset a multiplexed stream by sending a WT_RESET_STREAM capsule.

send(State, StreamId, Data, Fin)

-spec send(state(), non_neg_integer(), iodata(), boolean()) -> ok | {error, term()}.

Send data on a multiplexed stream via a WT_STREAM capsule.

send_capsule(State, Capsule)

-spec send_capsule(state(), wt_h2_capsule:capsule()) -> ok | {error, term()}.

Write a pre-built capsule on the CONNECT stream (e.g. for flow-control signalling).

send_datagram(State, Data)

-spec send_datagram(state(), binary()) -> ok | {error, term()}.

Send an unreliable datagram via the DATAGRAM capsule type.

stop_sending(State, StreamId, ErrorCode)

-spec stop_sending(state(), non_neg_integer(), non_neg_integer()) -> ok | {error, term()}.

Request the peer stop sending on a stream via a WT_STOP_SENDING capsule.

with_peer_settings(State, PeerSettings)

-spec with_peer_settings(state(), map()) -> state().

Return a new state with updated peer settings.