livery_h2 (livery v0.1.0)

View Source

HTTP/2 adapter on top of the h2 library.

Starts an h2 server bound to a Livery middleware stack and handler. For every inbound request the adapter:

  1. Builds a #livery_req{} from the method, path, and headers delivered by h2.
  2. Spawns a livery_req_proc worker under livery_req_sup.
  3. Spawns a small translator process that turns {h2, Conn, _} body and trailer events into the {livery_body, Ref, _} shape the worker reads via livery_body.
  4. Registers the translator as the h2 stream handler.

Response emission goes through livery:emit/3 and lands on the adapter callbacks (send_headers/4, send_data/3, send_trailers/2, reset/2), which call into h2:send_response/4, h2:send_data/3,4, and h2:send_trailers/3. extended_connect is reported as supported in capabilities/1.

Summary

Functions

Accept a WebSocket session on an extended-CONNECT stream.

Hand an extended-CONNECT stream to the webtransport library.

Start a listener with the given options.

Types

listen_opts()

-type listen_opts() ::
          #{port => inet:port_number(),
            transport => tcp | ssl,
            cert => binary() | string(),
            key => binary() | string(),
            cacerts => [binary()],
            acceptors => pos_integer(),
            enable_connect_protocol => boolean(),
            max_body => non_neg_integer() | infinity,
            stack := livery_middleware:stack(),
            handler := livery_middleware:handler()}.

listener()

-type listener() :: h2:server_ref().

stream()

-type stream() :: {h2:connection(), h2:stream_id()}.

Functions

accept_ws/4

-spec accept_ws(stream(), livery_req:req(), module(), term()) -> {ok, pid()} | {error, term()}.

Accept a WebSocket session on an extended-CONNECT stream.

Validates the RFC 8441 handshake, replies 200 (extended CONNECT uses 200, not 101), then hands the stream to the ws library driven by the livery_ws_h2 transport.

accept_wt/4

-spec accept_wt(h2, livery_req:req(), module(), term()) -> {ok, pid()} | {error, term()}.

Hand an extended-CONNECT stream to the webtransport library.

Reconstructs the CONNECT pseudo-headers from the request value (the adapter delivers method/path out of band, but webtransport:accept/4 expects them inline) and calls webtransport:accept/4 with transport => h2.

capabilities(Listener)

-spec capabilities(listener()) -> livery_adapter:capabilities().

peer_info/1

-spec peer_info(stream()) -> livery_adapter:peer_info().

reset/2

-spec reset(stream(), term()) -> ok.

send_data/3

send_headers/4

-spec send_headers(stream(), 100..599, [{binary(), binary()}], livery_adapter:send_opts()) ->
                      livery_adapter:send_result().

send_trailers/2

-spec send_trailers(stream(), [{binary(), binary()}]) -> livery_adapter:send_result().

start(Opts)

-spec start(listen_opts()) -> {ok, listener()} | {error, term()}.

Start a listener with the given options.

Opts must include stack and handler. port defaults to 0 (random port). transport defaults to tcp for h2c; pass ssl plus cert and key to serve over TLS with ALPN-negotiated h2.

start(Name, Opts, StartOpts)

-spec start(atom() | undefined, listen_opts(), map()) -> {ok, listener()} | {error, term()}.

stop(Listener)

-spec stop(listener()) -> ok.