My agentic slop goes here. Not intended for anyone else!
at main 3.8 kB view raw
1(** HTTP headers management with case-insensitive keys 2 3 This module provides an efficient implementation of HTTP headers with 4 case-insensitive header names as per RFC 7230. Headers can have multiple 5 values for the same key (e.g., multiple Set-Cookie headers). 6 7 {2 Examples} 8 9 {[ 10 let headers = 11 Headers.empty 12 |> Headers.content_type Mime.json 13 |> Headers.bearer "token123" 14 |> Headers.set "X-Custom" "value" 15 ]} 16*) 17 18(** Log source for header operations *) 19val src : Logs.Src.t 20 21type t 22(** Abstract header collection type. Headers are stored with case-insensitive 23 keys and maintain insertion order. *) 24 25(** {1 Creation and Conversion} *) 26 27val empty : t 28(** [empty] creates an empty header collection. *) 29 30val of_list : (string * string) list -> t 31(** [of_list pairs] creates headers from an association list. 32 Later entries override earlier ones for the same key. *) 33 34val to_list : t -> (string * string) list 35(** [to_list headers] converts headers to an association list. 36 The order of headers is preserved. *) 37 38(** {1 Manipulation} *) 39 40val add : string -> string -> t -> t 41(** [add name value headers] adds a header value. Multiple values 42 for the same header name are allowed (e.g., for Set-Cookie). *) 43 44val set : string -> string -> t -> t 45(** [set name value headers] sets a header value, replacing any 46 existing values for that header name. *) 47 48val get : string -> t -> string option 49(** [get name headers] returns the first value for a header name, 50 or [None] if the header doesn't exist. *) 51 52val get_all : string -> t -> string list 53(** [get_all name headers] returns all values for a header name. 54 Returns an empty list if the header doesn't exist. *) 55 56val remove : string -> t -> t 57(** [remove name headers] removes all values for a header name. *) 58 59val mem : string -> t -> bool 60(** [mem name headers] checks if a header name exists. *) 61 62val merge : t -> t -> t 63(** [merge base override] merges two header collections. 64 Headers from [override] replace those in [base]. *) 65 66(** {1 Common Header Builders} 67 68 Convenience functions for setting common HTTP headers. 69*) 70 71val content_type : Mime.t -> t -> t 72(** [content_type mime headers] sets the Content-Type header. *) 73 74val content_length : int64 -> t -> t 75(** [content_length length headers] sets the Content-Length header. *) 76 77val accept : Mime.t -> t -> t 78(** [accept mime headers] sets the Accept header. *) 79 80val authorization : string -> t -> t 81(** [authorization value headers] sets the Authorization header with a raw value. *) 82 83val bearer : string -> t -> t 84(** [bearer token headers] sets the Authorization header with a Bearer token. 85 Example: [bearer "abc123"] sets ["Authorization: Bearer abc123"] *) 86 87val basic : username:string -> password:string -> t -> t 88(** [basic ~username ~password headers] sets the Authorization header with 89 HTTP Basic authentication (base64-encoded username:password). *) 90 91val user_agent : string -> t -> t 92(** [user_agent ua headers] sets the User-Agent header. *) 93 94val host : string -> t -> t 95(** [host hostname headers] sets the Host header. *) 96 97val cookie : string -> string -> t -> t 98(** [cookie name value headers] adds a cookie to the Cookie header. 99 Multiple cookies can be added by calling this function multiple times. *) 100 101val range : start:int64 -> ?end_:int64 -> unit -> t -> t 102(** [range ~start ?end_ () headers] sets the Range header for partial content. 103 Example: [range ~start:0L ~end_:999L ()] requests the first 1000 bytes. *) 104 105(** {1 Aliases} *) 106 107val get_multi : string -> t -> string list 108(** [get_multi] is an alias for {!get_all}. *) 109 110(** Pretty printer for headers *) 111val pp : Format.formatter -> t -> unit 112 113(** Brief pretty printer showing count only *) 114val pp_brief : Format.formatter -> t -> unit