Module
whiplash/http2
src/whiplash/http2.pookaHTTP/2, many streams down one connection (RFC 9113).
Two layers, both sans-IO. The frame codec reads and writes the ten frame
types of section 6. The Connection above it keeps everything a server
must agree on with its peer: settings, stream states, the HPACK contexts
and both directions of flow control. Bytes go in with receive, events
come out; responses go in with respond and send_data, and flush
writes whatever the peer's windows allow.
Cleartext HTTP/2 with prior knowledge (h2c) and HTTP/2 over TLS chosen by ALPN look the same from here: both start with the client's preface.
API reference
- functionpreface
- enumErrorCode
- classSetting
- classSettings
- classFrame
- classData
- classHeaders
- classPriority
- classRstStream
- classSettingsFrame
- classPushPromise
- classPing
- classGoaway
- classWindowUpdate
- classContinuation
- classUnknown
- classHttp2Error
- classEvent
- classRequest
- classBody
- classRequestTrailers
- classStreamReset
- classSettingsAcknowledged
- classPeerGoaway
- enumStreamState
- classStream
- classConnection
- functiondefault_settings
- functionserver_settings
- functionerror_code
- functioncode_of
- functionencode_frame
- functiondecode_frame
- functionserver
- functionconnection_window
- functionafter_preface
- functionreceive
- functionrespond
- functionsend_data
- functionflush
- functionflush_parts
- functionpriority
- functionrespond_with_body
- functionrespond_queued
- functionreset
- functiongoaway
- functionis_idle
- functionmanual_flow_control
- functionpending_input
- functionconsume
preface() -> BitArray
fn() -> BitArrayThe connection preface every client sends first (section 3.4).
enum ErrorCode
The error codes of section 7.
ErrorCode.NoError
ErrorCode.ProtocolError
ErrorCode.InternalError
ErrorCode.FlowControlError
ErrorCode.SettingsTimeout
ErrorCode.StreamClosed
ErrorCode.FrameSizeError
ErrorCode.RefusedStream
ErrorCode.Cancel
ErrorCode.CompressionError
ErrorCode.ConnectError
ErrorCode.EnhanceYourCalm
ErrorCode.InadequateSecurity
ErrorCode.Http11Required
class Setting(id: Int, value: Int)
A setting's identifier and value (section 6.5.2, and RFC 8441's SETTINGS_ENABLE_CONNECT_PROTOCOL, 0x8).
id: Int
Intvalue: Int
Int
fn(Int, Int) -> whiplash/http2.Settingclass Settings(
header_table_size: Int,
enable_push: Bool,
max_concurrent_streams: Int,
initial_window_size: Int,
max_frame_size: Int,
max_header_list_size: Int,
enable_connect_protocol: Bool
)
What one end announces in its SETTINGS frame.
header_table_size: Int
Intenable_push: Bool
Boolmax_concurrent_streams: Int
Intinitial_window_size: Int
Intmax_frame_size: Int
Intmax_header_list_size: Int
Intenable_connect_protocol: Bool
Bool
fn(Int, Bool, Int, Int, Int, Int, Bool) -> whiplash/http2.Settingsclass Frame
One frame (section 6).
class Data(stream: Int, end_stream: Bool, data: BitArray) is Frame
stream: Int
Intend_stream: Bool
Booldata: BitArray
BitArray
fn(Int, Bool, BitArray) -> whiplash/http2.Dataclass Headers(
stream: Int,
end_stream: Bool,
end_headers: Bool,
block: BitArray
) is Frame
stream: Int
Intend_stream: Bool
Boolend_headers: Bool
Boolblock: BitArray
BitArray
fn(Int, Bool, Bool, BitArray) -> whiplash/http2.Headersclass Priority(stream: Int, depends_on: Int, exclusive: Bool, weight: Int) is Frame
stream: Int
Intdepends_on: Int
Intexclusive: Bool
Boolweight: Int
Int
fn(Int, Int, Bool, Int) -> whiplash/http2.Priorityclass RstStream(stream: Int, code: ErrorCode) is Frame
stream: Int
Intcode: ErrorCode
whiplash/http2.ErrorCode
fn(Int, whiplash/http2.ErrorCode) -> whiplash/http2.RstStreamclass SettingsFrame(ack: Bool, settings: List(Setting)) is Frame
ack: Bool
Boolsettings: List(Setting)
List(whiplash/http2.Setting)
fn(Bool, List(whiplash/http2.Setting)) -> whiplash/http2.SettingsFrameclass PushPromise(
stream: Int,
promised: Int,
end_headers: Bool,
block: BitArray
) is Frame
stream: Int
Intpromised: Int
Intend_headers: Bool
Boolblock: BitArray
BitArray
fn(Int, Int, Bool, BitArray) -> whiplash/http2.PushPromiseclass Ping(ack: Bool, opaque: BitArray) is Frame
ack: Bool
Boolopaque: BitArray
BitArray
fn(Bool, BitArray) -> whiplash/http2.Pingclass Goaway(last_stream: Int, code: ErrorCode, debug: BitArray) is Frame
last_stream: Int
Intcode: ErrorCode
whiplash/http2.ErrorCodedebug: BitArray
BitArray
fn(Int, whiplash/http2.ErrorCode, BitArray) -> whiplash/http2.Goawayclass WindowUpdate(stream: Int, increment: Int) is Frame
stream: Int
Intincrement: Int
Int
fn(Int, Int) -> whiplash/http2.WindowUpdateclass Continuation(stream: Int, end_headers: Bool, block: BitArray) is Frame
stream: Int
Intend_headers: Bool
Boolblock: BitArray
BitArray
fn(Int, Bool, BitArray) -> whiplash/http2.Continuationclass Unknown(kind: Int, flags: Int, stream: Int, payload: BitArray) is Frame
A frame of a type this end does not know, which it must ignore.
kind: Int
Intflags: Int
Intstream: Int
Intpayload: BitArray
BitArray
fn(Int, Int, Int, BitArray) -> whiplash/http2.Unknownclass Http2Error(code: ErrorCode, message: String)
A connection error: the connection ends with GOAWAY and this code.
code: ErrorCode
whiplash/http2.ErrorCodemessage: String
String
fn(whiplash/http2.ErrorCode, String) -> whiplash/http2.Http2Errorclass Event
What the peer did, as the server sees it.
class Request(stream: Int, fields: List(hpack.Field), end_stream: Bool) is Event
A request's header fields, decoded and checked (section 8.3.1).
stream: Int
Intfields: List(hpack.Field)
List(whiplash/hpack.Field)end_stream: Bool
Bool
fn(Int, List(whiplash/hpack.Field), Bool) -> whiplash/http2.Requestclass Body(stream: Int, data: BitArray, end_stream: Bool) is Event
Part of a request's body.
stream: Int
Intdata: BitArray
BitArrayend_stream: Bool
Bool
fn(Int, BitArray, Bool) -> whiplash/http2.Bodyclass RequestTrailers(stream: Int, fields: List(hpack.Field)) is Event
Trailer fields, ending the request.
stream: Int
Intfields: List(hpack.Field)
List(whiplash/hpack.Field)
fn(Int, List(whiplash/hpack.Field)) -> whiplash/http2.RequestTrailersclass StreamReset(stream: Int, code: ErrorCode) is Event
The peer reset a stream, or this end did because it was malformed.
stream: Int
Intcode: ErrorCode
whiplash/http2.ErrorCode
fn(Int, whiplash/http2.ErrorCode) -> whiplash/http2.StreamResetclass SettingsAcknowledged is Event
The peer acknowledged our SETTINGS. The runtime owns the deadline; the protocol reports the acknowledgement only after the entire frame is valid. https://www.rfc-editor.org/rfc/rfc9113.html#section-6.5.3
class PeerGoaway(last_stream: Int, code: ErrorCode) is Event
The peer is going away.
last_stream: Int
Intcode: ErrorCode
whiplash/http2.ErrorCode
fn(Int, whiplash/http2.ErrorCode) -> whiplash/http2.PeerGoawayenum StreamState
The state of one stream (section 5.1).
StreamState.Idle
StreamState.Open
StreamState.HalfClosedRemote
StreamState.HalfClosedLocal
StreamState.Closed
class Stream(
id: Int,
state: StreamState,
send_window: Int,
receive_window: Int,
pending: BitArray,
pending_end: Bool,
expected: Int,
received: Int
)
One stream: its state, both windows, data waiting for window, and the request's declared and received body lengths (-1 when undeclared).
id: Int
Intstate: StreamState
whiplash/http2.StreamStatesend_window: Int
Intreceive_window: Int
Intpending: BitArray
BitArraypending_end: Bool
Boolexpected: Int
Intreceived: Int
Int
fn(Int, whiplash/http2.StreamState, Int, Int, BitArray, Bool, Int, Int) -> whiplash/http2.Streamclass Connection(
local: Settings,
remote: Settings,
decoder: hpack.Decoder,
encoder: hpack.Encoder,
streams: Dict(Int, Stream),
last_stream: Int,
send_window: Int,
receive_window: Int,
buffer: BitArray,
phase: Int,
headers_in_progress: Option(#(Int, BitArray, Bool)),
closed: List(Int),
queue: List(Int),
going_away: Bool,
manual_credit: Bool,
pending_credit: Dict(Int, Int),
priorities: Dict(Int, #(Int, Bool))
)
One connection, from the server's side. phase is 0 before the client's
preface, 1 until its first SETTINGS, then 2. closed holds the ranges of
client identifiers skipped over, as low/high pairs: every other identifier
up to last_stream was used and is now closed or open.
local: Settings
whiplash/http2.Settingsremote: Settings
whiplash/http2.Settingsdecoder: hpack.Decoder
whiplash/hpack.Decoderencoder: hpack.Encoder
whiplash/hpack.Encoderstreams: Dict(Int, Stream)
$package/pooka_stdlib/pooka/dict.Dict(Int, whiplash/http2.Stream)last_stream: Int
Intsend_window: Int
Intreceive_window: Int
Intbuffer: BitArray
BitArrayphase: Int
Intheaders_in_progress: Option(#(Int, BitArray, Bool))
Option(#(Int, BitArray, Bool))closed: List(Int)
List(Int)queue: List(Int)
List(Int)going_away: Bool
Boolmanual_credit: Bool
Boolpending_credit: Dict(Int, Int)
$package/pooka_stdlib/pooka/dict.Dict(Int, Int)priorities: Dict(Int, #(Int, Bool))
$package/pooka_stdlib/pooka/dict.Dict(Int, #(Int, Bool))
fn(whiplash/http2.Settings, whiplash/http2.Settings, whiplash/hpack.Decoder, whiplash/hpack.Encoder, $package/pooka_stdlib/pooka/dict.Dict(Int, whiplash/http2.Stream), Int, Int, Int, BitArray, Int, Option(#(Int, BitArray, Bool)), List(Int), List(Int), Bool, Bool, $package/pooka_stdlib/pooka/dict.Dict(Int, Int), $package/pooka_stdlib/pooka/dict.Dict(Int, #(Int, Bool))) -> whiplash/http2.Connectiondefault_settings() -> Settings
fn() -> whiplash/http2.SettingsThe defaults of section 6.5.2: 4096, push on, no stream limit, 65535, 16384, no header list limit.
server_settings() -> Settings
fn() -> whiplash/http2.SettingsSettings for a server: no push, 100 concurrent streams, extended CONNECT for WebSockets over HTTP/2.
error_code(code: ErrorCode) -> Int
fn(whiplash/http2.ErrorCode) -> IntAn error code's number on the wire.
code_of(value: Int) -> ErrorCode
fn(Int) -> whiplash/http2.ErrorCodeThe error code for a number. Unknown codes mean INTERNAL_ERROR (section 7).
encode_frame(frame: Frame) -> BitArray
fn(whiplash/http2.Frame) -> BitArrayThe bytes of one frame.
decode_frame(bytes: BitArray, max_frame_size: Int) -> Result(
Option(#(Frame, BitArray)),
Http2Error
)
fn(BitArray, Int) -> Result(Option(#(whiplash/http2.Frame, BitArray)), whiplash/http2.Http2Error)One frame from the front of the bytes, and the bytes after it. Ok(None)
when the frame has not fully arrived. Frames over max_frame_size are a
FRAME_SIZE_ERROR.
server(settings: Settings) -> #(Connection, BitArray)
fn(whiplash/http2.Settings) -> #(whiplash/http2.Connection, BitArray)A server's connection, and the SETTINGS frame to send at once.
connection_window() -> Int
fn() -> IntThe connection-level receive window this end keeps open: 1 MiB, refilled whenever half of it has been used. Each stream still has its own window.
after_preface(connection: Connection) -> Connection
fn(whiplash/http2.Connection) -> whiplash/http2.ConnectionA server's connection after an HTTP/1.1 upgrade or ALPN, when the preface has already been read by someone else.
receive(connection: Connection, bytes: BitArray) -> Result(
#(List(Event), BitArray, Connection),
#(Http2Error, BitArray)
)
fn(whiplash/http2.Connection, BitArray) -> Result(#(List(whiplash/http2.Event), BitArray, whiplash/http2.Connection), #(whiplash/http2.Http2Error, BitArray))Feed bytes from the peer. Returns the events, the bytes to write back (SETTINGS and PING acknowledgements, RST_STREAM, WINDOW_UPDATE), and the connection. A connection error carries the GOAWAY to write before closing.
respond(
connection: Connection,
stream: Int,
status: Int,
headers: List(hpack.Field),
end_stream: Bool
) -> #(BitArray, Connection)
fn(whiplash/http2.Connection, Int, Int, List(whiplash/hpack.Field), Bool) -> #(BitArray, whiplash/http2.Connection)Respond on a stream: a HEADERS frame, and CONTINUATION frames if the block is larger than the peer's frame size.
send_data(
connection: Connection,
stream: Int,
data: BitArray,
end_stream: Bool
) -> Connection
fn(whiplash/http2.Connection, Int, BitArray, Bool) -> whiplash/http2.ConnectionQueue body bytes on a stream. Nothing is written until flush.
flush(connection: Connection) -> #(BitArray, Connection)
fn(whiplash/http2.Connection) -> #(BitArray, whiplash/http2.Connection)Write every queued byte the windows allow, as DATA frames no larger than the peer's frame size, by RFC 9218 priority: lower urgency first; within an urgency, non-incremental streams one after another in identifier order, then incremental streams sharing what is left a frame at a time. Without priorities every stream has urgency 3, non-incremental.
flush_parts(connection: Connection) -> #(List(BitArray), Connection)
fn(whiplash/http2.Connection) -> #(List(BitArray), whiplash/http2.Connection)flush, as parts to write in order (frame headers and slices of the
queued data), so a socket can send them without copying the data again.
priority(connection: Connection, stream: Int) -> #(Int, Bool)
fn(whiplash/http2.Connection, Int) -> #(Int, Bool)The urgency (0 to 7) and incremental flag of a stream.
respond_with_body(
connection: Connection,
stream: Int,
status: Int,
headers: List(hpack.Field),
body: BitArray,
has_body: Bool
) -> #(BitArray, Connection)
fn(whiplash/http2.Connection, Int, Int, List(whiplash/hpack.Field), BitArray, Bool) -> #(BitArray, whiplash/http2.Connection)A whole response at once: HEADERS, then as much of the body as the
windows allow, the rest queued for flush. A HEAD or bodyless response
passes an empty body and has_body False.
respond_queued(
connection: Connection,
stream: Int,
status: Int,
headers: List(hpack.Field),
body: BitArray,
has_body: Bool
) -> #(BitArray, Connection)
fn(whiplash/http2.Connection, Int, Int, List(whiplash/hpack.Field), BitArray, Bool) -> #(BitArray, whiplash/http2.Connection)HEADERS now, and the body queued for the next flush_parts, which sends
it as slices without copying it. The returned bytes must be written
before what that flush returns.
reset(connection: Connection, stream: Int, code: ErrorCode) -> #(
BitArray,
Connection
)
fn(whiplash/http2.Connection, Int, whiplash/http2.ErrorCode) -> #(BitArray, whiplash/http2.Connection)Reset a stream.
goaway(connection: Connection, code: ErrorCode) -> #(BitArray, Connection)
fn(whiplash/http2.Connection, whiplash/http2.ErrorCode) -> #(BitArray, whiplash/http2.Connection)Start a graceful shutdown: GOAWAY with the last stream accepted, so the ones in flight may finish (section 6.8).
is_idle(connection: Connection) -> Bool
fn(whiplash/http2.Connection) -> BoolWhether no stream is open, so a connection that is going away may close.
manual_flow_control(connection: Connection) -> Connection
fn(whiplash/http2.Connection) -> whiplash/http2.ConnectionOpt into application-controlled stream credit before the first receive. Connection credit stays automatic, so one slow stream cannot stop other streams. Consume each delivered DATA payload, or reset its stream.
pending_input(connection: Connection, stream: Int) -> Int
fn(whiplash/http2.Connection, Int) -> Intconsume(connection: Connection, stream: Int, count: Int) -> Result(
#(BitArray, Connection),
String
)
fn(whiplash/http2.Connection, Int, Int) -> Result(#(BitArray, whiplash/http2.Connection), String)Return stream credit only after the application takes ownership of bytes. Padding is consumed automatically; do not count it again. Closed/reset streams need no further stream window, but their accounting is still freed.