H2_streamHTTP/2 Stream State Machine per RFC 9113 Section 5.1.
This module implements the HTTP/2 stream lifecycle as defined in RFC 9113 Section 5.1.
+--------+
send PP | | recv PP
,--------+ idle +--------.
/ | | \
v +--------+ v
+----------+ | +----------+
| | | send H / | |
,------+ reserved | | recv H | reserved +------.
| | (local) | | | (remote) | |
| +---+------+ v +------+---+ |
| | +--------+ | |
| | recv ES | | send ES | |
| send H | ,-------+ open +-------. | recv H |
| | / | | \ | |
| v v +---+----+ v v |
| +----------+ | +----------+ |
| | half- | | | half- | |
| | closed | | send R / | closed | |
| | (remote) | | recv R | (local) | |
| +----+-----+ | +-----+----+ |
| | | | |
| | send ES / | recv ES / | |
| | send R / v send R / | |
| | recv R +--------+ recv R | |
| send R / `----------->| |<-----------' send R / |
| recv R | closed | recv R |
`----------------------->| |<-----------------------'
+--------+ (* Create a new stream *)
let stream = H2_stream.create 1l in
(* Apply events to transition state *)
match H2_stream.apply_event stream (Send_headers { end_stream = false }) with
| Ok () -> (* stream is now Open *)
| Error (code, msg) -> (* protocol error *)type closed_reason = | FinishedBoth endpoints sent END_STREAM.
*)| ResetByUs of H2_frame.error_codeWe sent RST_STREAM.
*)| ResetByThem of H2_frame.error_codePeer sent RST_STREAM.
*)Why a stream was closed.
type state = | IdleInitial state.
*)| Reserved_localWe sent PUSH_PROMISE.
*)| Reserved_remotePeer sent PUSH_PROMISE.
*)| OpenBoth sides can send frames.
*)| Half_closed_localWe sent END_STREAM.
*)| Half_closed_remotePeer sent END_STREAM.
*)| Closed of closed_reasonTerminal state.
*)Stream state per RFC 9113 Section 5.1.
type event = | Send_headers of {}| Recv_headers of {}| Send_data of {}| Recv_data of {}| Send_push_promise| Recv_push_promise| Send_rst_stream of H2_frame.error_code| Recv_rst_stream of H2_frame.error_code| Send_end_stream| Recv_end_streamEvents that cause state transitions.
Result of applying an event to a state.
val create : ?initial_send_window:int -> ?initial_recv_window:int -> int32 -> tcreate ?initial_send_window ?initial_recv_window id creates a new stream. Stream starts in Idle state.
val id : t -> int32id t returns the stream identifier.
val is_idle : t -> boolis_idle t returns true if stream is in Idle state.
val is_active : t -> boolis_active t returns true if stream is Open or Half-closed.
val is_open : t -> boolis_open t returns true if stream is fully Open.
val is_closed : t -> boolis_closed t returns true if stream is Closed.
val can_send : t -> boolcan_send t returns true if we can send frames on this stream.
val can_recv : t -> boolcan_recv t returns true if we can receive frames on this stream.
val transition : state -> event -> transition_resulttransition state event computes the new state after applying event. Returns Ok new_state or Error (code, msg) if transition is invalid.
val apply_event : t -> event -> (unit, H2_frame.error_code * string) resultapply_event t event applies event to stream t, updating its state. Returns Ok () on success or Error (code, msg) on protocol error.
val reset : t -> H2_frame.error_code -> unitreset t code resets the stream with the given error code.
val send_window : t -> intsend_window t returns bytes available in send window.
val recv_window : t -> intrecv_window t returns bytes available in receive window.
val initial_recv_window : t -> intinitial_recv_window t returns the initial receive window size.
val consume_send_window : t -> int -> intconsume_send_window t bytes consumes up to bytes from send window. Returns number of bytes actually consumed.
val credit_send_window :
t ->
int ->
(unit, H2_frame.error_code * string) resultcredit_send_window t increment adds increment to send window. Returns error on overflow.
val consume_recv_window : t -> int -> unitconsume_recv_window t bytes consumes bytes from receive window.
val credit_recv_window : t -> int -> unitcredit_recv_window t increment adds increment to receive window.
val update_initial_window_size :
t ->
int ->
(unit, H2_frame.error_code * string) resultupdate_initial_window_size t new_size updates the initial window size from a SETTINGS frame. Adjusts current window by the delta.
is_client_initiated id returns true if id is odd (client-initiated).
is_server_initiated id returns true if id is even and non-zero.
val request_headers : t -> H2_hpack.header list optionrequest_headers t returns the request headers if set.
val set_request_headers : t -> H2_hpack.header list -> unitset_request_headers t headers sets the request headers.
val response_headers : t -> H2_hpack.header list optionresponse_headers t returns the response headers if set.
val set_response_headers : t -> H2_hpack.header list -> unitset_response_headers t headers sets the response headers.
val trailers : t -> H2_hpack.header list optiontrailers t returns the trailers if set.
val set_trailers : t -> H2_hpack.header list -> unitset_trailers t headers sets the trailers.
val pp_state : Format.formatter -> state -> unitPretty print stream state.
val pp_closed_reason : Format.formatter -> closed_reason -> unitPretty print closed reason.
val state_to_string : state -> stringConvert state to string.
val pp : Format.formatter -> t -> unitPretty print a stream.