| 1 |
[[middlewares]] |
| 2 |
== Middlewares |
| 3 |
|
| 4 |
Cowboy delegates the request processing to middleware components. |
| 5 |
By default, two middlewares are defined, for the routing and handling |
| 6 |
of the request, as is detailed in most of this guide. |
| 7 |
|
| 8 |
Middlewares give you complete control over how requests are to be |
| 9 |
processed. You can add your own middlewares to the mix or completely |
| 10 |
change the chain of middlewares as needed. |
| 11 |
|
| 12 |
Cowboy will execute all middlewares in the given order, unless one |
| 13 |
of them decides to stop processing. |
| 14 |
|
| 15 |
=== Usage |
| 16 |
|
| 17 |
Middlewares only need to implement a single callback: `execute/2`. |
| 18 |
It is defined in the `cowboy_middleware` behavior. |
| 19 |
|
| 20 |
This callback has two arguments. The first is the `Req` object. |
| 21 |
The second is the environment. |
| 22 |
|
| 23 |
Middlewares can return one of three different values: |
| 24 |
|
| 25 |
* `{ok, Req, Env}` to continue the request processing |
| 26 |
* `{suspend, Module, Function, Args}` to hibernate |
| 27 |
* `{stop, Req}` to stop processing and move on to the next request |
| 28 |
|
| 29 |
Of note is that when hibernating, processing will resume on the given |
| 30 |
MFA, discarding all previous stacktrace. Make sure you keep the `Req` |
| 31 |
and `Env` in the arguments of this MFA for later use. |
| 32 |
|
| 33 |
If an error happens during middleware processing, Cowboy will not try |
| 34 |
to send an error back to the socket, the process will just crash. It |
| 35 |
is up to the middleware to make sure that a reply is sent if something |
| 36 |
goes wrong. |
| 37 |
|
| 38 |
=== Configuration |
| 39 |
|
| 40 |
The middleware environment is defined as the `env` protocol option. |
| 41 |
In the previous chapters we saw it briefly when we needed to pass |
| 42 |
the routing information. It is a list of tuples with the first |
| 43 |
element being an atom and the second any Erlang term. |
| 44 |
|
| 45 |
Two values in the environment are reserved: |
| 46 |
|
| 47 |
* `listener` contains the name of the listener |
| 48 |
* `result` contains the result of the processing |
| 49 |
|
| 50 |
The `listener` value is always defined. The `result` value can be |
| 51 |
set by any middleware. If set to anything other than `ok`, Cowboy |
| 52 |
will not process any subsequent requests on this connection. |
| 53 |
|
| 54 |
The middlewares that come with Cowboy may define or require other |
| 55 |
environment values to perform. |
| 56 |
|
| 57 |
You can update the environment by calling the `cowboy:set_env/3` |
| 58 |
convenience function, adding or replacing a value in the environment. |
| 59 |
|
| 60 |
=== Routing middleware |
| 61 |
|
| 62 |
The routing middleware requires the `dispatch` value. If routing |
| 63 |
succeeds, it will put the handler name and options in the `handler` |
| 64 |
and `handler_opts` values of the environment, respectively. |
| 65 |
|
| 66 |
=== Handler middleware |
| 67 |
|
| 68 |
The handler middleware requires the `handler` and `handler_opts` |
| 69 |
values. It puts the result of the request handling into `result`. |