Something went wrong. Try again.
HTTP types and an I/O-free, zero-copy HTTP/1.1 request parser
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115(*--------------------------------------------------------------------------- Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
let src = Logs.Src.create "requests.method" ~doc:"HTTP Methods"
module Log = (val Logs.src_log src : Logs.LOG)
type t = [ `GET | `POST | `PUT | `DELETE | `HEAD | `OPTIONS | `PATCH | `CONNECT | `TRACE | `Other of string ]
let to_string = function | `GET -> "GET" | `POST -> "POST" | `PUT -> "PUT" | `DELETE -> "DELETE" | `HEAD -> "HEAD" | `OPTIONS -> "OPTIONS" | `PATCH -> "PATCH" | `CONNECT -> "CONNECT" | `TRACE -> "TRACE" | `Other s -> String.uppercase_ascii s
let of_string s = match String.uppercase_ascii s with | "GET" -> `GET | "POST" -> `POST | "PUT" -> `PUT | "DELETE" -> `DELETE | "HEAD" -> `HEAD | "OPTIONS" -> `OPTIONS | "PATCH" -> `PATCH | "CONNECT" -> `CONNECT | "TRACE" -> `TRACE | other -> `Other other
(* [buf.\[start..stop)] equals [m] byte for byte; top-level [let rec] (no closure per call). *)let rec span_eq buf start stop m i = i >= String.length m || start + i < stop && buf.[start + i] = m.[i] && span_eq buf start stop m (i + 1)
let is buf start stop m = String.length m = stop - start && span_eq buf start stop m 0
(* Recognize a method directly from [buf.\[start..stop)] without copying it. The registered methods are uppercase (RFC 9110 9.1), so the common request hits a span comparison and allocates nothing; a lowercase or extension method falls back to {!of_string}, which copies and is lenient about case. *)let of_span buf start stop = if is buf start stop "GET" then `GET else if is buf start stop "POST" then `POST else if is buf start stop "PUT" then `PUT else if is buf start stop "DELETE" then `DELETE else if is buf start stop "HEAD" then `HEAD else if is buf start stop "OPTIONS" then `OPTIONS else if is buf start stop "PATCH" then `PATCH else if is buf start stop "CONNECT" then `CONNECT else if is buf start stop "TRACE" then `TRACE else of_string (String.sub buf start (stop - start))
let pp ppf m = Fmt.pf ppf "%s" (to_string m)
let is_safe = function | `GET | `HEAD | `OPTIONS | `TRACE -> true | `POST | `PUT | `DELETE | `PATCH | `CONNECT | `Other _ -> false
let is_idempotent = function | `GET | `HEAD | `PUT | `DELETE | `OPTIONS | `TRACE -> true | `POST | `PATCH | `CONNECT | `Other _ -> false
type body_semantics = Required | Optional | Forbidden
let request_body_semantics = function | `POST | `PUT | `PATCH -> Required | `DELETE | `OPTIONS -> Optional | `GET -> Optional (* RFC 9110 Section 9.3.1: GET body has no defined semantics *) | `HEAD -> Forbidden (* RFC 9110 Section 9.3.2: identical to GET but no body in response *) | `TRACE -> Forbidden (* RFC 9110 Section 9.3.8: MUST NOT send body *) | `CONNECT -> Forbidden (* RFC 9110 Section 9.3.6: no body in CONNECT request *) | `Other _ -> Optional (* Unknown methods - allow body for flexibility *)
let is_cacheable = function | `GET | `HEAD -> true | `POST -> true (* POST can be cacheable with explicit headers *) | `PUT | `DELETE | `PATCH | `OPTIONS | `CONNECT | `TRACE | `Other _ -> false
let equal m1 m2 = match (m1, m2) with | `Other s1, `Other s2 -> String.equal (String.uppercase_ascii s1) (String.uppercase_ascii s2) | m1, m2 -> m1 = m2
let compare m1 m2 = match (m1, m2) with | `Other s1, `Other s2 -> String.compare (String.uppercase_ascii s1) (String.uppercase_ascii s2) | m1, m2 -> Stdlib.compare m1 m2