Something went wrong. Try again.
HTTP types and an I/O-free, zero-copy HTTP/1.1 request parser
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970(** Content codings and [Accept-Encoding] negotiation.
A content coding is a reversible transformation of a representation's bytes, named in the [Content-Encoding] response field (RFC 9110 section 8.4) by a token from the IANA HTTP Content Coding Registry. A client states the codings it can decode in the [Accept-Encoding] request field (section 12.5.3), and the server picks one with {!negotiate}. *)
type t = [ `Identity | `Gzip | `Deflate | `Compress | `Br | `Zstd ](** The type for the content codings of the registry in use: [identity] (no coding), [gzip] (RFC 1952), [deflate] (the zlib format of RFC 1950), [compress] (LZW), [br] (Brotli, RFC 7932) and [zstd] (Zstandard, RFC 8878).*)
val of_string : string -> t option(** [of_string s] is the coding named [s], compared case-insensitively. As RFC 9110 sections 8.4.1.1 and 8.4.1.3 ask of a recipient, ["x-compress"] is [`Compress] and ["x-gzip"] is [`Gzip]. It is [None] for any other name. *)
val to_string : t -> string(** [to_string c] is the registered name of [c], in lowercase. *)
val pp : Format.formatter -> t -> unit(** [pp] formats a coding as {!to_string}. *)
val equal : t -> t -> bool(** [equal a b] is [true] iff [a] and [b] are the same coding. *)
(** {1:accept Accept-Encoding} *)
(** The type for the coding an [Accept-Encoding] element names: ["*"] is {!Any}, any coding the field does not name. *)type range = Any | Coding of t
type accept = (range * int) list(** The type for an [Accept-Encoding] field value: its elements in field order, each with its weight in thousandths, from [0] (not acceptable) to [1000]. An element naming a coding outside {!t} is not kept. *)
val accept_of_string : string -> accept option(** [accept_of_string s] is the [Accept-Encoding] field value [s] (RFC 9110 section 12.5.3): a comma-separated list of codings, each optionally weighted with [;q=] and a qvalue (section 12.4.2) of at most three decimals, the [q] and the coding compared case-insensitively. Empty list elements are ignored, so [""] is [Some []]. It is [None] when [s] does not match the grammar: a coding that is not a token, a parameter other than the weight, or a qvalue above [1] or with more than three decimals. *)
val quality : accept -> t -> int(** [quality a c] is the weight [a] gives [c], in thousandths: that of the first element naming [c], else that of the first [*], else [0]. [identity] is the exception, since a representation with no coding is acceptable unless the field excludes it: unnamed and not covered by a [*], its weight is [1], acceptable and less preferred than any coding the field accepts. *)
val negotiate : Headers.t -> t list -> t option(** [negotiate headers offered] is the coding to send a client whose request headers are [headers], from the codings [offered] in the server's order of preference, and [identity]. It is the acceptable one of highest {!quality}; equal weights go to the earlier in [offered], and [identity] comes after them all. It is [None] when the field accepts none of them, not even [identity], which a server answers with 406 or with the representation as it is (RFC 9110 section 12.5.3).
Every [Accept-Encoding] field of [headers] is read, as one list. With none, any coding is acceptable, but a client that states nothing may decode nothing, so the result is [identity]. A field value {!accept_of_string} cannot read is disregarded (RFC 9110 section 12.1), and the result is [identity] too. *)