0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-15 20:50:24 +00:00

Improve handler interface and documentation

This change simplifies a little more the sub protocols mechanism.
Aliases have been removed. The renaming of loop handlers as long
polling handlers has been reverted.

Plain HTTP handlers now simply do their work in the init/2
callback. There is no specific code for them.

Loop handlers now follow the same return value as Websocket,
they use ok to continue and shutdown to stop.

Terminate reasons for all handler types have been documented.
The terminate callback is now appropriately called in all cases
(or should be).

Behaviors for all handler types have been moved in the module
that implement them. This means that cowboy_handler replaces
the cowboy_http_handler behavior, and similarly cowboy_loop
replaces cowboy_loop_handler, cowboy_websocket replaces
cowboy_websocket_handler. Finally cowboy_rest now has the
start of a behavior in it and will have the full list of
optional callbacks defined once Erlang 18.0 gets released.

The guide has been reorganized and should be easier to follow.
This commit is contained in:
Loïc Hoguin 2014-09-30 20:12:13 +03:00
parent 5ce4c2bfb4
commit 0dc063ab7d
82 changed files with 778 additions and 1037 deletions

View file

@ -3,12 +3,13 @@
The `cowboy_rest` module implements REST semantics on top of
the HTTP protocol.
This module cannot be described as a behaviour due to most of
the callbacks it defines being optional. It has the same
semantics as a behaviour otherwise.
This module is a sub protocol that defines many callbacks
be implemented by handlers. The `init/2` and `terminate/3`
callbacks are common to all handler types and are documented
in the manual for the ^cowboy_handler module.
The only mandatory callback is `init/3`, needed to perform
the protocol upgrade.
All other callbacks are optional, though some may become
required depending on the return value of previous callbacks.
:: Types
@ -43,47 +44,24 @@ The media-type is the content-type, excluding the charset.
This value is always defined after the call to
`content_types_provided/2`.
:: Terminate reasons
The following values may be received as the terminate reason
in the optional `terminate/3` callback.
: 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
: init({TransportName, ProtocolName}, Req, Opts)
-> {upgrade, protocol, cowboy_rest}
| {upgrade, protocol, cowboy_rest, Req, Opts}
Types:
* TransportName = tcp | ssl | atom()
* ProtocolName = http | atom()
* Req = cowboy_req:req()
* Opts = any()
Upgrade the protocol to `cowboy_rest`.
This is the only mandatory callback.
: rest_init(Req, Opts) -> {ok, Req, State}
Types:
* Req = cowboy_req:req()
* Opts = any()
* State = any()
Initialize the state for this request.
: rest_terminate(Req, State) -> ok
Types:
* Req = cowboy_req:req()
* State = any()
Perform any necessary cleanup of the state.
This callback should release any resource currently in use,
clear any active timer and reset the process to its original
state, as it might be reused for future requests sent on the
same connection.
: Callback(Req, State) -> {Value, Req, State} | {halt, Req, State}
Types: