forked from
anil.recoil.org/monopam-myspace
My aggregated monorepo of OCaml code, automaintained
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.method" ~doc:"HTTP Methods"
7module Log = (val Logs.src_log src : Logs.LOG)
8
9type t = [
10 | `GET
11 | `POST
12 | `PUT
13 | `DELETE
14 | `HEAD
15 | `OPTIONS
16 | `PATCH
17 | `CONNECT
18 | `TRACE
19 | `Other of string
20]
21
22let to_string = function
23 | `GET -> "GET"
24 | `POST -> "POST"
25 | `PUT -> "PUT"
26 | `DELETE -> "DELETE"
27 | `HEAD -> "HEAD"
28 | `OPTIONS -> "OPTIONS"
29 | `PATCH -> "PATCH"
30 | `CONNECT -> "CONNECT"
31 | `TRACE -> "TRACE"
32 | `Other s -> String.uppercase_ascii s
33
34let of_string s =
35 match String.uppercase_ascii s with
36 | "GET" -> `GET
37 | "POST" -> `POST
38 | "PUT" -> `PUT
39 | "DELETE" -> `DELETE
40 | "HEAD" -> `HEAD
41 | "OPTIONS" -> `OPTIONS
42 | "PATCH" -> `PATCH
43 | "CONNECT" -> `CONNECT
44 | "TRACE" -> `TRACE
45 | other -> `Other other
46
47let pp ppf m = Format.fprintf ppf "%s" (to_string m)
48
49let is_safe = function
50 | `GET | `HEAD | `OPTIONS | `TRACE -> true
51 | `POST | `PUT | `DELETE | `PATCH | `CONNECT | `Other _ -> false
52
53let is_idempotent = function
54 | `GET | `HEAD | `PUT | `DELETE | `OPTIONS | `TRACE -> true
55 | `POST | `PATCH | `CONNECT | `Other _ -> false
56
57let has_request_body = function
58 | `POST | `PUT | `PATCH -> true
59 | `GET | `HEAD | `DELETE | `OPTIONS | `CONNECT | `TRACE -> false
60 | `Other _ -> false (* Conservative default for unknown methods *)
61
62let is_cacheable = function
63 | `GET | `HEAD -> true
64 | `POST -> true (* POST can be cacheable with explicit headers *)
65 | `PUT | `DELETE | `PATCH | `OPTIONS | `CONNECT | `TRACE | `Other _ -> false
66
67let equal m1 m2 =
68 match m1, m2 with
69 | `Other s1, `Other s2 -> String.equal (String.uppercase_ascii s1) (String.uppercase_ascii s2)
70 | m1, m2 -> m1 = m2
71
72let compare m1 m2 =
73 match m1, m2 with
74 | `Other s1, `Other s2 -> String.compare (String.uppercase_ascii s1) (String.uppercase_ascii s2)
75 | m1, m2 -> Stdlib.compare m1 m2