Plug.Conn.Utils
Utilities for working with connection data
Summary
content_type(binary) | Parses the request content type header |
list(binary) | Parses a comma-separated header list |
params(t) | Parses headers parameters |
token(token) | Parses a value as defined in RFC-1341. For convenience, trims whitespace at the end of the token. Returns false is the token is invalid |
Functions
Specs:
- content_type(binary) :: {:ok, type :: binary, subtype :: binary, params} | :error
Parses the request content type header.
Type and subtype are case insensitive while the sensitiveness of params depends on its key and therefore are not handled by this parser.
Examples
iex> content_type "text/plain"
{:ok, "text", "plain", %{}}
iex> content_type "APPLICATION/vnd.ms-data+XML"
{:ok, "application", "vnd.ms-data+xml", %{}}
iex> content_type "x-sample/json; charset=utf-8"
{:ok, "x-sample", "json", %{"charset" => "utf-8"}}
iex> content_type "x-sample/json ; charset=utf-8 ; foo=bar"
{:ok, "x-sample", "json", %{"charset" => "utf-8", "foo" => "bar"}}
iex> content_type "\r\n text/plain;\r\n charset=utf-8\r\n"
{:ok, "text", "plain", %{"charset" => "utf-8"}}
iex> content_type "x y"
:error
iex> content_type "/"
:error
iex> content_type "x/y z"
:error
Specs:
- list(binary) :: [binary]
Parses a comma-separated header list.
Examples
iex> list("foo, bar")
["foo", "bar"]
iex> list("foobar")
["foobar"]
iex> list("")
[]
iex> list("empties, , are,, filtered")
["empties", "are", "filtered"]
Specs:
- params(binary) :: params
Parses headers parameters.
Keys are case insensitive and downcased, invalid key-value pairs are discarded.
Examples
iex> params("foo=bar")
%{"foo" => "bar"}
iex> params(" foo=bar ")
%{"foo" => "bar"}
iex> params("FOO=bar")
%{"foo" => "bar"}
iex> params("foo=BAR ; wat")
%{"foo" => "BAR"}
iex> params("=")
%{}
Specs:
- token(binary) :: binary | false
Parses a value as defined in RFC-1341. For convenience, trims whitespace at the end of the token. Returns false is the token is invalid.
Examples
iex> token("foo")
"foo"
iex> token("foo-bar")
"foo-bar"
iex> token("<foo>")
false
iex> token(~s["<foo>"])
"<foo>"
iex> token(~S["<f\oo>\"<b\ar>"])
"<foo>\"<bar>"
iex> token("foo ")
"foo"
iex> token("foo bar")
false