Mojito.Headers (mojito v0.7.9) View Source

Functions for working with HTTP request and response headers, as described in the HTTP 1.1 specification.

Headers are represented in Elixir as a list of {"header_name", "value"} tuples. Multiple entries for the same header name are allowed.

Capitalization of header names is preserved during insertion, however header names are handled case-insensitively during lookup and deletion.

Link to this section Summary

Functions

Returns an HTTP Basic Auth header from the given username and password.

Convert non string values to string where is possible.

Removes all instances of the given header.

Returns the value for the given HTTP request or response header, or nil if not found.

Returns all values for the given HTTP request or response header. Returns an empty list if none found.

Returns an ordered list of the header names from the given headers. Header names are returned in lowercase.

Returns a copy of the given headers where all header names are lowercased and multiple values for the same header have been joined with ",".

Puts the given header value under name, removing any values previously stored under name. The new header is placed at the end of the list.

Link to this section Types

Specs

headers() :: Mojito.headers()

Link to this section Functions

Link to this function

auth_header(username, password)

View Source

Specs

auth_header(String.t(), String.t()) :: Mojito.header()

Returns an HTTP Basic Auth header from the given username and password.

Example:

iex> Mojito.Headers.auth_header("hello", "world")
{"authorization", "Basic aGVsbG86d29ybGQ="}
Link to this function

convert_values_to_string(headers)

View Source

Specs

convert_values_to_string(headers()) :: headers()

Convert non string values to string where is possible.

Example:

iex> Mojito.Headers.convert_values_to_string([{"content-length", 0}])
[{"content-length", "0"}]

Specs

delete(headers(), String.t()) :: headers()

Removes all instances of the given header.

Header names are matched case-insensitively.

Example:

iex> headers = [
...>   {"header1", "foo"},
...>   {"header2", "bar"},
...>   {"Header1", "baz"}
...> ]
iex> Mojito.Headers.delete(headers, "HEADER1")
[{"header2", "bar"}]

Specs

get(headers(), String.t()) :: String.t() | nil

Returns the value for the given HTTP request or response header, or nil if not found.

Header names are matched case-insensitively.

If more than one matching header is found, the values are joined with "," as specified in RFC 2616.

Example:

iex> headers = [
...>   {"header1", "foo"},
...>   {"header2", "bar"},
...>   {"Header1", "baz"}
...> ]
iex> Mojito.Headers.get(headers, "header2")
"bar"
iex> Mojito.Headers.get(headers, "HEADER1")
"foo,baz"
iex> Mojito.Headers.get(headers, "header3")
nil
Link to this function

get_values(headers, name)

View Source

Specs

get_values(headers(), String.t()) :: [String.t()]

Returns all values for the given HTTP request or response header. Returns an empty list if none found.

Header names are matched case-insensitively.

Example:

iex> headers = [
...>   {"header1", "foo"},
...>   {"header2", "bar"},
...>   {"Header1", "baz"}
...> ]
iex> Mojito.Headers.get_values(headers, "header2")
["bar"]
iex> Mojito.Headers.get_values(headers, "HEADER1")
["foo", "baz"]
iex> Mojito.Headers.get_values(headers, "header3")
[]

Specs

keys(headers()) :: [String.t()]

Returns an ordered list of the header names from the given headers. Header names are returned in lowercase.

Example:

iex> headers = [
...>   {"header1", "foo"},
...>   {"header2", "bar"},
...>   {"Header1", "baz"}
...> ]
iex> Mojito.Headers.keys(headers)
["header1", "header2"]

Specs

normalize(headers()) :: headers()

Returns a copy of the given headers where all header names are lowercased and multiple values for the same header have been joined with ",".

Example:

iex> headers = [
...>   {"header1", "foo"},
...>   {"header2", "bar"},
...>   {"Header1", "baz"}
...> ]
iex> Mojito.Headers.normalize(headers)
[{"header1", "foo,baz"}, {"header2", "bar"}]
Link to this function

put(headers, name, value)

View Source

Specs

put(headers(), String.t(), String.t()) :: headers()

Puts the given header value under name, removing any values previously stored under name. The new header is placed at the end of the list.

Header names are matched case-insensitively, but case of name is preserved when adding the header.

Example:

iex> headers = [
...>   {"header1", "foo"},
...>   {"header2", "bar"},
...>   {"Header1", "baz"}
...> ]
iex> Mojito.Headers.put(headers, "HEADER1", "quux")
[{"header2", "bar"}, {"HEADER1", "quux"}]