Module H2.Client_connection
type ttype error=[|`Malformed_response of string|`Invalid_response_body_length of Response.t|`Protocol_error of Error_code.t * string|`Exn of exn]type response_handler= Response.t -> [ `read ] Body.t -> unittype error_handler= error -> unit
val create : ?config:Config.t -> ?push_handler:(Request.t -> (response_handler, unit) Stdlib.result) -> error_handler:error_handler -> tcreate ?config ?push_handler ~error_handlercreates a connection that can be used to interact with servers over the HTTP/2 protocol.error_handlerwill be called for connection-level errors. HTTP/2 is multiplexed over a single TCP connection and distinguishes connection-level errors from stream-level errors. See See RFC7540§5.4 for more details.If present,
push_handlerwill be called upon the receipt of PUSH_PROMISE frames with the request promised by the server. This function should returnOk response_handlerif the client wishes to accept the pushed request. In this case,response_handlerwill be called once the server respondes to the pushed request. ReturningError ()will signal to h2 that the client is choosing to reject the request that the server is pushing, and its stream will be closed, as per the following excerpt from the HTTP/2 specification:From RFC7540§6.6: Recipients of PUSH_PROMISE frames can choose to reject promised streams by returning a RST_STREAM referencing the promised stream identifier back to the sender of the PUSH_PROMISE.
val create_h2c : ?config:Config.t -> ?push_handler:(Request.t -> (response_handler, unit) Stdlib.result) -> http_request:Httpaf.Request.t -> error_handler:error_handler -> (response_handler * error_handler) -> (t, string) Stdlib.resultval request : t -> Request.t -> error_handler:error_handler -> response_handler:response_handler -> [ `write ] Body.trequest connection req ~error_handler ~response_handleropens a new HTTP/2 stream withreqand returns a request body that can be written to. Once a response arrives,response_handlerwill be called with its headers and body.error_handlerwill be called for stream-level errors.HTTP/2 is multiplexed over a single TCP connection and distinguishes connection-level errors from stream-level errors. See RFC7540§5.4 for more details.
val ping : t -> ?payload:Bigstringaf.t -> ?off:int -> (unit -> unit) -> unitping connection ?payload ?off fsends an HTTP/2 PING frame and registersfto be called when the server has sent an acknowledgement for it. A custompayload(and offset into that payload) for the PING frame may also be provided. If not, a payload with all bytes set to zero will be used. Note that a PING frame's payload must be 8 octets in length.In HTTP/2, the PING frame is a mechanism for measuring a minimal round-trip time from the sender, as well as determining whether an idle connection is still functional. See RFC7540§5.4 for more details.
val shutdown : t -> unitshutdown connectioninitiates the graceful shutdown ofconnection, and sends an HTTP/2 GOAWAY frame with NO_ERROR on the output channel (See RFC7540§6.8 for more details).
val next_read_operation : t -> [ `Read | `Close ]next_read_operation treturns a value describing the next operation that the caller should conduct on behalf of the connection.
val read : t -> Bigstringaf.t -> off:int -> len:int -> intread t bigstring ~off ~lenreads bytes of input from the provided range ofbigstringand returns the number of bytes consumed by the connection.readshould be called afternext_read_operationreturns a`Readvalue and additional input is available for the connection to consume.
val read_eof : t -> Bigstringaf.t -> off:int -> len:int -> intread t bigstring ~off ~lenreads bytes of input from the provided range ofbigstringand returns the number of bytes consumed by the connection.readshould be called afternext_read_operationreturns a`Readand an EOF has been received from the communication channel. The connection will attempt to consume any buffered input and then shutdown the HTTP parser for the connection.
val next_write_operation : t -> [ `Write of Bigstringaf.t IOVec.t list | `Yield | `Close of int ]next_write_operation treturns a value describing the next operation that the caller should conduct on behalf of the connection.
val report_write_result : t -> [ `Ok of int | `Closed ] -> unitreport_write_result t resultreports the result of the latest write attempt to the connection.report_write_resultshould be called after a call tonext_write_operationthat returns a`Write buffervalue.`Ok nindicates that the caller successfully wrotenbytes of output from the buffer that the caller was provided bynext_write_operation.`Closedindicates that the output destination will no longer accept bytes from the write processor.
val yield_writer : t -> (unit -> unit) -> unityield_writer t continueregisters with the connection to callcontinuewhen writing should resume.yield_writershould be called afternext_write_operationreturns a`Yieldvalue.
val report_exn : t -> exn -> unitreport_exn t exnreports that an errorexnhas been caught and that it has been attributed tot. Calling this function will switchtinto an error state. Depending on the statetis transitioning from, it may call its (connection-level) error handler before terminating the connection.
val is_closed : t -> boolis_closed tistrueif both the read and write processors have been shutdown. When this is the casenext_read_operationwill return`Close _andnext_write_operationwill do the same will return a`Write _until all buffered output has been flushed, at which point it will return`Close.