Net.HTTP.Headers
Header fields, typed.
let hs = field(ContentType, MediaType.json) <> field(Accept, MediaType.json)
Headers.get(hs, ContentType) -- Some(application/json)
Headers.get(hs, HeaderName.literal("content-type"))
-- Some(application/json), as text
A header is a type. Each field the library knows is a type with an
instance of Header, which gives the field's name, the type of its value,
and how that value is read from and written as text: ContentType holds a
MediaType and Authorization holds Credentials. A HeaderName is a
header too, whose value is the HeaderValue text, for any field the
library has no type for.
The reserved fields have no type and no name. Content-Length,
Transfer-Encoding, Host and the rest frame and route a message, and the
library writes them from the request or response itself. There is no
instance of Header for them, and HeaderName.parse refuses their names,
so Headers cannot hold one.
Order and multiplicity are kept. Headers is the fields in the order
they were added or arrived, each once per line, and nothing is ever joined:
two Set-Cookie fields stay two, as RFC 6265 requires, since an Expires
date has a comma in it. A field written as a comma-separated list is split
into its elements when it is read, where its header declares it one.
Values are stored as text. Each field is held as its name and the
HeaderValue its value is written as, and a typed value is read from that
text when it is asked for. So a field that arrived off the wire and one a
program added are the same thing, and get with a HeaderName sees typed
fields as the text they are sent as. A typed value is written when it is
added, so the text it would be sent as is checked then.
Headers is a monoid. field(h, v) is headers of one field, and
<> (combine) joins two sets of fields by each field's multiplicity: a
Single field on the right replaces the left's, and any other is added
after them. So a set of fields is a value that can be named, shared and
joined to a message's with withHeaders, and joining a default content
type and a program's own leaves one. Replacing any field explicitly is
set.
Headers is immutable: add, set and remove return new headers. A
message's headers are a few dozen fields, so each copy is small, and a
request built with |> cannot change a request shared with anyone else.
A header field the library knows the meaning of.
readValue returns None for text that is not a value of the field. An
instance's writeValue must return text that readValue reads back as
the same value, which the library relies on: a field is stored as the text
it is written as.
How many values a field has, and how they are spread across lines.
Single: one value, on one line. A second line is a duplicate.CommaSeparated: a list (RFC 9110 §5.6.1). Each line holds one or more elements separated by commas, and any number of lines are one list.writeValuewrites one element.OnePerLine: each line is one value, never split at commas. That is Set-Cookie, and any field read by itsHeaderName, whose grammar the library does not know.
A message with headers. setHeaders(m, hs) returns m with hs in place
of its headers.
Returns m with hs's fields after its own, as combine joins them: a
Single field replaces the message's, and any other is added after its
fields.
withHeaders(request(GET, url), field(Accept, MediaType.json))
withHeaders(request(GET, url), field(Accept, MediaType.json) <> field(h, v))
Returns headers of the one field h: v.
Returns headers holding fields in order, each appended by combine. A
codec that has read a message's fields builds its headers with this.
Returns every field, in order, as it is sent.
Returns the number of lines.
Returns hs with the field h: v after its other fields.
Returns hs with every h field replaced by the one field h: v, where
the first of them was, or after the other fields if there was none.
Returns hs without its h fields.
Returns the first value of h, or None if there is none or it does not
read. Where a second value would be an error, one reports it.
Returns every value of h that reads, in order: each line's, or for a
comma-separated field each element of each line. One that does not read
is left out.
Returns the value of h, or None if there is none. More than one value
is an error, DuplicateField, as is one that does not read,
UnreadableValue: where a field must be single, two readers that each
took a different one of two values would disagree about the message.
Accept (RFC 9110 §12.5.1): the media types a response may have, each a
range such as text/* with perhaps a q weight.
Content-Type (RFC 9110 §8.3): what the body is.
Authorization (RFC 9110 §11.6.2). Its value is written marked sensitive,
so it does not show.
Idempotency-Key: a key that makes a request safe to send again, since a
server that has seen the key once does not act on it twice. Its value is a
token, written as a structured-field string, "8e03978e".
Location (RFC 9110 §10.2.2): where a redirect points, or what a request
created. It may be relative, and is then read against the request's URL.
Retry-After (RFC 9110 §10.2.3): how long to wait before trying again.
A wait, as a length or as the time it ends. A negative or fractional length is written as whole seconds rounded towards zero and at least 0: the field has no other spelling, and a wait already over is a wait of 0.
Set-Cookie (RFC 6265 §4.1): one cookie per field, the fields never
joined.
One field per line, name: value, a sensitive value hidden.
Returns a's fields, then b's, leaving out a's fields of each name
for which b holds a Replaces field. So a Single field on the right
wins, and every other field is appended, Set-Cookie among them, never
joined into one line. Whether a's fields of a name stay depends only on
what b holds of that name, so the join is associative, field order
included, and empty is its identity on either side.
Headers read off the wire are never built with combine. Two
Content-Type fields from a peer are an ambiguity, which one refuses,
not a value to settle by keeping the last.