0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 04:30:25 +00:00

Update the cowboy_handler manual

Also fixes a small mistake in cowboy_websocket.
This commit is contained in:
Loïc Hoguin 2016-12-23 15:33:21 +01:00
parent 98ae01b599
commit b13c07932d
No known key found for this signature in database
GPG key ID: 71366FF21851DF03
3 changed files with 107 additions and 73 deletions

View file

@ -2,98 +2,68 @@
== Name == Name
cowboy_handler - handler middleware and behaviour cowboy_handler - Plain HTTP handlers
== Description == Description
The `cowboy_handler` middleware executes the handler passed The `cowboy_handler` middleware executes the handler selected
through the environment values `handler` and `handler_opts`, by the router or any other preceding middleware.
and adds the result of this execution to the environment as
the value `result`, indicating that the request has been
handled and received a response.
Environment input: 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.
handler = module():: Handler to be executed. This module also defines a callback interface for handling
handler_opts = any():: Options to be passed to the handler. HTTP requests.
Environment output:
result = ok:: Result of the request.
This module also defines the `cowboy_handler` behaviour that
defines the basic interface for handlers. All Cowboy handlers
implement at least the `init/2` callback, and may implement
the `terminate/3` callback optionally.
== Terminate reasons
The following values may be received as the terminate reason
in the optional `terminate/3` callback. Different handler types
may define additional terminate reasons.
normal::
The connection was closed normally.
{crash, Class, Reason}::
A crash occurred in the handler. `Class` and `Reason` can be
used to obtain more information about the crash. The function
`erlang:get_stacktrace/0` can also be called to obtain the
stacktrace of the process when the crash occurred.
== Callbacks == Callbacks
=== init(Req, Opts) -> {ok, Req, State} | {Module, Req, State} | {Module, Req, State, hibernate | Timeout} | {Module, Req, State, Timeout, hibernate} Plain HTTP handlers implement the following interface:
Req = cowboy_req:req():: The Req object. [source,erlang]
Opts = any():: Handler options. ----
State = any():: Handler state. init(Req, State) -> {ok, Req, State}
Module = module():: Module of the sub-protocol to use.
Timeout = timeout():: Timeout passed to the sub-protocol, when applicable.
Process the request. terminate(Reason, Req, State) -> ok %% optional
This function can be used to switch to an alternate handler Req :: cowboy_req:req()
type by returning the name of the module to be used, along State :: any()
with a few options. Reason :: normal
| {crash, error | exit | throw, any()}
----
For basic handlers this is the function where the response These two callbacks are common to all handlers.
should be sent. If no response is sent, Cowboy will ensure
that a `204 No Content` response is sent.
A crash in this callback will result in `terminate/3` being Plain HTTP handlers do all their work in the `init/2`
called if it is defined, with the `State` argument set to callback. Returning `ok` terminates the handler. If no
the value of `Opts` originally given to the `init/2` callback. response is sent, Cowboy will send a `204 No Content`.
=== terminate(Reason, Req, State) -> ok 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.
Reason = any():: Reason for termination. The following terminate reasons are defined for plain HTTP
Req = cowboy_req:req():: The Req object. handlers:
State = any():: Handler state.
Perform any necessary cleanup of the state. normal::
The connection was closed normally.
This callback should release any resource currently in use, {crash, Class, Reason}::
clear any active timer and reset the process to its original A crash occurred in the handler. `Class` and `Reason` can be
state, as it might be reused for future requests sent on the used to obtain more information about the crash. The function
same connection. Typical plain HTTP handlers rarely need to `erlang:get_stacktrace/0` can also be called to obtain the
use it. stacktrace of the process when the crash occurred.
A crash in this callback or an invalid return value will
result in the closing of the connection and the termination
of the process.
== Exports == Exports
=== terminate(Reason, Req, State, Handler) -> ok The following function should be called by modules implementing
custom handlers to execute the optional terminate callback:
Reason = any():: Reason for termination. * link:man:cowboy_handler:terminate(3)[cowboy_handler:terminate(3)] - Terminate the handler
Req = cowboy_req:req():: The Req object.
State = any():: Handler state.
Handler = module():: Handler module.
Call the optional `terminate/3` callback if it exists. == See also
This function should always be called at the end of the execution link:man:cowboy(7)[cowboy(7)]
of a handler, to give it a chance to clean up or perform
miscellaneous operations.

View file

@ -0,0 +1,64 @@
= cowboy_handler:terminate(3)
== Name
cowboy_handler:terminate - Terminate the handler
== Description
[source,erlang]
----
terminate(Reason, Req | undefined, State, Handler) -> ok
Reason :: any()
Req :: cowboy_req:req()
State :: any()
Handler :: module()
----
Call the optional terminate callback if it is defined.
Make sure to use this function at the end of the execution
of modules that implement custom handler behaviors.
== Arguments
Reason::
Reason for termination.
Req::
The Req object.
+
It is possible to pass `undefined` if the handler has no concept
of requests/responses and discarded the Req object before calling
this function.
State::
Handler state.
Handler::
Handler module.
== Return value
The atom `ok` is always returned. It can be safely ignored.
== Changelog
* *2.0*: Function introduced.
== Examples
.Terminate a handler normally
[source,erlang]
----
cowboy_handler:terminate(normal, Req, State, Handler).
----
== See also
link:man:cowboy_handler(3)[cowboy_handler(3)]

View file

@ -52,7 +52,7 @@ websocket_init(State) -> CallResult %% optional
websocket_handle(InFrame, State) -> CallResult websocket_handle(InFrame, State) -> CallResult
websocket_info(Info, State) -> CallResult websocket_info(Info, State) -> CallResult
terminate(Reason, undefined, State) -> any() %% optional terminate(Reason, undefined, State) -> ok %% optional
Req :: cowboy_req:req() Req :: cowboy_req:req()
State :: any() State :: any()