H2_connectionHTTP/2 Connection Management per RFC 9113.
Manages connection-level state, settings, and stream multiplexing.
(* Create a client connection *)
let conn = H2_connection.create Client in
(* Create a new stream *)
match H2_connection.create_stream conn with
| Ok stream -> (* use stream *)
| Error _ -> (* handle error *)
(* Encode headers for sending *)
let header_block = H2_connection.encode_headers conn headers in
(* Decode received headers *)
match H2_connection.decode_headers conn header_block with
| Ok headers -> (* process headers *)
| Error _ -> (* handle error *)HTTP/2 connection preface sent by client: "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
val is_connection_preface : Cstruct.t -> boolis_connection_preface buf returns true if buf starts with the HTTP/2 connection preface.
type settings = {header_table_size : int;SETTINGS_HEADER_TABLE_SIZE (default 4096).
*)enable_push : bool;SETTINGS_ENABLE_PUSH (default true for server, false for client).
*)max_concurrent_streams : int option;SETTINGS_MAX_CONCURRENT_STREAMS (no limit by default).
*)initial_window_size : int;SETTINGS_INITIAL_WINDOW_SIZE (default 65535).
*)max_frame_size : int;SETTINGS_MAX_FRAME_SIZE (default 16384, max 16777215).
*)max_header_list_size : int option;SETTINGS_MAX_HEADER_LIST_SIZE (no limit by default).
*)no_rfc7540_priorities : bool;SETTINGS_NO_RFC7540_PRIORITIES (RFC 9113 Section 5.3.2, default false).
*)}Connection settings per RFC 9113 Section 6.5.2.
val default_settings : settingsDefault settings per RFC 9113.
val default_client_settings : settingsDefault settings for client connections (enable_push = false).
val apply_settings :
settings ->
(int32 * int32) list ->
(settings, H2_frame.error_code * string) resultapply_settings settings pairs applies settings changes. Returns updated settings or error.
val settings_to_pairs : settings -> (int * int32) listsettings_to_pairs settings converts to (id, value) pairs for encoding.
val next_stream_id : role -> int32 -> int32next_stream_id role current returns the next stream ID for this role.
val valid_stream_id_for_role : role -> int32 -> boolvalid_stream_id_for_role role id checks if id can be initiated by role.
type state = | HandshakingWaiting for settings exchange.
*)| OpenConnection is active.
*)| Closing of {last_stream_id : int32;error_code : H2_frame.error_code;debug : string;}Sent or received GOAWAY.
*)| ClosedConnection is closed.
*)Connection state.
type connection_error = {error_code : H2_frame.error_code;debug_data : string;stream_id : int32 option;}Connection error.
create ?settings role creates a new connection. Default settings are used if not specified.
val is_open : t -> boolis_open t returns true if connection is open.
val is_closing : t -> boolis_closing t returns true if connection is closing or closed.
val active_stream_count : t -> intactive_stream_count t returns number of active streams.
val send_window : t -> intsend_window t returns connection-level send window.
val recv_window : t -> intrecv_window t returns connection-level receive window.
val max_send_window : t -> intmax_send_window t returns max send window from peer's settings.
val get_stream : t -> int32 -> H2_stream.t optionget_stream t id returns the stream with id, or None.
val create_stream : t -> (H2_stream.t, H2_frame.error_code * string) resultcreate_stream t creates a new stream initiated by us. Returns error if connection is closing.
val register_peer_stream :
t ->
int32 ->
(H2_stream.t, H2_frame.error_code * string) resultregister_peer_stream t id registers a stream initiated by peer.
val remove_stream : t -> int32 -> unitremove_stream t id removes a closed stream.
val exceeds_max_concurrent_streams : t -> boolexceeds_max_concurrent_streams t checks if we've hit the limit.
val iter_streams : t -> (H2_stream.t -> unit) -> unititer_streams t f calls f on each active stream.
val consume_send_window : t -> int -> intconsume_send_window t bytes consumes from connection send window. Returns bytes actually consumed.
val credit_send_window :
t ->
int ->
(unit, H2_frame.error_code * string) resultcredit_send_window t increment credits send window.
val consume_recv_window : t -> int -> unitconsume_recv_window t bytes consumes from receive window.
val credit_recv_window : t -> int -> unitcredit_recv_window t increment credits receive window.
val handle_settings :
t ->
ack:bool ->
(int32 * int32) list ->
([ `Ack_received | `Settings_received ], H2_frame.error_code * string) resulthandle_settings t ~ack pairs processes a received SETTINGS frame.
val mark_settings_sent : t -> unitmark_settings_sent t marks that we've sent SETTINGS.
val encode_headers : t -> H2_hpack.header list -> Cstruct.tencode_headers t headers encodes headers using connection HPACK context.
val decode_headers :
t ->
Cstruct.t ->
(H2_hpack.header list, H2_hpack.error) resultdecode_headers t block decodes header block.
val go_away : t -> H2_frame.error_code -> string -> unitgo_away t code debug initiates graceful shutdown.
val handle_goaway :
t ->
last_stream_id:int32 ->
error_code:H2_frame.error_code ->
debug:string ->
unithandle_goaway t ~last_stream_id ~error_code ~debug handles received GOAWAY.
val close : t -> unitclose t closes the connection.
val mark_preface_sent : t -> unitmark_preface_sent t marks preface as sent.
val mark_preface_received : t -> unitmark_preface_received t marks preface as received.
val handshake_complete : t -> boolhandshake_complete t returns true if handshake is done.
val pp_role : Format.formatter -> role -> unitPretty print role.
val pp_state : Format.formatter -> state -> unitPretty print state.
val pp : Format.formatter -> t -> unitPretty print connection.