Dynamo.Connection behaviour
This modules defines the connection API.
By default, Elixir ships with two implementations
of this API: Dynamo.Cowboy.Connection
(used by
Cowboy web server) and Dynamo.Connection.Test
.
Notice that this module documentation uses the record
notation. So although the documentation says params(conn)
,
the function should be invoked as conn.params()
and
Elixir automatically moves the conn
to the last argument.
It is also important to remind that, as in all Elixir
structures, a connection is immutable. So if you are using
conn.put_resp_header("X-API", "123456")
to set a response
header, a new connection will be returned with the new header
set. The original conn
is not going to be modified.
Functions summary
default_before_send() | Default values for before send callbacks. It contains callbacks to set content type, configure cookies and session. |
Callbacks summary
already_sent?(conn) | Returns a boolean if the connection was already sent or not. This must return the correct result regardless if we have an updated copy of the connection or not. |
assign(key, value, conn) | Puts a new assign with the given key and value. |
assigns(conn) | Returns a keywords list with assigns set so far. |
chunk(body, conn) | Send the given data through the socket.
|
delete_resp_header(key, conn) | Deletes a response header. The header key must be downcased. |
fetch(fetch_aspect, conn) | Responsible for fetching and caching aspects of the response. The default "fetchable" aspects are: headers, params, cookies and body. |
fetchable(atom, list2, conn) | Register an aspect as fetchable in the connection. |
forward_to(segments, module, conn) | Mounts the request by setting the new path information to the given segments. Both scriptname/1 and pathsegments/1 are updated. The segments given must be a suffix of the current path segments. |
host(conn) | Returns the request host. |
host_url(conn) | Returns the host_url (i.e. containing the scheme, hort and port). |
main(conn) | Returns the main module that started the connection the request. |
method(conn) | Returns the HTTP method as a binary. |
method(method, conn) | Changes the request method to the given |
original_method(conn) | Returns the original HTTP method as a binary. Sometimes a filter may change the method from HEAD to GET or from POST to PUT, this function returns the original method. |
params(conn) | Returns the params retrieved from the query string and the request
body as a |
path(conn) | Returns the full path as a binary, as received by the web server. |
path_info(conn) | Returns the request path relative to the forwarding endpoint as a binary. |
path_info_segments(conn) | Return the path as a list of binaries split on "/".
If the request was forwarded request, |
path_segments(conn) | Returns the full path segments, as received by the web server. |
peer(conn) | The remote host ip address. |
port(conn) | Returns the request port. |
private(conn) | Returns a keywords list with private assigns set so far. |
put_assign(key, value, conn) | The same as |
put_private(key, value, conn) | Sets a new private assign with the given key and value. We recommended private keys to follow the format: |
put_resp_cookie(key, value, opts, conn) | Puts the response cookie with the given key, value and list of options. |
put_resp_header(key, value, conn) | Puts a response header, overriding any previous value.
Both |
query_string(conn) | Returns the query string as a binary. |
req_body(conn) | Returns the request body as a binary. |
req_cookies(conn) | Returns the cookies sent in the request as a |
req_headers(conn) | Returns the request headers as |
resp(status, body, conn) | Sets a response to the given status and body. The
response will only be sent when |
resp_body(conn) | Returns the response body if one was set. |
resp_body(body, conn) | Sets the response body and changes the state to |
resp_charset(conn) | Gets the response charset. Defaults to "utf-8". |
resp_charset(charset, conn) | Sets the response charset. The charset is just added
to the response if |
resp_content_type(conn) | Gets the response content-type. |
resp_content_type(content_type, conn) | Sets the response content-type. This is sent as a header when the response is sent. |
resp_cookies(conn) | Returns the response cookies as a list of three element tuples containing the key, value and given options. |
resp_headers(conn) | Returns the response headers as |
route_params(conn) | Returns the parameters that were set as part of the route matching. This is used internally by Dynamo and a developer likely does not need to invoke it manually. |
route_params(t, conn) | Updates the route parameters. This is used internally by Dynamo and a developer does not need to invoke it manually. |
scheme(conn) | Returns the request scheme. |
script_name(conn) | As in CGI environment, returns the current forwarded endpoint as binary. |
script_name_segments(conn) | As in CGI environment, returns the current forwarded endpoint as segments. |
send(conn) | A shortcut to |
send(status, body, conn) | Sends to the client the given status and body.
An updated connection is returned with |
send_chunked(status, conn) | Starts to send a chunked response to the client.
An updated connection is returned with |
sendfile(status, path, conn) | Sends the file at the given path. It is expected that the given path exists and it points to a regular file. The file is sent straight away. |
state(conn) | Returns the response state. It can be: |
status(conn) | Returns the response status if one was set. |
status(status, conn) | Sets the response status and changes the state to |
version(conn) | Returns the HTTP version. |
Types
body :: binary
status :: non_neg_integer
method :: binary
segments :: [binary]
charset :: binary
content_type :: binary
fetch_aspect :: :headers | :params | :cookies | :body | atom
main :: module
scheme :: :http | :https
host :: binary
port_number :: :inet.port_number
host_url :: binary
state :: :unset | :set | :chunked | :sendfile | :sent
Functions
default_before_send()
Default values for before send callbacks. It contains callbacks to set content type, configure cookies and session.
Callbacks
already_sent?(conn)
Specs:
- already_sent?(conn) :: boolean
Returns a boolean if the connection was already sent or not. This must return the correct result regardless if we have an updated copy of the connection or not.
chunk(body, conn)
Specs:
Send the given data through the socket.
send_chunked/2
needs to be called before chunk/2
.
delete_resp_header(key, conn)
Specs:
- delete_resp_header(key :: String.Chars.t, conn) :: conn
Deletes a response header. The header key must be downcased.
fetch(fetch_aspect, conn)
Specs:
- fetch(fetch_aspect | [fetch_aspect], conn) :: conn
Responsible for fetching and caching aspects of the response. The default "fetchable" aspects are: headers, params, cookies and body.
forward_to(segments, module, conn)
Specs:
Mounts the request by setting the new path information to the given segments. Both scriptname/1 and pathsegments/1 are updated. The segments given must be a suffix of the current path segments.
method(method, conn)
Specs:
Changes the request method to the given method
,
storing the previous value in original_method.
original_method(conn)
Specs:
Returns the original HTTP method as a binary. Sometimes a filter may change the method from HEAD to GET or from POST to PUT, this function returns the original method.
Examples
conn.original_method #=> "GET"
params(conn)
Specs:
- params(conn) :: Binary.Dict.t | no_return
Returns the params retrieved from the query string and the request
body as a Binary.Dict
. The parameters need to be explicitly
fetched with conn.fetch(:params)
before using this function.
path(conn)
Specs:
- path(conn) :: binary
Returns the full path as a binary, as received by the web server.
path_info(conn)
Specs:
- path_info(conn) :: binary
Returns the request path relative to the forwarding endpoint as a binary.
path_info_segments(conn)
Specs:
Return the path as a list of binaries split on "/".
If the request was forwarded request, path_info_segments
returns
only the segments related to the current forwarded endpoint.
path_segments(conn)
Specs:
- path_segments(conn) :: [binary]
Returns the full path segments, as received by the web server.
put_private(key, value, conn)
Specs:
Sets a new private assign with the given key and value. We recommended private keys to follow the format:
:"#{plugin}_#{key}"
For example, a Dynamo key to store timeout values would
be set as: :dynamo_timeout
.
put_resp_cookie(key, value, opts, conn)
Specs:
Puts the response cookie with the given key, value and list of options.
put_resp_header(key, value, conn)
Specs:
- put_resp_header(key :: String.Chars.t, value :: String.Chars.t, conn) :: conn
Puts a response header, overriding any previous value.
Both key
and value
are converted to binary.
The header key must be downcased.
req_cookies(conn)
Specs:
- req_cookies(conn) :: Binary.Dict.t | no_return
Returns the cookies sent in the request as a Binary.Dict
.
Cookies need to be explicitly fetched with conn.fetch(:cookies)
before using this function.
req_headers(conn)
Specs:
Returns the request headers as Binary.Dict
. Note that duplicated
entries are removed. The headers need to be explicitly fetched with
conn.fetch(:headers)
before using this function. Headers keys are
all downcased.
resp(status, body, conn)
Specs:
Sets a response to the given status and body. The
response will only be sent when send
is called.
After calling this function, the state changes to :set
,
both status
and resp_body
are set.
resp_charset(conn)
Specs:
- resp_charset(conn) :: binary
Gets the response charset. Defaults to "utf-8".
resp_charset(charset, conn)
Specs:
Sets the response charset. The charset is just added
to the response if resp_content_type
is also set.
resp_content_type(conn)
Specs:
- resp_content_type(conn) :: binary | nil
Gets the response content-type.
resp_content_type(content_type, conn)
Specs:
- resp_content_type(content_type, conn) :: conn
Sets the response content-type. This is sent as a header when the response is sent.
resp_cookies(conn)
Specs:
- resp_cookies(conn) :: [{binary, binary, []}]
Returns the response cookies as a list of three element tuples containing the key, value and given options.
resp_headers(conn)
Specs:
- resp_headers(conn) :: Binary.Dict.t
Returns the response headers as Binary.Dict
.
Header names are all downcased.
route_params(conn)
Specs:
Returns the parameters that were set as part of the route matching. This is used internally by Dynamo and a developer likely does not need to invoke it manually.
route_params(t, conn)
Specs:
Updates the route parameters. This is used internally by Dynamo and a developer does not need to invoke it manually.
script_name(conn)
Specs:
- script_name(conn) :: binary
As in CGI environment, returns the current forwarded endpoint as binary.
script_name_segments(conn)
Specs:
As in CGI environment, returns the current forwarded endpoint as segments.
send(status, body, conn)
Specs:
Sends to the client the given status and body.
An updated connection is returned with :sent
state,
the given status and response body set to nil.
send_chunked(status, conn)
Specs:
Starts to send a chunked response to the client.
An updated connection is returned with :chunked
state,
the given status and response body set to nil.
Use chunk/2
to chunk each part of the response.
sendfile(status, path, conn)
Specs:
Sends the file at the given path. It is expected that the given path exists and it points to a regular file. The file is sent straight away.
state(conn)
Specs:
Returns the response state. It can be:
:unset
- the response was not configured yet:set
- the response was set viaconn.resp_body
orconn.status
:chunked
- the response is being sent in chunks:sent
- the response was sent