H2_protocolHTTP Protocol Abstraction Layer.
This module provides a unified interface for making HTTP requests over either HTTP/1.1 or HTTP/2, with automatic protocol selection via TLS ALPN negotiation.
For HTTPS connections, ALPN (Application-Layer Protocol Negotiation) is used during the TLS handshake to agree on the protocol:
(* Configure for automatic protocol selection *)
let mode = H2_protocol.Auto in
let tls_config = H2_protocol.create_tls_config ~mode ~verify_tls:true ~host () in
(* After TLS handshake, detect the protocol *)
let alpn_result = H2_protocol.get_alpn_from_epoch epoch in
let protocol = H2_protocol.detect_protocol ~mode alpn_result in
(* Create a protocol-aware connection *)
let conn = H2_protocol.create_connection ~protocol in
(* Make requests *)
let request = H2_protocol.make_request
~meth:"GET"
~uri:(Huri.of_string "https://example.com/")
()
inSee RFC 9113 Section 3 for protocol identification requirements.
val pp_mode : Format.formatter -> mode -> unitPretty print mode.
val mode_to_string : mode -> stringConvert mode to string.
val alpn_protocols : mode -> string listalpn_protocols mode returns ALPN protocol identifiers for TLS config.
"h2"; "http/1.1""http/1.1""h2"val create_tls_config :
mode:mode ->
verify_tls:bool ->
host:string ->
unit ->
Tls.Config.clientcreate_tls_config ~mode ~verify_tls ~host () creates TLS configuration with appropriate ALPN protocols for the given mode.
val get_alpn_from_epoch : Tls.Core.epoch_data -> string optionget_alpn_from_epoch epoch extracts the negotiated ALPN protocol from TLS epoch data after handshake.
The negotiated HTTP protocol for a connection.
val pp_negotiated : Format.formatter -> negotiated -> unitPretty print negotiated protocol.
val negotiated_to_string : negotiated -> stringConvert negotiated protocol to string.
val negotiated_of_alpn : string -> negotiated optionnegotiated_of_alpn alpn parses ALPN result string.
val default_protocol : unit -> negotiateddefault_protocol () returns the default protocol (HTTP/1.1) when ALPN is not available.
val detect_protocol : mode:mode -> string option -> negotiateddetect_protocol ~mode alpn_result determines the protocol to use.
type request = {meth : string;HTTP method (GET, POST, etc)
*)uri : Uri.t;Request URI
*)headers : (string * string) list;Request headers (name, value) pairs
*)body : string option;Optional request body
*)}HTTP request representation.
val make_request :
meth:string ->
uri:Uri.t ->
?headers:(string * string) list ->
?body:string ->
unit ->
requestmake_request ~meth ~uri ?headers ?body () creates a request.
val make_request_from_strings :
meth:string ->
scheme:string ->
host:string ->
?port:int ->
?path:string ->
?query:(string * string list) list ->
?headers:(string * string) list ->
?body:string ->
unit ->
requestmake_request_from_strings ~meth ~scheme ~host ?port ?path ?query ?headers ?body () creates a request from string components. This avoids Uri module conflicts when calling from libraries that have their own Uri module.
val pp_request : Format.formatter -> request -> unitPretty print request.
type response = {status : int;HTTP status code
*)headers : (string * string) list;Response headers
*)body : string;Response body
*)protocol : negotiated;Protocol used for this response
*)}HTTP response representation.
val pp_response : Format.formatter -> response -> unitPretty print response.
val create_connection : protocol:negotiated -> connectioncreate_connection ~protocol creates a connection for the given protocol. For HTTP/2, this initializes the connection state machine.
val connection_protocol : connection -> negotiatedconnection_protocol conn returns the negotiated protocol.
val get_h2_connection : connection -> H2_connection.t optionget_h2_connection conn returns the HTTP/2 connection state if using HTTP/2, or None if using HTTP/1.1. Useful for accessing HTTP/2-specific features.
val is_http2 : connection -> boolis_http2 conn returns true if using HTTP/2.
val is_http1 : connection -> boolis_http1 conn returns true if using HTTP/1.1.
val request_to_h2_headers : request -> H2_hpack.header listrequest_to_h2_headers request converts a request to HTTP/2 headers including the required pseudo-headers (:method, :scheme, :authority, :path). Regular headers are converted to lowercase as required by HTTP/2.
val h2_headers_to_response :
H2_hpack.header list ->
int * (string * string) listh2_headers_to_response headers extracts status code and regular headers from HTTP/2 response headers. Returns (status_code, header_list).