An OCaml webserver, but the allocating version (vs httpz which doesnt)
1(* err.ml - Error handling combinators for HTTP parsing *)
2
3(* Re-export status type for convenience *)
4type status = Buf_read.status =
5 | Complete
6 | Partial
7 | Invalid_method
8 | Invalid_target
9 | Invalid_version
10 | Invalid_header
11 | Headers_too_large
12 | Malformed
13 | Content_length_overflow
14 | Ambiguous_framing
15 | Bare_cr_detected
16 | Missing_host_header
17 | Unsupported_transfer_encoding
18
19(* Core exception *)
20exception Parse_error of status
21
22(* Basic fail - raise with specific status *)
23let fail status = raise (Parse_error status)
24
25(* Common status shortcuts *)
26let partial () = raise (Parse_error Partial)
27let malformed () = raise (Parse_error Malformed)
28
29(* Conditional raises - raise if condition is TRUE *)
30let when_ cond status = if cond then raise (Parse_error status)
31let partial_when cond = if cond then raise (Parse_error Partial)
32let malformed_when cond = if cond then raise (Parse_error Malformed)
33
34(* Guard - raise if condition is FALSE (i.e., require condition to be true) *)
35let guard cond status = if not cond then raise (Parse_error status)
36let partial_unless cond = if not cond then raise (Parse_error Partial)
37let malformed_unless cond = if not cond then raise (Parse_error Malformed)
38
39(* Optional: try parser, return None on failure, restore pos via callback *)
40let optional ~(save : unit -> 'pos) ~(restore : 'pos -> unit) (f : unit -> 'a) : 'a option =
41 let saved = save () in
42 match f () with
43 | v -> Some v
44 | exception Parse_error _ -> restore saved; None