An OCaml webserver, but the allocating version (vs httpz which doesnt)
at main 71 lines 2.5 kB view raw
1(** ETag parsing and comparison per RFC 7232. *) 2 3(** Entity tag. Record pointing into the parse buffer. 4 The [off] and [len] fields reference the opaque-tag content without quotes. *) 5type t = 6 { weak : bool (** [true] if prefixed with W/ *) 7 ; off : int (** Offset of tag content (after opening quote) *) 8 ; len : int (** Length of tag content (excluding quotes) *) 9 } 10 11(** Parse status. *) 12type status = 13 | Valid (** Successfully parsed *) 14 | Invalid (** Invalid ETag syntax *) 15 16(** Parse a single ETag value from a span. 17 Accepts formats: ["xyzzy"], [W/"xyzzy"], [""] 18 Returns (status, tag) - tag is only valid if status = Valid. *) 19val parse : Base_bigstring.t -> Span.t -> status * t 20 21(** Empty/invalid ETag constant. *) 22val empty : t 23 24(** Parse ETag to string (allocates). Useful for storage/comparison. *) 25val to_string : Base_bigstring.t -> t -> string 26 27(** {2 If-Match / If-None-Match Parsing} *) 28 29(** Result of parsing If-Match or If-None-Match header. *) 30type match_condition = 31 | Any (** "*" - matches any entity *) 32 | Tags (** List of entity tags - use [get_tags] to retrieve *) 33 | Empty (** No valid tags found *) 34 35(** Maximum number of ETags that can be parsed from a header. *) 36val max_tags : int 37 38(** Parse If-Match or If-None-Match header value. 39 Handles "*" and comma-separated lists of entity tags. 40 Tags are stored in the provided array (up to [max_tags]). 41 Returns (condition, count) where count is number of tags if Tags. *) 42val parse_match_header 43 : Base_bigstring.t 44 -> Span.t 45 -> t array 46 -> match_condition * int 47 48(** {2 Comparison Functions} *) 49 50(** Strong comparison per RFC 7232 Section 2.3.2. *) 51val strong_match : Base_bigstring.t -> t -> t -> bool 52 53(** Weak comparison per RFC 7232 Section 2.3.2. *) 54val weak_match : Base_bigstring.t -> t -> t -> bool 55 56(** Check if an etag matches any in array (weak comparison). *) 57val matches_any_weak : Base_bigstring.t -> t -> t array -> count:int -> bool 58 59(** Check if an etag matches any in array (strong comparison). *) 60val matches_any_strong : Base_bigstring.t -> t -> t array -> count:int -> bool 61 62(** {2 Response Writing} *) 63 64(** Write ETag header: [ETag: "tag"\r\n] or [ETag: W/"tag"\r\n]. *) 65val write_etag : Base_bigstring.t -> off:int -> t -> Base_bigstring.t -> int 66 67(** Write ETag header from string value. *) 68val write_etag_string : Base_bigstring.t -> off:int -> weak:bool -> string -> int 69 70(** Pretty-print etag. *) 71val pp : Base_bigstring.t -> Stdlib.Format.formatter -> t -> unit