package http
Library
Module
Module type
Parameter
Class
Class type
Associative list representing HTTP headers. Order of transmission is preserved, which implies that headers with same name are neither removed or concataned by default (see clean_dup
to do that).
The type for HTTP headers.
val init : unit -> t
init ()
constructs a fresh, empty list of HTTP headers.
val is_empty : t -> bool
is_empty h
tests whether HTTP headers h
are empty or not.
val of_list_rev : (string * string) list -> t
val of_list : (string * string) list -> t
of_list l
construct a fresh headers from the content of l
and in same order. to_list
and of_list
are defined such as to_list (of_list l) = l
is true with case insensitive comparison.
val to_list : t -> (string * string) list
to_list h
converts HTTP headers h
to a list. Order and case is preserved.
Invariant (with case insensitive comparison): to_list (of_list l) = l
val init_with : string -> string -> t
init_with k v
construct a fresh HTTP headers with a single header with name k
and value v
.
add h k v
adds the header name k
and its associated value v
at the front of header list h
.
add_list h l
adds in order all header pairs contained in l
to the header list h
.
Invariant (with case insensitive comparison): to_list (add_list h l) = to_list h @ l
add_multi h k vs
add multiple header pairs with same name h
and values contained in vs
in h
. The new headers are in the same order that in vs
.
Invariant: get_multi (add_multi h k vs) k = (get_multi h k) @ vs
add_opt hopt k v
adds the header (k, v)
to h
if hopt
is Some h
, or constructs a fresh header list containing this single header if hopt
is None
.
add_unless_exists h k v
adds (k, v)
to h
unless the header name k
is already present in the header.
add_opt_unless_exists h k v
adds (k, v)
to h
if hopt
is Some h
unless the header name k
is already present in the headers. If h
is None
then a fresh header list is constructed containing the header (k, v)
.
remove h k
removes every values associated to the header name k
from h
.
replace h k v
replaces the last added value of k
from h
and removed all other occurences of k
if it exists. Otherwise it adds (k, v)
to h
.
Invariant: forall h, k, v. get_multi (replace h k v) = [ v ]
val mem : t -> string -> bool
mem h k
returns true
if the header name k
appears in h
and false
otherwise.
val get : t -> string -> string option
get h k
returns Some v
where v
is the last added value associated with k
in h
if it exists and None
otherwise
val get_multi : t -> string -> string list
get_multi h k
returns a list of all values associated with k
in h
in order they appear in it.
val get_multi_concat : ?list_value_only:bool -> t -> string -> string option
get_multi_concat h k
returns Some v
if there is at least one value associated with k
in h
and None
otherwise. v
is the concatenation of all values paired with k
in h
, separated by a comma and in order they appear in h
.
The optional argument ?list_value_only
is false
by default. If it is true
and there is at least one value associated to k
, the returned value is the concatenated values only if k
is a header that can have multiple values (like transfer-encoding or accept). Otherwise, the returned value is the last value paired with k
in h
.
Invariant: forall h, k not a list-value header. get_multi_concat ~list-value-only:true h k = get h k
update h k f
returns an header list containing the same headers as h
, except for the header name k
. Depending on the value of v
where v
is f (get h k)
, the header pair (k, v)
is added, removed or updated.
- If
v
isNone
, the last occurence ofk
inh
is removed;
- If
v
isSome w
then the last value paired withk
inh
is replaced byw
if it exists. Otherwise, the pair(k, w)
is added;
- If
k
was already associated last inh
to a value that is physically equal tow
,h
is returned unchanged.
update_all h k f
returns an header list containing the same headers as h
, except for the header k
. Depending on the list of values vs
where vs
is f (get_multi h k)
, the values associated to the header k
are added, removed or updated.
- If
vs
is an empty list, every occurences of the headerk
inh
are removed;
- If
vs
is a non-empty list, all values previously associated tok
are removed and all values invs
are added withadd_multi
;
- If
k
was already associated inh
to a list that is equal tovs
,h
is returned unchanged.
val iter : (string -> string -> unit) -> t -> unit
val fold : (string -> string -> 'a -> 'a) -> t -> 'a -> 'a
val to_lines : t -> string list
to_lines h
returns header fields as a list of lines. Beware that each line ends with "\r\n" characters.
val to_frames : t -> string list
to_frames h
returns the same as to_lines
but lines do not end with "\r\n" characters.
val to_string : t -> string
clean_dup h
cleans duplicates in h
following RFC7230§3.2.2; if a duplicated header can not have multiple values, only the last value is kept in place. Otherwise, the values are concatenated and place at the first position the header is encountered in h
.
Already concatenated values (like anhost.com, anotherhost.com
in the example below) are not affected by clean_dup
. For example,
transfer-encoding: gzip host: afirsthost.com connection: keep-alive host: anhost.com, anotherhost.com transfer-encoding: chunked
becomes
transfer-encoding: gzip, chunked connection: keep-alive host: anhost.com, anotherhost.com
Finally, following RFC7230§3.2.2, the header Set-cookie
is treated as an exception and ignored by clean_dup
.
val get_connection_close : t -> bool
val get_transfer_encoding : t -> Transfer.encoding
get_transfer_encoding h
checks the "content-length", "content-range" and "transfer-encoding" headers to infer the transfer encoding. Uses Unknown if nothing is found.
val add_transfer_encoding : t -> Transfer.encoding -> t
val connection : t -> [ `Keep_alive | `Close | `Unknown of string ] option
val get_location : t -> string option
val pp_hum : Format.formatter -> t -> unit
Human-readable output, used by the toplevel printer
module Private : sig ... end