Http
12 types36 functionsHTTP requests and responses, methods, headers, cookies and Basic
authentication, as pure values and functions over them. Requests and
responses are records, so getHeader, setHeader, prependHeader and
setBody take either. Header names are lowercase throughout: the functions
that take one lowercase it, and getHeader finds only a lowercase one.
At a glance
Types
Method
A request method. Any other method is Other, with its case kept:
"PROPFIND" is Other("PROPFIND"), and "get" is Other("get").
Scheme
http or https.
Option
Shorthand for [Some(a) | None], a value that may be absent.
Header
A header, and every other name/value pair: a query parameter, a cookie, a header parameter.
Request
A request, with a body of any type. A server hands one over with
Bytes.@Bytes as its body; mapRequest turns that into
what a handler wants to read. Header names are lowercase, and query is the
raw query string, without its ?.
Response
A response, with a body of any type. A server sends one with a
Bytes.@Bytes body; mapResponse turns a text body into
one.
SameSitePolicy
The SameSite attribute of a cookie.
CookieAttributes
What a set-cookie header says beside the cookie itself.
ContentDisposition
A content-disposition header: its disposition, such as attachment, and
its parameters.
HeaderLink
One link of a link header. The URI reference is kept as it is written.
BasicCredentials
The user name and password of HTTP Basic authentication.
Result
Shorthand for [Ok(a) | Err(e)], the variant this module's fallible
functions return.
Functions
parseMethod
A standard method by its uppercase name. Any other valid token is Other,
case kept, so "get" is Other("get").
methodToString
The method as a request line writes it, in uppercase: GET, POST, or an
Other method's own text.
schemeToString
"http" or "https".
schemeFromString
Either scheme, in any case.
parseContentDisposition
A content-disposition header: its type and parameters, both names
lowercased. A parameter value may be quoted, with \ escaping the next
character.
parseLinkHeader
A link header (RFC 8288): each link's URI reference and its parameters,
in order, names lowercased and duplicates kept. A comma inside a quoted
value does not end the link. An extended parameter such as title* is
kept undecoded.
new
A GET of https://localhost with no headers, no query and an empty body,
to update into the request wanted.
newResponse
A response with this status, no headers and an empty body.
redirect
A 303 See Other to uri.
getHeader
The value of the first header named key, in any case.
setHeader
Replace the first header named key, or add one at the end.
prependHeader
Add a header in front, keeping any already named key.
setBody
A body of the same type; mapRequest and mapResponse change its type.
mapRequest
The request with its body passed through transform.
mapResponse
The response with its body passed through transform.
tryMapResponse
mapResponse with a transform that may fail, whose error is the result's.
pathSegments
The path's non-empty segments, dot-segments resolved.
getQuery
The decoded query parameters; no query has none.
setQuery
The parameters as the query, each name and value percent-encoded, +
included, so that getQuery reads back what was set.
setMethod
The request with its method replaced.
setScheme
The request with its scheme replaced.
setHost
The request with its host replaced.
setPort
The request with its port set.
setPath
The request with its path replaced.
getRequestCookies
Every cookie of every cookie header. A malformed pair is skipped.
setRequestCookie
Set a cookie in the cookie header, replacing one of the same name. The
header moves to the front. With more than one cookie header, only the
first is searched for the name.
removeRequestCookie
Drop the cookies named name from the first cookie header. It does not
remove the cookie from the client.
getResponseCookies
Every cookie of every set-cookie header, attributes included as pairs. A
malformed pair is skipped.
setResponseCookie
Adds a set-cookie header that sets the cookie name to value, with these
attributes.
expireResponseCookie
Expire a cookie on the client. The attributes should be the ones it was set with.
parseCookies
The cookies of a cookie header. Pairs are separated by ;, or by , as
the obsolete RFC 2965 allows; a pair with no =, an empty name, or
whitespace inside its name or value is skipped.
setCookieHeader
The value of a set-cookie header. A maxAge of 0 also writes an
Expires in the past, so that a client that predates Max-Age deletes the
cookie too.
cookieDefaults
Attributes for a cookie on the site at scheme: path /, HttpOnly,
SameSite=Lax, and Secure over HTTPS.
getBasicAuth
The credentials of an authorization header with the Basic scheme (RFC
7617): the scheme in any case, one or more spaces, and the standard, padded
base64 of user:password in UTF-8. The user name ends at the first colon,
so the password may hold colons and either may be empty. No such header is
MissingAuthorization; any other scheme, or a token that is not base64 of
UTF-8 with a colon in it, is InvalidAuthorization.
setBasicAuth
Set the authorization header to basicAuthHeader(credentials), replacing
any there is.
basicAuthHeader
The value of an authorization header with the Basic scheme: Basic
and the standard, padded base64 of user:password in UTF-8. A user name
with a colon in it is encoded as it is, and getBasicAuth reads it back
split at that first colon.