2016-01-14 13:35:25 +01:00
|
|
|
= cowboy_handler(3)
|
|
|
|
|
|
|
|
== Name
|
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
cowboy_handler - Plain HTTP handlers
|
2016-01-14 13:35:25 +01:00
|
|
|
|
|
|
|
== Description
|
2013-05-17 13:13:27 +02:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
The `cowboy_handler` middleware executes the handler selected
|
|
|
|
by the router or any other preceding middleware.
|
2013-05-17 13:13:27 +02:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
This middleware takes the handler module and initial state
|
|
|
|
from the `handler` and `handler_opts` environment values,
|
|
|
|
respectively. On completion, it adds a `result` value to
|
|
|
|
the middleware environment, containing the return value
|
|
|
|
of the `terminate/3` callback (if defined) and `ok` otherwise.
|
2014-07-06 13:10:35 +02:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
This module also defines a callback interface for handling
|
|
|
|
HTTP requests.
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
== Callbacks
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
Plain HTTP handlers implement the following interface:
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
[source,erlang]
|
|
|
|
----
|
|
|
|
init(Req, State) -> {ok, Req, State}
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
terminate(Reason, Req, State) -> ok %% optional
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-12-23 17:20:54 +01:00
|
|
|
Req :: cowboy_req:req()
|
|
|
|
State :: any()
|
|
|
|
Reason :: normal
|
|
|
|
| {crash, error | exit | throw, any()}
|
2016-12-23 15:33:21 +01:00
|
|
|
----
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
These two callbacks are common to all handlers.
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
Plain HTTP handlers do all their work in the `init/2`
|
|
|
|
callback. Returning `ok` terminates the handler. If no
|
|
|
|
response is sent, Cowboy will send a `204 No Content`.
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
The optional `terminate/3` callback will ultimately be called
|
|
|
|
with the reason for the termination of the handler.
|
|
|
|
Cowboy will terminate the process right after this. There
|
|
|
|
is no need to perform any cleanup in this callback.
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
The following terminate reasons are defined for plain HTTP
|
|
|
|
handlers:
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
normal::
|
|
|
|
The connection was closed normally.
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
{crash, Class, Reason}::
|
|
|
|
A crash occurred in the handler. `Class` and `Reason` can be
|
2019-12-31 15:10:38 +01:00
|
|
|
used to obtain more information about the crash.
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-01-14 13:35:25 +01:00
|
|
|
== Exports
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
The following function should be called by modules implementing
|
|
|
|
custom handlers to execute the optional terminate callback:
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
* link:man:cowboy_handler:terminate(3)[cowboy_handler:terminate(3)] - Terminate the handler
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
== See also
|
2014-09-30 20:12:13 +03:00
|
|
|
|
2016-12-23 15:33:21 +01:00
|
|
|
link:man:cowboy(7)[cowboy(7)]
|