Hcs.H2_client
hcs · API reference
HTTP/2-specific client.
Use only when you need direct control over HTTP/2 multiplexing behavior. Prefer Client for general application code.
HTTP/2 Client implementation on the http sans-I/O codec (Http2.Client_connection).
Multiplexed HTTP/2 client over Eio with connection pooling. The transport (TCP/TLS), DNS, pooling and timeouts live here; framing/HPACK/flow-control are driven by http. The connection is driven with two fibers (reader + writer) over the same read / next_write_operation surface as the server, with an Eio.Stream wake mailbox.
module C = Http2.Client_connectionTypes
type error =
| Connection_failed of string
| Tls_error of string
| Protocol_error of string
| Timeout
| Invalid_response of string
| Response_body_too_large of int64type response = {
status : Status.t;
headers : Http_core.Headers.t;
body : string;
}type request_spec = {
meth : Method.t;
headers : (string * string) list;
body : string;
url : Uri.t;
}type config = {
connect_timeout : float;
read_timeout : float;
buffer_size : int;
max_response_body : int64 option;
default_headers : (string * string) list;
pool : Pool.config;
}val default_config : configval default_default_headers : (string * string) listval with_timeout : float -> config -> configval with_default_header : string -> string -> config -> configval with_default_headers : (string * string) list -> config -> configval with_max_response_body : int64 -> config -> configval with_pool_config : Pool.config -> config -> configInternal connection type
type conn = {
flow : Eio.Flow.two_way_ty Eio.Std.r;
mutable alive : bool;
}type t = {
config : config;
pool : conn Pool.t;
mutex : Eio.Mutex.t;
connect : host:string ->
port:int ->
is_https:bool ->
Eio.Flow.two_way_ty Eio.Std.r;
resolve : string -> bool;
now : unit -> float;
with_timeout : 'a. float -> (unit -> 'a) -> 'a option;
}Internal helpers
val writev :
[> Eio.Flow.sink_ty ] Eio.Flow.sink ->
Http_core.Iovec.t list ->
intWrite the codec's iovecs to the flow; returns the byte count written.
module Read_buffer : sig ... endval close_conn : conn -> unitval drive :
[> `Flow | `R | `W ] Eio.Flow.source ->
C.t ->
done_:bool ref ->
unitDrive one client connection with a reader and writer fiber sharing a wake mailbox (the writer parks on it when there is nothing to send; the reader signals it after each read so flow-control acks / window updates go out). done_ decides when the work is finished so the writer can stop.
val map_error : max_response_body:int64 option -> C.error -> errorval request_parts : Uri.t -> string * bool * string * int * stringval authority : host:string -> port:int -> is_https:bool -> stringval method_has_body : Method.t -> boolMethod may carry a request body (controls the default content-type).
val build_headers :
t ->
has_body:bool ->
headers:(string * string) list ->
Http_core.Headers.tBuild the regular header set (no pseudo-headers, no content-length — the codec adds :method/:scheme/:path/:authority and frames the body).
val do_request :
?body:string ->
?max_response_body:int64 ->
[> `Flow | `R | `W ] Eio.Flow.source ->
meth:Http_core.Method.t ->
target:string ->
scheme:string ->
authority:string ->
headers:Http_core.Headers.t ->
(response, error) resultPerform one HTTP/2 request on a connected flow.
val do_requests :
?max_response_body:int64 ->
[> `Flow | `R | `W ] Eio.Flow.source ->
(Http_core.Method.t * string * string * string * Http_core.Headers.t * string)
array ->
(response, error) result listPerform many HTTP/2 requests concurrently on one connection.
val create_connection :
t ->
host:string ->
port:int ->
is_https:bool ->
(conn, error) resultval acquire_connection :
t ->
host:string ->
port:int ->
is_https:bool ->
(conn, error) resultval release_connection :
t ->
host:string ->
port:int ->
is_https:bool ->
conn ->
keep_alive:bool ->
unitPublic API
val create :
sw:Eio.Switch.t ->
net:[> [> `Generic ] Eio.Net.ty ] Eio.Net.t ->
clock:[> float Eio.Time.clock_ty ] Eio.Time.clock ->
?config:config ->
unit ->
tval close : t -> unitval request :
t ->
Method.t ->
?headers:(string * string) list ->
?body:string ->
Uri.t ->
(response, error) resultPerform an HTTP/2 request with any method using pooled connections.
val same_origin :
('a * 'b * String.t * 'c * 'd) ->
('e * 'b * String.t * 'c * 'f) ->
boolval chunks : int -> 'a list -> 'a list listval request_many :
?max_concurrent:int ->
t ->
request_spec list ->
(response, error) result listPerform multiple HTTP/2 requests concurrently on one connection per chunk.
Convenience methods
val get :
t ->
?headers:(string * string) list ->
Uri.t ->
(response, error) resultval post :
t ->
?headers:(string * string) list ->
Uri.t ->
body:string ->
(response, error) resultval put :
t ->
?headers:(string * string) list ->
Uri.t ->
body:string ->
(response, error) resultval patch :
t ->
?headers:(string * string) list ->
Uri.t ->
body:string ->
(response, error) resultval delete :
t ->
?headers:(string * string) list ->
?body:string ->
Uri.t ->
(response, error) resultval head :
t ->
?headers:(string * string) list ->
Uri.t ->
(response, error) resultval options :
t ->
?headers:(string * string) list ->
Uri.t ->
(response, error) resultStateless API
val get' :
sw:Eio.Switch.t ->
net:[> [> `Generic ] Eio.Net.ty ] Eio.Net.t ->
clock:[> float Eio.Time.clock_ty ] Eio.Time.clock ->
?config:config ->
Uri.t ->
(response, error) resultval post' :
sw:Eio.Switch.t ->
net:[> [> `Generic ] Eio.Net.ty ] Eio.Net.t ->
clock:[> float Eio.Time.clock_ty ] Eio.Time.clock ->
?config:config ->
Uri.t ->
body:string ->
(response, error) result