A batteries included HTTP/1.1 client in OCaml
at main 50 lines 1.4 kB view raw
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" 7 8module Log = (val Logs.src_log src : Logs.LOG) 9 10type t = { 11 connect : float option; 12 read : float option; 13 total : float option; 14 expect_100_continue : float option; (** Timeout for 100-continue response *) 15} 16 17let none = 18 { connect = None; read = None; total = None; expect_100_continue = None } 19 20let v ?connect ?read ?total ?expect_100_continue () = 21 { connect; read; total; expect_100_continue } 22 23let default = 24 { 25 connect = Some 10.0; 26 read = Some 30.0; 27 total = None; 28 expect_100_continue = Some 1.0; 29 (* 1 second default for 100-continue *) 30 } 31 32let connect t = t.connect 33let read t = t.read 34let total t = t.total 35let expect_100_continue t = t.expect_100_continue 36 37let pp ppf t = 38 let fmt_opt name = Option.map (Fmt.str "%s:%.1fs" name) in 39 let items = 40 List.filter_map Fun.id 41 [ 42 fmt_opt "connect" t.connect; 43 fmt_opt "read" t.read; 44 fmt_opt "total" t.total; 45 fmt_opt "expect" t.expect_100_continue; 46 ] 47 in 48 match items with 49 | [] -> Fmt.pf ppf "no timeouts" 50 | _ -> Fmt.pf ppf "%s" (String.concat ", " items)