h2_connection (h2 v0.6.0)
View SourceHTTP/2 Connection State Machine (RFC 7540)
This module implements the HTTP/2 connection protocol using gen_statem. It handles both client and server modes, managing the connection preface, settings exchange, stream lifecycle, flow control, and frame dispatch.
States: - preface: Exchanging connection preface - settings: Exchanging SETTINGS frames - connected: Ready for requests/responses - goaway_sent: Graceful shutdown initiated by us - goaway_received: Peer initiated shutdown - closing: Connection closing
Summary
Functions
Activate the socket after ownership transfer. Must be called after transferring socket ownership to this process. Synchronous so the caller knows the preface + SETTINGS have been sent and the socket has been set to active mode before it proceeds.
Cancel a stream.
Cancel a stream with a specific error code.
Close the connection.
Transfer ownership of the connection.
Get peer settings.
Get local settings.
Send data on a stream.
Send data on a stream with end_stream flag.
Send GOAWAY frame.
Send GOAWAY frame with error code.
Send a request (client mode).
Send a request with EndStream flag (client mode).
Send a request with a pre-built header list (including pseudo-headers).
Send a response (server mode).
Send trailers on a stream.
Register a pid to receive body data for StreamId. By default any DATA frames that arrived before the handler was registered are replayed to the handler pid as {h2, Conn, {data, StreamId, Data, Fin}} messages, and the call returns ok. Pass #{drain_buffer => true} to receive the raw buffer in the reply ({ok, [{Data, Fin}, ...]}) and forward it manually. Matches quic_h3:set_stream_handler/3,4.
Start a connection as a client.
Start a connection with explicit owner.
Wait for the connection to reach connected state.
Wait for the connection to reach connected state with timeout.
Functions
Activate the socket after ownership transfer. Must be called after transferring socket ownership to this process. Synchronous so the caller knows the preface + SETTINGS have been sent and the socket has been set to active mode before it proceeds.
-spec cancel_stream(pid(), non_neg_integer()) -> ok | {error, term()}.
Cancel a stream.
-spec cancel_stream(pid(), non_neg_integer(), atom()) -> ok | {error, term()}.
Cancel a stream with a specific error code.
-spec close(pid()) -> ok.
Close the connection.
Transfer ownership of the connection.
-spec get_peer_settings(pid()) -> h2_settings:settings().
Get peer settings.
-spec get_settings(pid()) -> h2_settings:settings().
Get local settings.
-spec send_data(pid(), non_neg_integer(), binary()) -> ok | {error, term()}.
Send data on a stream.
-spec send_data(pid(), non_neg_integer(), binary(), boolean()) -> ok | {error, term()}.
Send data on a stream with end_stream flag.
Send GOAWAY frame.
Send GOAWAY frame with error code.
-spec send_request(pid(), binary(), binary(), [{binary(), binary()}]) -> {ok, non_neg_integer()} | {error, term()}.
Send a request (client mode).
-spec send_request(pid(), binary(), binary(), [{binary(), binary()}], boolean()) -> {ok, non_neg_integer()} | {error, term()}.
Send a request with EndStream flag (client mode).
-spec send_request_headers(pid(), [{binary(), binary()}], boolean()) -> {ok, non_neg_integer()} | {error, term()}.
Send a request with a pre-built header list (including pseudo-headers).
-spec send_response(pid(), non_neg_integer(), non_neg_integer(), [{binary(), binary()}]) -> ok | {error, term()}.
Send a response (server mode).
-spec send_trailers(pid(), non_neg_integer(), [{binary(), binary()}]) -> ok | {error, term()}.
Send trailers on a stream.
-spec set_stream_handler(pid(), non_neg_integer(), pid()) -> ok | {ok, [{binary(), boolean()}]} | {error, term()}.
Register a pid to receive body data for StreamId. By default any DATA frames that arrived before the handler was registered are replayed to the handler pid as {h2, Conn, {data, StreamId, Data, Fin}} messages, and the call returns ok. Pass #{drain_buffer => true} to receive the raw buffer in the reply ({ok, [{Data, Fin}, ...]}) and forward it manually. Matches quic_h3:set_stream_handler/3,4.
-spec start_link(client, gen_tcp:socket() | ssl:sslsocket(), map()) -> {ok, pid()} | {error, term()}.
Start a connection as a client.
-spec start_link(client | server, gen_tcp:socket() | ssl:sslsocket(), pid(), map()) -> {ok, pid()} | {error, term()}.
Start a connection with explicit owner.
-spec unset_stream_handler(pid(), non_neg_integer()) -> ok.
Wait for the connection to reach connected state.
Wait for the connection to reach connected state with timeout.