A batteries included HTTP/1.1 client in OCaml
1(*---------------------------------------------------------------------------
2 Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
3 SPDX-License-Identifier: ISC
4 ---------------------------------------------------------------------------*)
5
6let src = Logs.Src.create "requests.timeout" ~doc:"HTTP Request Timeouts"
7module Log = (val Logs.src_log src : Logs.LOG)
8
9type t = {
10 connect : float option;
11 read : float option;
12 total : float option;
13 expect_100_continue : float option; (** Timeout for 100-continue response *)
14}
15
16let none = {
17 connect = None;
18 read = None;
19 total = None;
20 expect_100_continue = None;
21}
22
23let create ?connect ?read ?total ?expect_100_continue () = {
24 connect;
25 read;
26 total;
27 expect_100_continue;
28}
29
30let default = {
31 connect = Some 10.0;
32 read = Some 30.0;
33 total = None;
34 expect_100_continue = Some 1.0; (* 1 second default for 100-continue *)
35}
36
37let connect t = t.connect
38let read t = t.read
39let total t = t.total
40let expect_100_continue t = t.expect_100_continue
41
42let pp ppf t =
43 let fmt_opt name = Option.map (Printf.sprintf "%s:%.1fs" name) in
44 let items = List.filter_map Fun.id [
45 fmt_opt "connect" t.connect;
46 fmt_opt "read" t.read;
47 fmt_opt "total" t.total;
48 fmt_opt "expect" t.expect_100_continue;
49 ] in
50 match items with
51 | [] -> Format.fprintf ppf "no timeouts"
52 | _ -> Format.fprintf ppf "%s" (String.concat ", " items)