Plug.Conn
The Plug connection.
This module defines a struct and the main functions for working with Plug connections.
All the struct fields are defined below. Note both request and response headers are expected to have lower-case keys.
Request fields
Those fields contain request information:
host
- the requested host as a binary, example:"www.example.com"
method
- the request method as a binary, example:"GET"
path_info
- the path split into segments, example:["hello", "world"]
port
- the requested port as an integer, example:80
peer
- the actual TCP peer that connected, example:{{127, 0, 0, 1}, 12345}
. Often this is not the actual IP and port of the client, but rather of a load-balancer or request-router.remote_ip
- the IP of the client, example:{151, 236, 219, 228}
. This field is meant to be overwritten by plugs that understand e.g. theX-Forwarded-For
header or HAProxy’s PROXY protocol. It defaults to peer’s IP.req_headers
- the request headers as a list, example:[{"content-type", "text/plain"}]
scheme
- the request scheme as an atom, example::http
query_string
- the request query string as a binary, example:"foo=bar"
Fetchable fields
Those fields contain request information and they need to be explicitly fetched.
Before fetching those fields return a Plug.Conn.Unfetched
record.
cookies
- the request cookies with the response cookiesparams
- the request paramsreq_cookies
- the request cookies (without the response ones)
Response fields
Those fields contain response information:
resp_body
- the response body, by default is an empty string. It is set to nil after the response is set, except for test connections.resp_charset
- the response charset, defaults to “utf-8”resp_cookies
- the response cookies with their name and optionsresp_headers
- the response headers as a dict, by defaultcache-control
is set to"max-age=0, private, must-revalidate"
status
- the response status
Furthermore, the before_send
field stores callbacks that are invoked
before the connection is sent. Callbacks are invoked in the reverse order
they are registered (callbacks registered first are invoked last) in order
to mimic a Plug stack behaviour.
Connection fields
assigns
- shared user data as a dictstate
- the connection statehalted
- the boolean status on whether the stack was haltedsecret_key_base
- a secret key used to verify and encrypt cookies. the field must be set manually whenever one of those features are used. This data must be kept in the connection and never used directly, always usePlug.Crypto.KeyGenerator.generate/3
to derive keys from it.
The connection state is used to track the connection lifecycle. It starts
as :unset
but is changed to :set
(via Plug.Conn.resp/3
) or :file
(when invoked via Plug.Conn.send_file/3
). Its final result is
:sent
or :chunked
depending on the response model.
Private fields
Those fields are reserved for libraries/framework usage.
adapter
- holds the adapter information in a tupleprivate
- shared library data as a dict
Summary
assign(conn, key, value) | Assigns a new key and value in the connection |
chunk(conn, chunk) | Sends a chunk as part of a chunked response |
configure_session(conn, opts) | Configures session |
delete_resp_cookie(conn, key, opts \\ []) | Deletes a response cookie |
delete_resp_header(conn, key) | Deletes a response header |
delete_session(conn, key) | Deletes session for given key |
fetch_cookies(conn, opts \\ []) | Fetches cookies from the request headers |
fetch_params(conn, opts \\ []) | Fetches parameters from the query string |
fetch_session(conn, opts \\ []) | Fetches session from session store. Will also fetch cookies |
get_req_header(conn, key) | Gets a request header |
get_resp_header(conn, key) | Gets a response header |
get_session(conn, key) | Returns session value for given key |
halt(conn) | Halts the Plug stack by preventing further plugs downstream from being invoked |
put_private(conn, key, value) | Assigns a new private key and value in the connection |
put_resp_content_type(conn, content_type, charset \\ "utf-8") | Puts the content-type response header taking into account the charset |
put_resp_cookie(conn, key, value, opts \\ []) | Puts a response cookie |
put_resp_header(conn, key, value) | Puts a new response header |
put_session(conn, key, value) | Puts specified value in session for given key |
put_status(conn, status) | Stores the given status code in the connection |
read_body(conn, opts \\ []) | Reads the request body |
register_before_send(conn, callback) | Registers a callback to be invoked before the response is sent |
resp(conn, status, body) | Sets the response to given status and body |
send_chunked(conn, status) | Sends the response headers as a chunked response |
send_file(conn, status, file, offset \\ 0, length \\ :all) | Sends a file as the response body with the given |
send_resp(conn) | Sends a response to the client |
send_resp(conn, status, body) | Sends a response with given status and body |
Types ↑
adapter :: {module, term}
assigns :: %{atom => any}
before_send :: [(t -> t)]
body :: iodata | nil
cookies :: %{binary => binary} | Plug.Conn.Unfetched.t
halted :: boolean
headers :: [{binary, binary}]
host :: binary
int_status :: non_neg_integer | nil
method :: binary
peer :: {:inet.ip_address, :inet.port_number}
resp_cookies :: %{binary => %{}}
scheme :: :http | :https
secret_key_base :: binary | nil
segments :: [binary]
state :: :unset | :set | :file | :chunked | :sent
status :: atom | int_status
t :: %Plug.Conn{adapter: adapter, assigns: assigns, before_send: before_send, cookies: cookies, host: host, method: method, params: params | Plug.Conn.Unfetched.t, path_info: segments, port: 0 .. 65335, private: assigns, query_string: query_string, peer: peer, remote_ip: :inet.ip_address, req_cookies: cookies, req_headers: headers, resp_body: body, resp_cookies: resp_cookies, resp_headers: headers, scheme: scheme, script_name: segments, secret_key_base: secret_key_base, state: state, status: int_status, halted: term}
Functions
Specs:
Assigns a new key and value in the connection.
Examples
iex> conn.assigns[:hello]
nil
iex> conn = assign(conn, :hello, :world)
iex> conn.assigns[:hello]
:world
Specs:
Sends a chunk as part of a chunked response.
It expects a connection with state :chunked
as set by
send_chunked/2
, returns {:ok, conn}
in case of success,
otherwise {:error, reason}
.
Specs:
Configures session.
Options
:renew
- generates a new session id for the cookie;:drop
- drops the session, a session cookie will not be included in the response;
Specs:
Deletes a response cookie.
Deleting a cookie requires the same options as to when the cookie was put.
Check put_resp_cookie/4
for more information.
Specs:
Deletes a response header.
Specs:
Deletes session for given key.
Specs:
Fetches cookies from the request headers.
Specs:
Fetches parameters from the query string.
This function does not fetch parameters from the body. To fetch
parameters from the body, use the Plug.Parsers
plug.
Specs:
Fetches session from session store. Will also fetch cookies.
Specs:
Halts the Plug stack by preventing further plugs downstream from being invoked
Specs:
Assigns a new private key and value in the connection.
This storage is meant to be used by libraries and frameworks to avoid writing to the user storage (assigns). It is recommended for libraries/frameworks to prefix the keys by the library name.
For example, if some plug needs to store a :hello
key, it
should do so as :plug_hello
:
iex> conn.private[:plug_hello]
nil
iex> conn = put_private(conn, :plug_hello, :world)
iex> conn.private[:plug_hello]
:world
Specs:
Puts the content-type response header taking into account the charset.
Specs:
Puts a response cookie.
Options
:domain
- the domain the cookie applies to;:max_age
- the cookie max-age;:path
- the path the cookie applies to;:secure
- if the cookie must be sent only over https;
Specs:
Puts a new response header.
Specs:
Puts specified value in session for given key.
Specs:
Stores the given status code in the connection.
The status code can an nil, an integer or an atom.
The list of allowed atoms is available in Plug.Conn.Status
.
Specs:
Reads the request body.
This function reads a chunk of the request body. If there is more data to be
read, then {:more, partial_body, conn}
is returned. Otherwise
{:ok, body, conn}
is returned. In case of error reading the socket,
{:error, reason}
is returned as per :gen_tcp.recv/2
.
Because the request body can be of any size, reading the body will only
work once, as Plug will not cache the result of these operations. If you
need to access the body multiple times, it is your responsibility to store
it. Finally keep in mind some plugs like Plug.Parsers
may read the body,
so the body may be unavailable after accessing such plugs.
This function is able to handle both chunked and identity transfer-encoding by default.
Options
:length
- sets the max body length to read, defaults to 8,000,000 bytes;:read_length
- set the amount of bytes to read at one time, defaults to 1,000,000 bytes;:read_timeout
- set the timeout for each chunk received, defaults to 15000ms;
Example
{:ok, body, conn} = Plug.Conn.read_body(conn, length: 1_000_000)
Specs:
Registers a callback to be invoked before the response is sent.
Callbacks are invoked in the reverse order they are defined (callbacks defined first are invoked last).
Specs:
Sets the response to given status and body.
It sets the connection state to :set
(if not yet :set
)
and raises Plug.Conn.AlreadySentError
if it was already sent.
Specs:
Sends the response headers as a chunked response.
It expects a connection that was not yet :sent
and sets its
state to :chunked
afterwards. Otherwise raises
Plug.Conn.AlreadySentError
.
Specs:
- send_file(t, status, filename :: binary, offset :: integer, length :: integer | :all) :: t | no_return
Sends a file as the response body with the given status
and optionally starting at the given offset until the given length.
If available, the file is sent directly over the socket using
the operating system sendfile
operation.
It expects a connection that was not yet :sent
and sets its
state to :sent
afterwards. Otherwise raises
Plug.Conn.AlreadySentError
.
Specs:
Sends a response to the client.
It expects the connection state is to be :set
,
otherwise raises ArgumentError for :unset
connections
or Plug.Conn.AlreadySentError
if it was already sent.
At the end sets the connection state to :sent
.