nhttp_lib (nhttp_lib v1.0.0)
View SourceCore HTTP types for the nhttp library.
This module is the umbrella for the type vocabulary used across the nhttp HTTP framework. Types defined elsewhere are re-exported here as thin aliases so consumers only ever import from one upstream module.
Core Types
method/0- HTTP request methods (GET, POST, etc.)version/0- HTTP version (http1_0|http1_1|http2|http3)scheme/0- URI / wire scheme (http|https|ws|wss)authority/0- Authority component (host[:port])peer/0- Remote socket addressheader_name/0/header_value/0- Header field name/value (binary)headers/0- HTTP headers as a list of name/value pairsstatus/0- HTTP response status codes (100-599)stream_id/0- Transport-level stream identifier (HTTP/2, HTTP/3, QPACK)fin/0- End-of-stream marker shared by HTTP/2 and HTTP/3role/0- Connection role (client or server)error_code/0- Open peer error code: an RFC-defined atom or the raw integeraction/0- Caller-executed I/O action emitted by H2 / H3 codecserror/0- Unified error shape across H2 and H3event_common/0- Shape-shared events emitted by both H2 and H3 codecsevent_h2/0- Alias fornhttp_h2:event/0event_h3/0- Alias fornhttp_h3:event/0event/0- Cross-protocol union; preferevent_h2/0orevent_h3/0request/0- Canonical HTTP request shape across H1, H2, and H3response/0- Canonical HTTP response shape across H1, H2, and H3
The version field on request/0 and response/0 records the HTTP
version (http1_0 | http1_1 | http2 | http3) and is filled by
every server parser. The scheme field is http or https on the
wire; ws/wss only appear when a client preserved the URI scheme as
parsed from a URL. WebSocket upgrades ride on http/https with
Upgrade: websocket (H1) or :protocol = websocket (H2/H3 Extended
CONNECT, RFC 8441 §4 / RFC 9220 §3).
The connect_protocol field on request/0 carries the RFC 8441 / 9220
:protocol pseudo-header sent on Extended CONNECT requests
(<<"websocket">> for the WS upgrade case). It is independent of the
wire version field above and is only present when the peer sent
:protocol.
The body field is for the convenience case where the whole payload is
already in memory. The streaming atom signals that the body is being
fed chunk-by-chunk through the streaming-body callback (see
nhttp_h1:parse_request_headers/2 and parse_request_body/2).
The trailers field is the symmetric convenience for trailing header
fields (RFC 9110 §6.5) when sent alongside an in-memory body. For
streaming, emit trailers separately after the body. H1 emits trailer
fields between encode_last_chunk/0 and the closing CRLF; H2 / H3
emit them via a final send_headers/4 call with fin.
WebSocket umbrella re-exports
WS frame, message, and option types live in nhttp_ws / nhttp_ws_frame
and are re-exported here as aliases (ws_message/0, ws_frame/0,
close_code/0, ws_send_opts/0, ws_session_opts/0,
ws_runtime_opts/0, ws_close_opts/0).
Summary
Functions
Decode an HTTP method from its wire binary form. Known methods become
the corresponding atom (get, post, ...); unknown methods stay as
the input binary.
Decode a scheme from its binary form. Strict: crashes with
function_clause on unknown values. The closed scheme/0 union is
enforced here, so callers must validate untrusted input first (or wrap
the call in try/catch).
Encode an HTTP method to its wire binary form (get -> <<"GET">>).
Encode a URI / wire scheme to its binary form.
Build the pseudo-header prefix plus regular headers for an HTTP/2 or
HTTP/3 request, ready to feed into nhttp_h2:send_headers/4 or
nhttp_h3:send_headers/4.
Pseudo-header order is :method, :scheme, :authority, :path,
the conventional order; HTTP/2 (RFC 9113 §8.3) and HTTP/3 (RFC 9114
§4.3) require only that pseudo-headers precede regular fields.
The Host header is filtered out of the regular set because its value
already lives in :authority (which is required on request/0).
Other headers are passed through in their original order.
Extended CONNECT (RFC 8441 / 9220) is supported by leaving any
caller-supplied {<<\":protocol\">>, _} entry in the request's
headers list. The helper does not touch entries other than Host.
Types
-type authority() :: binary().
-type close_code() :: nhttp_ws:close_code().
-type error() :: {connection_error, error_code(), binary()} | {stream_error, stream_id(), error_code(), binary()} | {protocol_violation, atom(), binary()}.
-type error_code() :: atom() | non_neg_integer().
-type event_h2() :: nhttp_h2:event().
-type event_h3() :: nhttp_h3:event().
-type fin() :: fin | nofin.
-type header_name() :: binary().
-type header_value() :: binary().
-type headers() :: [{header_name(), header_value()}].
-type method() :: get | head | post | put | delete | connect | options | trace | patch | binary().
-type peer() :: {inet:ip_address(), inet:port_number()}.
-type role() :: client | server.
-type scheme() :: http | https | ws | wss.
-type status() :: 100..599.
-type stream_id() :: non_neg_integer().
-type version() :: http1_0 | http1_1 | http2 | http3.
-type ws_close_opts() :: nhttp_ws:ws_close_opts().
-type ws_frame() :: nhttp_ws:ws_frame().
-type ws_message() :: nhttp_ws:ws_message().
-type ws_runtime_opts() :: nhttp_ws:ws_runtime_opts().
-type ws_send_opts() :: nhttp_ws:ws_send_opts().
-type ws_session_opts() :: nhttp_ws:ws_session_opts().
Functions
Decode an HTTP method from its wire binary form. Known methods become
the corresponding atom (get, post, ...); unknown methods stay as
the input binary.
Decode a scheme from its binary form. Strict: crashes with
function_clause on unknown values. The closed scheme/0 union is
enforced here, so callers must validate untrusted input first (or wrap
the call in try/catch).
Encode an HTTP method to its wire binary form (get -> <<"GET">>).
Encode a URI / wire scheme to its binary form.
Build the pseudo-header prefix plus regular headers for an HTTP/2 or
HTTP/3 request, ready to feed into nhttp_h2:send_headers/4 or
nhttp_h3:send_headers/4.
Pseudo-header order is :method, :scheme, :authority, :path,
the conventional order; HTTP/2 (RFC 9113 §8.3) and HTTP/3 (RFC 9114
§4.3) require only that pseudo-headers precede regular fields.
The Host header is filtered out of the regular set because its value
already lives in :authority (which is required on request/0).
Other headers are passed through in their original order.
Extended CONNECT (RFC 8441 / 9220) is supported by leaving any
caller-supplied {<<\":protocol\">>, _} entry in the request's
headers list. The helper does not touch entries other than Host.