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
6(** HTTP 100-Continue configuration
7
8 Configuration for the HTTP 100-Continue protocol, which allows clients
9 to check if the server will accept a request before sending a large body.
10 Per RFC 9110 Section 10.1.1 (Expect) and Section 15.2.1 (100 Continue). *)
11
12type t = {
13 enabled : bool;
14 threshold : int64;
15 timeout : float;
16}
17
18let default = {
19 enabled = true;
20 threshold = 1_048_576L; (* 1MB *)
21 timeout = 1.0; (* 1 second *)
22}
23
24let make ?(enabled = true) ?(threshold = 1_048_576L) ?(timeout = 1.0) () =
25 { enabled; threshold; timeout }
26
27let disabled = { enabled = false; threshold = 0L; timeout = 0.0 }
28
29let enabled t = t.enabled
30let threshold t = t.threshold
31let timeout t = t.timeout
32
33let should_use t body_size =
34 t.enabled && body_size >= t.threshold
35
36let pp fmt t =
37 Format.fprintf fmt "@[<v 2>Expect_continue {@ \
38 enabled: %b@ \
39 threshold: %Ld bytes@ \
40 timeout: %.2fs@ \
41 }@]"
42 t.enabled
43 t.threshold
44 t.timeout
45
46let to_string t = Format.asprintf "%a" pp t