Module H2_adapter

HTTP/2 Adapter for Requests Library.

This module provides integration between the H2 implementation and the Requests library, handling automatic response decompression.

NOTE: Connection state caching was removed because it was incompatible with how Conpool manages TCP connections. For proper HTTP/2 multiplexing with connection reuse, the connection management needs to be integrated at the Conpool level.

Usage

  match H2_adapter.request ~flow ~uri ~headers ~method_:`GET
          ~auto_decompress:true () with
  | Ok resp -> Printf.printf "Status: %d\n" resp.status
  | Error msg -> Printf.printf "Error: %s\n" msg

Response Type

type response = {
  1. status : int;
  2. headers : Headers.t;
  3. body : string;
}

HTTP/2 response with Headers.t.

Body Conversion

val body_to_string_opt : Body.t -> string option

body_to_string_opt body converts a request body to a string option. Returns None for empty bodies or streaming bodies that can't be converted.

Response Construction

val make_response : auto_decompress:bool -> H2_protocol.response -> response

make_response ~auto_decompress h2_resp converts an H2_protocol response to an adapter response, applying decompression if enabled.

Request Functions

val request : sw:Eio.Switch.t -> flow:[> Eio.Flow.two_way_ty ] Eio.Resource.t -> uri:Uri.t -> headers:Headers.t -> ?body:Body.t -> method_:Method.t -> auto_decompress:bool -> unit -> (response, string) result

request ~sw ~flow ~uri ~headers ?body ~method_ ~auto_decompress () makes an HTTP/2 request.

This function creates a fresh H2_client for each request and performs a new handshake. For proper HTTP/2 multiplexing with connection reuse, the connection management needs to be integrated at the Conpool level.

  • parameter sw

    Switch for the reader fiber

  • parameter flow

    The underlying TLS connection

  • parameter uri

    Request URI

  • parameter headers

    Request headers

  • parameter body

    Optional request body

  • parameter method_

    HTTP method

  • parameter auto_decompress

    Whether to decompress gzip/deflate response bodies

  • returns

    Response on success, Error msg on failure

val one_request : sw:Eio.Switch.t -> flow:[> Eio.Flow.two_way_ty ] Eio.Resource.t -> uri:Uri.t -> headers:Headers.t -> ?body:Body.t -> method_:Method.t -> auto_decompress:bool -> unit -> (response, string) result

one_request is an alias for request.

Connection Management

val clear_connection_cache : unit -> unit

clear_connection_cache () is a no-op (caching was removed).

val connection_cache_stats : unit -> int * string list

connection_cache_stats () always returns (0, []) (caching was removed).