0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 20:30:23 +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

@ -33,8 +33,3 @@ Instead of going into every single details of the language,
Joe focuses on the central concepts behind Erlang, and shows Joe focuses on the central concepts behind Erlang, and shows
you how they can be used to write a variety of different you how they can be used to write a variety of different
applications. applications.
At the time of writing, the 2nd edition of the book is in beta,
and includes a few details about upcoming Erlang features that
cannot be used today. Choose the edition you want, then get
reading!

View file

@ -150,15 +150,15 @@ $ make new t=cowboy_http n=hello_handler
``` ```
You can then open the `src/hello_handler.erl` file and modify You can then open the `src/hello_handler.erl` file and modify
the `handle/2` function like this to send a reply. the `init/2` function like this to send a reply.
``` erlang ``` erlang
handle(Req, State=#state{}) -> init(Req, Opts) ->
Req2 = cowboy_req:reply(200, Req2 = cowboy_req:reply(200,
[{<<"content-type">>, <<"text/plain">>}], [{<<"content-type">>, <<"text/plain">>}],
<<"Hello Erlang!">>, <<"Hello Erlang!">>,
Req), Req),
{ok, Req2, State}. {ok, Req2, Opts}.
``` ```
What the above code does is send a `200 OK` reply, with the What the above code does is send a `200 OK` reply, with the

View file

@ -0,0 +1,99 @@
::: Handlers
Handlers are Erlang modules that handle HTTP requests.
:: Plain HTTP handlers
The most basic handler in Cowboy implements the mandatory
`init/2` callback, manipulates the request, optionally
sends a response and then returns.
This callback receives the ^"Req object^req and the options
defined during the ^"router configuration^routing^.
A handler that does nothing would look like this:
``` erlang
init(Req, Opts) ->
{ok, Req, Opts}.
```
Despite sending no reply, a `204 No Content` reply will be
sent to the client, as Cowboy makes sure that a reply is
sent for every request.
We need to use the Req object for sending a reply.
``` erlang
init(Req, Opts) ->
Req2 = cowboy_req:reply(200, [
{<<"content-type">>, <<"text/plain">>}
], <<"Hello World!">>, Req),
{ok, Req2, Opts}.
```
As you can see we return a 3-tuple. `ok` means that the
handler ran successfully. The Req object is returned as
it may have been modified as is the case here: replying
returns a modified Req object that you need to return
back to Cowboy for proper operations.
The last value of the tuple is a state that will be used
in every subsequent callbacks to this handler. Plain HTTP
handlers only have one additional callback, the optional
`terminate/3`.
:: Other handlers
The `init/2` callback can also be used to inform Cowboy
that this is a different kind of handler and that Cowboy
should switch to it. To do this you simply need to return
the module name of the handler type you want to switch to.
Cowboy comes with three handler types you can switch to:
^"cowboy_rest^rest_handlers^, ^"cowboy_websocket^ws_handlers^
and ^"cowboy_loop^loop_handlers^. In addition to those you
can define your own handler types.
Switching is simple. Instead of returning `ok`, you simply
return the name of the handler type you want to use. The
following snippet switches to a Websocket handler:
``` erlang
init(Req, Opts) ->
{cowboy_websocket, Req, Opts}.
```
You can also switch to your own custom handler type:
``` erlang
init(Req, Opts) ->
{my_handler_type, Req, Opts}.
```
How to implement a custom handler type is described in the
^"Sub protocols^sub_protocols chapter.
:: Cleaning up
All handlers coming with Cowboy allow the use of the optional
`terminate/3` callback.
``` erlang
terminate(_Reason, Req, State) ->
ok.
```
This callback is strictly reserved for any required cleanup.
You cannot send a response from this function. There is no
other return value.
If you used the process dictionary, timers, monitors or may
be receiving messages, then you can use this function to clean
them up, as Cowboy might reuse the process for the next
keep-alive request.
Note that while this function may be called in a Websocket
handler, it is generally not useful to do any clean up as
the process terminates immediately after calling this callback
when using Websocket.

View file

@ -1,132 +0,0 @@
::: Handling plain HTTP requests
The simplest way to handle a request is by writing a
plain HTTP handler. It is modeled after Erlang/OTP's
gen_server behaviour, although simplified, as Cowboy
will simply call the three callbacks sequentially.
:: Initialization
The first callback, `init/2`, is common to all handlers,
as it is used to identify the type of handler. Plain
HTTP handlers just return `http`.
``` erlang
init(Req, Opts) ->
{http, Req, Opts}.
```
This function receives the options associated with
this route that you configured previously.
You do not need to validate the options unless they
are user configured. If they are, and there's a
configuration error, you may choose to crash. For
example, this will crash if the required `lang`
option is not found.
``` erlang
init(Req, Opts) ->
{_, Lang} = lists:keyfind(lang, 1, Opts),
{http, Req, Lang}.
```
If your users are unlikely to figure out the issue
without explanations, then you should send a more
meaningful error back to the user. Since we already
replied to the user, there's no need for us to
continue with the handler code, so we use the
`shutdown` return value to stop early.
``` erlang
init(Req, Opts) ->
case lists:keyfind(lang, 1, Opts) of
false ->
Req2 = cowboy_req:reply(500, [
{<<"content-type">>, <<"text/plain">>}
], "Missing option 'lang'.", Req),
{shutdown, Req2, undefined};
{_, Lang} ->
{http, Req, Lang}
end.
```
Once the options have been validated, we can use them
safely. So we need to pass them onward to the rest of
the handler. That's what the third element of the return
tuple, the state, is for.
You may use a state record for this. The record will make
your handler code clearer and will allow Dialyzer to better
type check your code.
``` erlang
-record(state, {
lang :: en | fr
%% More fields here.
}).
init(Req, Opts) ->
{_, Lang} = lists:keyfind(lang, 1, Opts),
{http, Req, #state{lang=Lang}}.
```
You may also use a map. A map is interesting in that you
do not need to define it beforehand, but is a little less
efficient and not as well supported by Dialyzer.
``` erlang
init(Req, Opts) ->
{_, Lang} = lists:keyfind(lang, 1, Opts),
{http, Req, #{lang => Lang}.
```
:: Handling the request
The second callback, `handle/2`, is specific to plain HTTP
handlers. It's where you handle the request.
A handle function that does nothing would look like this:
``` erlang
handle(Req, State) ->
{ok, Req, State}.
```
There's no other return value. To obtain information about
the request, or send a response, you would use the Req object
here. The Req object is documented in its own chapter.
The following handle function will send a fairly original response.
``` erlang
handle(Req, State) ->
Req2 = cowboy_req:reply(200, [
{<<"content-type">>, <<"text/plain">>}
], <<"Hello World!">>, Req),
{ok, Req2, State}.
```
:: Cleaning up
The third and last callback, `terminate/3`, is optional.
``` erlang
terminate(_Reason, Req, State) ->
ok.
```
This callback is strictly reserved for any required cleanup.
You cannot send a response from this function. There is no
other return value.
If you used the process dictionary, timers, monitors or may
be receiving messages, then you can use this function to clean
them up, as Cowboy might reuse the process for the next
keep-alive request.
The chances of any of this happening in your handler are pretty
thin however. The use of the process dictionary is discouraged
in Erlang code in general. And if you need to use timers, monitors
or to receive messages, you are better off with a loop handler,
a different kind of handler meant specifically for this use.

View file

@ -1,35 +1,34 @@
::: Cowboy User Guide ::: Cowboy User Guide
The Cowboy User Guide explores the modern Web and how to make The Cowboy User Guide explores the modern Web and how to make
best use of Cowboy for writing powerful web applications. best use of Cowboy for writing powerful Web applications.
:: Introducing Cowboy :: Rationale
* ^"Introduction^introduction
* ^"The modern Web^modern_web * ^"The modern Web^modern_web
* ^"Erlang and the Web^erlang_web * ^"Erlang and the Web^erlang_web
* ^"Erlang for beginners^erlang_beginners
:: Introduction
* ^"Introduction^introduction
* ^"Getting started^getting_started * ^"Getting started^getting_started
* ^"Request overview^overview
* ^"Erlang for beginners^erlang_beginners
:: HTTP :: Configuration
* ^"The life of a request^http_req_life
* ^"Routing^routing * ^"Routing^routing
* ^"Constraints^constraints * ^"Constraints^constraints
* ^"Handling plain HTTP requests^http_handlers * ^"Static files^static_files
:: Request and response
* ^"Handlers^handlers
* ^"The Req object^req * ^"The Req object^req
* ^"Reading the request body^req_body * ^"Reading the request body^req_body
* ^"Sending a response^resp * ^"Sending a response^resp
* ^"Using cookies^cookies * ^"Using cookies^cookies
* ^"Multipart^multipart
:: Multipart
* ^"Introduction to multipart^multipart_intro
* ^"Multipart requests^multipart_req
:: Static files
* ^"Static handler^static_handlers
:: REST :: REST
@ -43,14 +42,14 @@ best use of Cowboy for writing powerful web applications.
* ^"The Websocket protocol^ws_protocol * ^"The Websocket protocol^ws_protocol
* ^"Handling Websocket connections^ws_handlers * ^"Handling Websocket connections^ws_handlers
:: Server push :: Push technology
* ^"Loop handlers^loop_handlers * ^"Loop handlers^loop_handlers
:: Pluggable interface :: Extensions
* ^"Middlewares^middlewares * ^"Middlewares^middlewares
* ^"Protocol upgrades^upgrade_protocol * ^"Sub protocols^sub_protocols
* ^"Hooks^hooks * ^"Hooks^hooks
:: Internals :: Internals

View file

@ -16,9 +16,7 @@ features both a Function Reference and a User Guide.
:: Prerequisites :: Prerequisites
No Erlang knowledge is required for reading this guide. The reader will Beginner Erlang knowledge is recommended for reading this guide.
be introduced to Erlang concepts and redirected to reference material
whenever necessary.
Knowledge of the HTTP protocol is recommended but not required, as it Knowledge of the HTTP protocol is recommended but not required, as it
will be detailed throughout the guide. will be detailed throughout the guide.
@ -32,12 +30,15 @@ guarantee that the experience will be safe and smooth. You are advised
to perform the necessary testing and security audits prior to deploying to perform the necessary testing and security audits prior to deploying
on other platforms. on other platforms.
Cowboy is developed for Erlang/OTP R16B01, R16B02, R16B03-1, 17.0 and Cowboy is developed for Erlang/OTP 17.0, 17.1.2 and 17.3. By the time
17.1.2. this branch gets released the target version will probably be 18.0 and
above.
Cowboy may be compiled on other Erlang versions with small source code Cowboy may be compiled on other Erlang versions with small source code
modifications but there is no guarantee that it will work as expected. modifications but there is no guarantee that it will work as expected.
Cowboy uses the maps data type which was introduced in Erlang 17.0.
:: Versioning :: Versioning
Cowboy uses ^"Semantic Versioning 2.0.0^http://semver.org/^. Cowboy uses ^"Semantic Versioning 2.0.0^http://semver.org/^.

View file

@ -8,7 +8,7 @@ a response.
Loop handlers are used for requests where a response might not Loop handlers are used for requests where a response might not
be immediately available, but where you would like to keep the be immediately available, but where you would like to keep the
connection open for a while in case the response arrives. The connection open for a while in case the response arrives. The
most known example of such practice is known as long-polling. most known example of such practice is known as long polling.
Loop handlers can also be used for requests where a response is Loop handlers can also be used for requests where a response is
partially available and you need to stream the response body partially available and you need to stream the response body
@ -21,12 +21,12 @@ and allow using built-in features like hibernation and timeouts.
Loop handlers essentially wait for one or more Erlang messages Loop handlers essentially wait for one or more Erlang messages
and feed these messages to the `info/3` callback. It also features and feed these messages to the `info/3` callback. It also features
the `init/3` and `terminate/3` callbacks which work the same as the `init/2` and `terminate/3` callbacks which work the same as
for plain HTTP handlers. for plain HTTP handlers.
:: Initialization :: Initialization
The `init/3` function must return a `loop` tuple to enable The `init/2` function must return a `cowboy_loop` tuple to enable
loop handler behavior. This tuple may optionally contain loop handler behavior. This tuple may optionally contain
a timeout value and/or the atom `hibernate` to make the a timeout value and/or the atom `hibernate` to make the
process enter hibernation until a message is received. process enter hibernation until a message is received.
@ -35,7 +35,7 @@ This snippet enables the loop handler.
``` erlang ``` erlang
init(Req, Opts) -> init(Req, Opts) ->
{long_polling, Req, Opts}. {cowboy_loop, Req, Opts}.
``` ```
However it is largely recommended that you set a timeout However it is largely recommended that you set a timeout
@ -44,7 +44,7 @@ also makes the process hibernate.
``` erlang ``` erlang
init(Req, Opts) -> init(Req, Opts) ->
{long_polling, Req, Opts, 30000, hibernate}. {cowboy_loop, Req, Opts, 30000, hibernate}.
``` ```
:: Receive loop :: Receive loop
@ -61,9 +61,9 @@ message otherwise.
``` erlang ``` erlang
info({reply, Body}, Req, State) -> info({reply, Body}, Req, State) ->
Req2 = cowboy_req:reply(200, [], Body, Req), Req2 = cowboy_req:reply(200, [], Body, Req),
{ok, Req2, State}; {shutdown, Req2, State};
info(_Msg, Req, State) -> info(_Msg, Req, State) ->
{loop, Req, State, hibernate}. {ok, Req, State, hibernate}.
``` ```
Do note that the `reply` tuple here may be any message Do note that the `reply` tuple here may be any message
@ -76,17 +76,17 @@ return a tuple indicating if more messages are to be expected.
The callback may also choose to do nothing at all and just The callback may also choose to do nothing at all and just
skip the message received. skip the message received.
If a reply is sent, then the `ok` tuple should be returned. If a reply is sent, then the `shutdown` tuple should be returned.
This will instruct Cowboy to end the request. This will instruct Cowboy to end the request.
Otherwise a `loop` tuple should be returned. Otherwise an `ok` tuple should be returned.
:: Streaming loop :: Streaming loop
Another common case well suited for loop handlers is Another common case well suited for loop handlers is
streaming data received in the form of Erlang messages. streaming data received in the form of Erlang messages.
This can be done by initiating a chunked reply in the This can be done by initiating a chunked reply in the
`init/3` callback and then using `cowboy_req:chunk/2` `init/2` callback and then using `cowboy_req:chunk/2`
every time a message is received. every time a message is received.
The following snippet does exactly that. As you can see The following snippet does exactly that. As you can see
@ -96,15 +96,15 @@ and the loop is stopped by sending an `eof` message.
``` erlang ``` erlang
init(Req, Opts) -> init(Req, Opts) ->
Req2 = cowboy_req:chunked_reply(200, [], Req), Req2 = cowboy_req:chunked_reply(200, [], Req),
{long_polling, Req2, Opts}. {cowboy_loop, Req2, Opts}.
info(eof, Req, State) -> info(eof, Req, State) ->
{ok, Req, State}; {shutdown, Req, State};
info({chunk, Chunk}, Req, State) -> info({chunk, Chunk}, Req, State) ->
cowboy_req:chunk(Chunk, Req), cowboy_req:chunk(Chunk, Req),
{loop, Req, State}; {ok, Req, State};
info(_Msg, Req, State) -> info(_Msg, Req, State) ->
{loop, Req, State}. {ok, Req, State}.
``` ```
:: Cleaning up :: Cleaning up

View file

@ -1,11 +1,60 @@
::: Multipart requests ::: Multipart requests
Multipart originates from MIME, an Internet standard that
extends the format of emails. Multipart messages are a
container for parts of any content-type.
For example, a multipart message may have a part
containing text and a second part containing an
image. This is what allows you to attach files
to emails.
In the context of HTTP, multipart is most often used
with the `multipart/form-data` content-type. This is
the content-type you have to use when you want browsers
to be allowed to upload files through HTML forms.
Multipart is of course not required for uploading
files, it is only required when you want to do so
through HTML forms.
You can read and parse multipart messages using the You can read and parse multipart messages using the
Req object directly. Req object directly.
Cowboy defines two functions that allows you to get Cowboy defines two functions that allows you to get
information about each part and read their contents. information about each part and read their contents.
:: Structure
A multipart message is a list of parts. Parts may
contain either a multipart message or a non-multipart
content-type. This allows parts to be arranged in a
tree structure, although this is a rare case as far
as the Web is concerned.
:: Form-data
In the normal case, when a form is submitted, the
browser will use the `application/x-www-form-urlencoded`
content-type. This type is just a list of keys and
values and is therefore not fit for uploading files.
That's where the `multipart/form-data` content-type
comes in. When the form is configured to use this
content-type, the browser will use one part of the
message for each form field. This means that a file
input field will be sent in its own part, but the
same applies to all other kinds of fields.
A form with a text input, a file input and a select
choice box will result in a multipart message with
three parts, one for each field.
The browser does its best to determine the content-type
of the files it sends this way, but you should not
rely on it for determining the contents of the file.
Proper investigation of the contents is recommended.
:: Checking the content-type :: Checking the content-type
While there is a variety of multipart messages, the While there is a variety of multipart messages, the

View file

@ -1,50 +0,0 @@
::: Introduction to multipart
Multipart originates from MIME, an Internet standard that
extends the format of emails. Multipart messages are a
container for parts of any content-type.
For example, a multipart message may have a part
containing text and a second part containing an
image. This is what allows you to attach files
to emails.
In the context of HTTP, multipart is most often used
with the `multipart/form-data` content-type. This is
the content-type you have to use when you want browsers
to be allowed to upload files through HTML forms.
Multipart is of course not required for uploading
files, it is only required when you want to do so
through HTML forms.
:: Structure
A multipart message is a list of parts. Parts may
contain either a multipart message or a non-multipart
content-type. This allows parts to be arranged in a
tree structure, although this is a rare case as far
as the Web is concerned.
:: Form-data
In the normal case, when a form is submitted, the
browser will use the `application/x-www-form-urlencoded`
content-type. This type is just a list of keys and
values and is therefore not fit for uploading files.
That's where the `multipart/form-data` content-type
comes in. When the form is configured to use this
content-type, the browser will use one part of the
message for each form field. This means that a file
input field will be sent in its own part, but the
same applies to all other kinds of fields.
A form with a text input, a file input and a select
choice box will result in a multipart message with
three parts, one for each field.
The browser does its best to determine the content-type
of the files it sends this way, but you should not
rely on it for determining the contents of the file.
Proper investigation of the contents is recommended.

View file

@ -1,4 +1,4 @@
::: The life of a request ::: Request overview
This chapter explains the different steps a request This chapter explains the different steps a request
goes through until a response is sent, along with goes through until a response is sent, along with

View file

@ -1,20 +1,20 @@
::: REST handlers ::: REST handlers
REST is implemented in Cowboy as a protocol upgrade. Once upgraded, REST is implemented in Cowboy as a sub protocol. The request
the request is handled as a state machine with many optional callbacks is handled as a state machine with many optional callbacks
describing the resource and modifying the machine's behavior. describing the resource and modifying the machine's behavior.
The REST handler is the recommended way to handle requests. The REST handler is the recommended way to handle HTTP requests.
:: Initialization :: Initialization
First, the `init/2` callback is called. This callback is common First, the `init/2` callback is called. This callback is common
to all handlers. To use REST for the current request, this function to all handlers. To use REST for the current request, this function
must return a `rest` tuple. must return a `cowboy_rest` tuple.
``` erlang ``` erlang
init(Req, Opts) -> init(Req, Opts) ->
{rest, Req, Opts}. {cowboy_rest, Req, Opts}.
``` ```
Cowboy will then switch to the REST protocol and start executing Cowboy will then switch to the REST protocol and start executing

View file

@ -1,8 +1,9 @@
::: Static handler ::: Static files
The static handler is a built-in REST handler for serving files. Cowboy comes with a special handler built as a REST handler
It is available as a convenience and provides a quick solution and designed specifically for serving static files. It is
for serving files during development. provided as a convenience and provides a quick solution for
serving files during development.
For systems in production, consider using one of the many For systems in production, consider using one of the many
Content Distribution Network (CDN) available on the market, Content Distribution Network (CDN) available on the market,

View file

@ -0,0 +1,64 @@
::: Sub protocols
Sub protocols are used for creating new types of handlers that
provide extra functionality in a reusable way. Cowboy uses this
mechanism to provide its loop, REST and Websocket handlers.
This chapter will explain how to create your own sub protocols
and handler types.
:: Usage
To switch to a sub protocol, the `init/2` callback must return
the name of the sub protocol module. Everything past this point
is handled by the sub protocol.
``` erlang
init(Req, Opts) ->
{cowboy_websocket, Req, Opts}.
```
The return value may also have a `Timeout` value and/or the
atom `hibernate`. These options are useful for long living
connections. When they are not provided, the timeout value
defaults to `infinity` and the hibernate value to `run`.
The following snippet switches to the `my_protocol` sub
protocol, sets the timeout value to 5 seconds and enables
hibernation:
``` erlang
init(Req, Opts) ->
{my_protocol, Req, Opts, 5000, hibernate}.
```
If a sub protocol does not make use of these options, it should
crash if it receives anything other than the default values.
:: Upgrade
After the `init/2` function returns, Cowboy will then call the
`upgrade/6` function. This is the only callback defined by the
`cowboy_sub_protocol` behavior.
The function is named `upgrade` because it mimics the mechanism
of HTTP protocol upgrades. For some sub protocols, like Websocket,
an actual upgrade is performed. For others, like REST, this is
only an upgrade at Cowboy's level and the client has nothing to
do about it.
The upgrade callback receives the Req object, the middleware
environment, the handler and its options, and the aforementioned
timeout and hibernate values.
``` erlang
upgrade(Req, Env, Handler, HandlerOpts, Timeout, Hibernate) ->
%% Sub protocol code here.
```
This callback is expected to behave like a middleware and to
return an updated environment and Req object.
Sub protocols are expected to call the `cowboy_handler:terminate/4`
function when they terminate. This function will make sure that
the optional `terminate/3` callback is called, if present.

View file

@ -1,61 +0,0 @@
::: Protocol upgrades
Cowboy features many different handlers, each for different purposes.
All handlers have a common entry point: the `init/2` function.
This function returns the name of the protocol that will be
used for processing the request, along with various options.
Cowboy defines four built-in handler types. Three of them are
implemented as sub protocols. More can be implemented by
writing a custom sub protocol.
The following table lists the built-in handler types.
|| Alias Module Description
|
| http - Plain HTTP handler
| long_polling cowboy_long_polling Long-polling handler
| rest cowboy_rest REST handler
| ws cowboy_websocket Websocket handler
Both the alias or the module name can be used to specify the
kind of handler. In addition, a user-defined module name can
be used.
``` erlang
init(Req, Opts) ->
{my_protocol, Req, Opts}.
```
The `init/2` function can also return some extra options for
handlers that are meant to be long running, for example the
`long_polling` and `ws` handler types. These options can also
be passed on to custom sub protocols. For example the following
`init/2` function defines both a timeout value and enables
process hibernation:
``` erlang
init(Req, Opts) ->
{my_protocol, Req, Opts, 5000, hibernate}.
```
It is up to the sub protocol to implement these (or reject
them if they are not supported).
The `cowboy_sub_protocol` behavior only requires one callback,
`upgrade/6`. It receives the Req object, the middleware environment,
the handler and options for this request, and the timeout and
hibernate values. The default timeout value is `infinity` and
the default hibernate value is `run`.
``` erlang
upgrade(Req, Env, Handler, HandlerOpts, Timeout, Hibernate) ->
%% ...
```
This callback is expected to behave like a middleware. Please
see the corresponding chapter for more information.
Sub protocols are expected to call the `cowboy_handler:terminate/5`
function when they terminate. This function will make sure that
the optional `terminate/3` callback will be called, if present.

View file

@ -17,7 +17,7 @@ must return a `ws` tuple.
``` erlang ``` erlang
init(Req, Opts) -> init(Req, Opts) ->
{ws, Req, Opts}. {cowboy_websocket, Req, Opts}.
``` ```
Upon receiving this tuple, Cowboy will switch to the code Upon receiving this tuple, Cowboy will switch to the code
@ -61,7 +61,7 @@ or enabling timers.
init(Req, _Opts) -> init(Req, _Opts) ->
self() ! post_init, self() ! post_init,
%% Register process here... %% Register process here...
{ws, Req, #state{}}. {cowboy_websocket, Req, #state{}}.
websocket_info(post_init, Req, State) -> websocket_info(post_init, Req, State) ->
%% Perform post_init initialization here... %% Perform post_init initialization here...
@ -161,7 +161,7 @@ A good timeout value is 60 seconds.
``` erlang ``` erlang
init(Req, _Opts) -> init(Req, _Opts) ->
{ws, Req, #state{}, 60000}. {cowboy_websocket, Req, #state{}, 60000}.
``` ```
This value cannot be changed once it is set. It defaults to This value cannot be changed once it is set. It defaults to

View file

@ -25,7 +25,7 @@ excluding the initial flawed draft sometimes known as
:: Implementation :: Implementation
Cowboy implements Websocket as a protocol upgrade. Once the Cowboy implements Websocket as a protocol upgrade. Once the
upgrade is performed from the `init/3` callback, Cowboy upgrade is performed from the `init/2` callback, Cowboy
switches to Websocket. Please consult the next chapter for switches to Websocket. Please consult the next chapter for
more information on initiating and handling Websocket more information on initiating and handling Websocket
connections. connections.

View file

@ -15,10 +15,96 @@ Environment output:
* result = ok * result = ok
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.
:: Types :: Types
None. None.
:: 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
: init(Req, Opts)
-> {ok, Req, State}
| {Module, Req, State}
| {Module, Req, State, hibernate}
| {Module, Req, State, Timeout}
| {Module, Req, State, Timeout, hibernate}
Types:
* Req = cowboy_req:req()
* Opts = any()
* State = any()
* Module = module()
* Timeout = timeout()
Process the request.
This function can be used to switch to an alternate handler
type by returning the name of the module to be used, along
with a few options.
For basic handlers this is the function where the response
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
called if it is defined, with the `State` argument set to
the value of `Opts` originally given to the `init/2` callback.
: terminate(Reason, Req, State) -> ok
Types:
* Reason = any()
* 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. Typical plain HTTP handlers rarely need to
use it.
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
None. : terminate(Reason, Req, State, Handler) -> ok
Types:
* Reason = any()
* Req = cowboy_req:req()
* State = any()
* Handler = module()
Call the optional `terminate/3` callback if it exists.
This function should always be called at the end of the execution
of a handler, to give it a chance to clean up or perform
miscellaneous operations.

View file

@ -1,57 +0,0 @@
::: cowboy_http_handler
The `cowboy_http_handler` behaviour defines the interface used
by plain HTTP handlers.
Unless noted otherwise, the callbacks will be executed sequentially.
:: Types
None.
:: Callbacks
: init({TransportName, ProtocolName}, Req, Opts)
-> {ok, Req, State} | {shutdown, Req, State}
Types:
* TransportName = tcp | ssl | atom()
* ProtocolName = http | atom()
* Req = cowboy_req:req()
* Opts = any()
* State = any()
Initialize the state for this request.
The `shutdown` return value can be used to skip the `handle/2`
call entirely.
: handle(Req, State) -> {ok, Req, State}
Types:
* Req = cowboy_req:req()
* State = any()
Handle the request.
This callback is where the request is handled and a response
should be sent. If a response is not sent, Cowboy will send
a `204 No Content` response automatically.
: terminate(Reason, Req, State) -> ok
Types:
* Reason = {normal, shutdown} | {error, atom()}
* 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. Typical plain HTTP handlers rarely need to
use it.

View file

@ -0,0 +1,100 @@
::: cowboy_loop
The `cowboy_loop` module implements a handler interface for
long running HTTP connections. It is the recommended interface
for long polling and server-sent events, amongst others.
This module is a sub protocol that defines three callbacks to
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 `info/3` callback is specific to loop handlers and will be
called as many times as necessary until a reply is sent.
It is highly recommended to return a timeout value from the
`init/2` callback to ensure that the process is terminated
when no data has been received during that timespan. The
default timeout is `infinity`, which should only be used if
you have alternate means of ending inactive connections.
:: Types
None.
:: Terminate reasons
The following values may be received as the terminate reason
in the optional `terminate/3` callback.
: normal
The connection was closed normally before switching to the
loop sub protocol. This typically happens if an `ok` tuple is
returned from the `init/2` callback.
: shutdown
The handler requested to close the connection by returning
a `shutdown` tuple.
: timeout
The connection has been closed due to inactivity. The timeout
value can be configured from `init/2`. The response sent when
this happens is a `204 No Content`.
: {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.
: {error, overflow}
The connection is being closed and the process terminated
because the buffer Cowboy uses to keep data sent by the
client has reached its maximum. The buffer size can be
configured through the environment value `loop_max_buffer`
and defaults to 5000 bytes.
If the long running request comes with a body it is recommended
to process this body before switching to the loop sub protocol.
: {error, closed}
The socket has been closed brutally without a close frame being
received first.
: {error, Reason}
A socket error ocurred.
:: Callbacks
: info(Info, Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
| {shutdown, Req, State}
Types:
* Info = any()
* Req = cowboy_req:req()
* State = any()
Handle the Erlang message received.
This function will be called every time an Erlang message
has been received. The message can be any Erlang term.
The `shutdown` return value can be used to stop the receive loop,
typically because a response has been sent.
The `hibernate` option will hibernate the process until
it receives another message.
:: Exports
None.

View file

@ -1,91 +0,0 @@
::: cowboy_loop_handler
The `cowboy_loop_handler` behaviour defines the interface used
by HTTP handlers that do not send a response directly, instead
requiring a receive loop to process Erlang messages.
This interface is best fit for long-polling types of requests.
The `init/3` callback will always be called, followed by zero
or more calls to `info/3`. The `terminate/3` callback will
always be called last.
:: Types
None.
:: Callbacks
: init({TransportName, ProtocolName}, Req, Opts)
-> {loop, Req, State}
| {loop, Req, State, hibernate}
| {loop, Req, State, Timeout}
| {loop, Req, State, Timeout, hibernate}
| {shutdown, Req, State}
Types:
* TransportName = tcp | ssl | atom()
* ProtocolName = http | atom()
* Req = cowboy_req:req()
* Opts = any()
* State = any()
* Timeout = timeout()
Initialize the state for this request.
This callback will typically be used to register this process
to an event manager or a message queue in order to receive
the messages the handler wants to process.
The receive loop will run for a duration of up to `Timeout`
milliseconds after it last received data from the socket,
at which point it will stop and send a `204 No Content` reply.
By default this value is set to `infinity`. It is recommended
to either set this value or ensure by any other mechanism
that the handler will be closed after a certain period of
inactivity.
The `hibernate` option will hibernate the process until it
starts receiving messages.
The `shutdown` return value can be used to skip the receive
loop entirely.
: info(Info, Req, State) -> {ok, Req, State} | {loop, Req, State}
| {loop, Req, State, hibernate}
Types:
* Info = any()
* Req = cowboy_req:req()
* State = any()
Handle the Erlang message received.
This function will be called every time an Erlang message
has been received. The message can be any Erlang term.
The `ok` return value can be used to stop the receive loop,
typically because a response has been sent.
The `hibernate` option will hibernate the process until
it receives another message.
: terminate(Reason, Req, State) -> ok
Types:
* Reason = {normal, shutdown} | {normal, timeout} | {error, closed} | {error, overflow} | {error, atom()}
* Req = cowboy_req:req()
* State = any()
Perform any necessary cleanup of the state.
This callback will typically unregister from any event manager
or message queue it registered to in `init/3`.
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.

View file

@ -3,12 +3,13 @@
The `cowboy_rest` module implements REST semantics on top of The `cowboy_rest` module implements REST semantics on top of
the HTTP protocol. the HTTP protocol.
This module cannot be described as a behaviour due to most of This module is a sub protocol that defines many callbacks
the callbacks it defines being optional. It has the same be implemented by handlers. The `init/2` and `terminate/3`
semantics as a behaviour otherwise. 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 All other callbacks are optional, though some may become
the protocol upgrade. required depending on the return value of previous callbacks.
:: Types :: Types
@ -43,47 +44,24 @@ The media-type is the content-type, excluding the charset.
This value is always defined after the call to This value is always defined after the call to
`content_types_provided/2`. `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 :: 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} : Callback(Req, State) -> {Value, Req, State} | {halt, Req, State}
Types: Types:

View file

@ -2,8 +2,25 @@
The `cowboy_websocket` module implements the Websocket protocol. The `cowboy_websocket` module implements the Websocket protocol.
The callbacks for websocket handlers are defined in the manual This module is a sub protocol that defines four callbacks to
for the `cowboy_websocket_handler` behaviour. 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 `websocket_handle/3` and `websocket_info/3` callbacks are
specific to Websocket handlers and will be called as many times
as necessary until the Websocket connection is closed.
The `init/2` callback can be used to negotiate Websocket protocol
extensions with the client. It is highly recommended to return a
timeout value from this callback to ensure that the process is
terminated when no data has been received during that timespan.
The default timeout is `infinity`, which should only be used if
you have alternate means of ending inactive connections.
Cowboy will terminate the process right after closing the
Websocket connection. This means that there is no real need to
perform any cleanup in the optional `terminate/3` callback.
:: Types :: Types
@ -31,6 +48,118 @@ Type: 7 | 8 | 13
The version of the Websocket protocol being used. The version of the Websocket protocol being used.
:: Terminate reasons
The following values may be received as the terminate reason
in the optional `terminate/3` callback.
: normal
The connection was closed normally before establishing a Websocket
connection. This typically happens if an `ok` tuple is returned
from the `init/2` callback.
: remote
The remote endpoint closed the connection without giving any
further details.
: {remote, Code, Payload}
The remote endpoint closed the connection with the given
`Code` and `Payload` as the reason.
: shutdown
The handler requested to close the connection, either by returning
a `shutdown` tuple or by sending a `close` frame.
: timeout
The connection has been closed due to inactivity. The timeout
value can be configured from `init/2`.
: {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.
: {error, badencoding}
A text frame was sent by the client with invalid encoding. All
text frames must be valid UTF-8.
: {error, badframe}
A protocol error has been detected.
: {error, closed}
The socket has been closed brutally without a close frame being
received first.
: {error, Reason}
A socket error ocurred.
:: Callbacks
: websocket_handle(InFrame, Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
| {reply, OutFrame | [OutFrame], Req, State}
| {reply, OutFrame | [OutFrame], Req, State, hibernate}
| {shutdown, Req, State}
Types:
* InFrame = {text | binary | ping | pong, binary()}
* Req = cowboy_req:req()
* State = any()
* OutFrame = frame()
Handle the data received from the Websocket connection.
This function will be called every time data is received
from the Websocket connection.
The `shutdown` return value can be used to close the
connection. A close reply will also result in the connection
being closed.
The `hibernate` option will hibernate the process until
it receives new data from the Websocket connection or an
Erlang message.
: websocket_info(Info, Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
| {reply, OutFrame | [OutFrame], Req, State}
| {reply, OutFrame | [OutFrame], Req, State, hibernate}
| {shutdown, Req, State}
Types:
* Info = any()
* Req = cowboy_req:req()
* State = any()
* OutFrame = frame()
Handle the Erlang message received.
This function will be called every time an Erlang message
has been received. The message can be any Erlang term.
The `shutdown` return value can be used to close the
connection. A close reply will also result in the connection
being closed.
The `hibernate` option will hibernate the process until
it receives another message or new data from the Websocket
connection.
:: Exports :: Exports
None. None.

View file

@ -1,133 +0,0 @@
::: cowboy_websocket_handler
The `cowboy_websocket_handler` behaviour defines the interface used
by Websocket handlers.
The `init/3` and `websocket_init/3` callbacks will always be called,
followed by zero or more calls to `websocket_handle/3` and
`websocket_info/3`. The `websocket_terminate/3` will always
be called last.
:: Types
None.
:: Callbacks
: init({TransportName, ProtocolName}, Req, Opts)
-> {upgrade, protocol, cowboy_websocket}
| {upgrade, protocol, cowboy_websocket, Req, Opts}
Types:
* TransportName = tcp | ssl | atom()
* ProtocolName = http | atom()
* Req = cowboy_req:req()
* Opts = any()
Upgrade the protocol to `cowboy_websocket`.
: websocket_init(TransportName, Req, Opts)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
| {ok, Req, State, Timeout}
| {ok, Req, State, Timeout, hibernate}
| {shutdown, Req}
Types:
* TransportName = tcp | ssl | atom()
* Req = cowboy_req:req()
* Opts = any()
* State = any()
* Timeout = timeout()
Initialize the state for this session.
This function is called before the upgrade to Websocket occurs.
It can be used to negotiate Websocket protocol extensions
with the client. It will typically be used to register this process
to an event manager or a message queue in order to receive
the messages the handler wants to process.
The connection will stay up for a duration of up to `Timeout`
milliseconds after it last received data from the socket,
at which point it will stop and close the connection.
By default this value is set to `infinity`. It is recommended
to either set this value or ensure by any other mechanism
that the handler will be closed after a certain period of
inactivity.
The `hibernate` option will hibernate the process until it
starts receiving either data from the Websocket connection
or Erlang messages.
The `shutdown` return value can be used to close the connection
before upgrading to Websocket.
: websocket_handle(InFrame, Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
| {reply, OutFrame | [OutFrame], Req, State}
| {reply, OutFrame | [OutFrame], Req, State, hibernate}
| {shutdown, Req, State}
Types:
* InFrame = {text | binary | ping | pong, binary()}
* Req = cowboy_req:req()
* State = any()
* OutFrame = cowboy_websocket:frame()
Handle the data received from the Websocket connection.
This function will be called every time data is received
from the Websocket connection.
The `shutdown` return value can be used to close the
connection. A close reply will also result in the connection
being closed.
The `hibernate` option will hibernate the process until
it receives new data from the Websocket connection or an
Erlang message.
: websocket_info(Info, Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
| {reply, OutFrame | [OutFrame], Req, State}
| {reply, OutFrame | [OutFrame], Req, State, hibernate}
| {shutdown, Req, State}
Types:
* Info = any()
* Req = cowboy_req:req()
* State = any()
* OutFrame = cowboy_websocket:frame()
Handle the Erlang message received.
This function will be called every time an Erlang message
has been received. The message can be any Erlang term.
The `shutdown` return value can be used to close the
connection. A close reply will also result in the connection
being closed.
The `hibernate` option will hibernate the process until
it receives another message or new data from the Websocket
connection.
: websocket_terminate(Reason, Req, State) -> ok
Types:
* Reason = {normal, shutdown | timeout} | {remote, closed} | {remote, cowboy_websocket:close_code(), binary()} | {error, badencoding | badframe | closed | atom()}
* Req = cowboy_req:req()
* State = any()
Perform any necessary cleanup of the state.
The connection will be closed and the process stopped right
after this call.

View file

@ -5,8 +5,7 @@ The function reference documents the public interface of Cowboy.
* ^"The Cowboy Application^cowboy_app * ^"The Cowboy Application^cowboy_app
* ^cowboy * ^cowboy
* ^cowboy_handler * ^cowboy_handler
* ^cowboy_http_handler * ^cowboy_loop
* ^cowboy_loop_handler
* ^cowboy_middleware * ^cowboy_middleware
* ^cowboy_protocol * ^cowboy_protocol
* ^cowboy_req * ^cowboy_req
@ -16,5 +15,4 @@ The function reference documents the public interface of Cowboy.
* ^cowboy_static * ^cowboy_static
* ^cowboy_sub_protocol * ^cowboy_sub_protocol
* ^cowboy_websocket * ^cowboy_websocket
* ^cowboy_websocket_handler
* ^"HTTP status codes^http_status_codes * ^"HTTP status codes^http_status_codes

View file

@ -4,16 +4,12 @@
-module(toppage_handler). -module(toppage_handler).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
Req2 = cowboy_req:chunked_reply(200, Req), Req2 = cowboy_req:chunked_reply(200, Req),
cowboy_req:chunk("Hello\r\n", Req2), cowboy_req:chunk("Hello\r\n", Req2),
timer:sleep(1000), timer:sleep(1000),
cowboy_req:chunk("World\r\n", Req2), cowboy_req:chunk("World\r\n", Req2),
timer:sleep(1000), timer:sleep(1000),
cowboy_req:chunk("Chunked!\r\n", Req2), cowboy_req:chunk("Chunked!\r\n", Req2),
{ok, Req2, State}. {ok, Req2, Opts}.

View file

@ -4,12 +4,8 @@
-module(toppage_handler). -module(toppage_handler).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
BigBody = BigBody =
<<"A cowboy is an animal herder who tends cattle on ranches in North America, <<"A cowboy is an animal herder who tends cattle on ranches in North America,
traditionally on horseback, and often performs a multitude of other ranch- traditionally on horseback, and often performs a multitude of other ranch-
@ -24,4 +20,4 @@ considerable respect for their achievements. There are also cattle handlers
in many other parts of the world, particularly South America and Australia, in many other parts of the world, particularly South America and Australia,
who perform work similar to the cowboy in their respective nations.\n">>, who perform work similar to the cowboy in their respective nations.\n">>,
Req2 = cowboy_req:reply(200, [], BigBody, Req), Req2 = cowboy_req:reply(200, [], BigBody, Req),
{ok, Req2, State}. {ok, Req2, Opts}.

View file

@ -4,12 +4,8 @@
-module(toppage_handler). -module(toppage_handler).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
NewValue = integer_to_list(random:uniform(1000000)), NewValue = integer_to_list(random:uniform(1000000)),
Req2 = cowboy_req:set_resp_cookie( Req2 = cowboy_req:set_resp_cookie(
<<"server">>, NewValue, [{path, <<"/">>}], Req), <<"server">>, NewValue, [{path, <<"/">>}], Req),
@ -22,4 +18,4 @@ handle(Req, State) ->
Req3 = cowboy_req:reply(200, Req3 = cowboy_req:reply(200,
[{<<"content-type">>, <<"text/html">>}], [{<<"content-type">>, <<"text/html">>}],
Body, Req2), Body, Req2),
{ok, Req3, State}. {ok, Req3, Opts}.

View file

@ -4,16 +4,12 @@
-module(toppage_handler). -module(toppage_handler).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
Method = cowboy_req:method(Req), Method = cowboy_req:method(Req),
#{echo := Echo} = cowboy_req:match_qs(Req, [echo]), #{echo := Echo} = cowboy_req:match_qs(Req, [echo]),
Req2 = echo(Method, Echo, Req), Req2 = echo(Method, Echo, Req),
{ok, Req2, State}. {ok, Req2, Opts}.
echo(<<"GET">>, undefined, Req) -> echo(<<"GET">>, undefined, Req) ->
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req); cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);

View file

@ -4,16 +4,12 @@
-module(toppage_handler). -module(toppage_handler).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
Method = cowboy_req:method(Req), Method = cowboy_req:method(Req),
HasBody = cowboy_req:has_body(Req), HasBody = cowboy_req:has_body(Req),
Req2 = maybe_echo(Method, HasBody, Req), Req2 = maybe_echo(Method, HasBody, Req),
{ok, Req2, State}. {ok, Req2, Opts}.
maybe_echo(<<"POST">>, true, Req) -> maybe_echo(<<"POST">>, true, Req) ->
{ok, PostVals, Req2} = cowboy_req:body_qs(Req), {ok, PostVals, Req2} = cowboy_req:body_qs(Req),

View file

@ -10,12 +10,12 @@ init(Req, Opts) ->
Headers = [{<<"content-type">>, <<"text/event-stream">>}], Headers = [{<<"content-type">>, <<"text/event-stream">>}],
Req2 = cowboy_req:chunked_reply(200, Headers, Req), Req2 = cowboy_req:chunked_reply(200, Headers, Req),
erlang:send_after(1000, self(), {message, "Tick"}), erlang:send_after(1000, self(), {message, "Tick"}),
{long_polling, Req2, Opts, 5000}. {cowboy_loop, Req2, Opts, 5000}.
info({message, Msg}, Req, State) -> info({message, Msg}, Req, State) ->
cowboy_req:chunk(["id: ", id(), "\ndata: ", Msg, "\n\n"], Req), cowboy_req:chunk(["id: ", id(), "\ndata: ", Msg, "\n\n"], Req),
erlang:send_after(1000, self(), {message, "Tick"}), erlang:send_after(1000, self(), {message, "Tick"}),
{loop, Req, State}. {ok, Req, State}.
id() -> id() ->
{Mega, Sec, Micro} = erlang:now(), {Mega, Sec, Micro} = erlang:now(),

View file

@ -4,13 +4,9 @@
-module(toppage_handler). -module(toppage_handler).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
Req2 = cowboy_req:reply(200, [ Req2 = cowboy_req:reply(200, [
{<<"content-type">>, <<"text/plain">>} {<<"content-type">>, <<"text/plain">>}
], <<"Hello world!">>, Req), ], <<"Hello world!">>, Req),
{ok, Req2, State}. {ok, Req2, Opts}.

View file

@ -9,7 +9,7 @@
-export([to_text/2]). -export([to_text/2]).
init(Req, Opts) -> init(Req, Opts) ->
{rest, Req, Opts}. {cowboy_rest, Req, Opts}.
is_authorized(Req, State) -> is_authorized(Req, State) ->
case cowboy_req:parse_header(<<"authorization">>, Req) of case cowboy_req:parse_header(<<"authorization">>, Req) of

View file

@ -10,7 +10,7 @@
-export([hello_to_text/2]). -export([hello_to_text/2]).
init(Req, Opts) -> init(Req, Opts) ->
{rest, Req, Opts}. {cowboy_rest, Req, Opts}.
content_types_provided(Req, State) -> content_types_provided(Req, State) ->
{[ {[

View file

@ -17,7 +17,7 @@
init(Req, Opts) -> init(Req, Opts) ->
random:seed(now()), random:seed(now()),
{rest, Req, Opts}. {cowboy_rest, Req, Opts}.
allowed_methods(Req, State) -> allowed_methods(Req, State) ->
{[<<"GET">>, <<"POST">>], Req, State}. {[<<"GET">>, <<"POST">>], Req, State}.

View file

@ -8,7 +8,7 @@
-export([streaming_csv/2]). -export([streaming_csv/2]).
init(Req, Table) -> init(Req, Table) ->
{rest, Req, Table}. {cowboy_rest, Req, Table}.
content_types_provided(Req, State) -> content_types_provided(Req, State) ->
{[ {[

View file

@ -4,13 +4,9 @@
-module(toppage_handler). -module(toppage_handler).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
Req2 = cowboy_req:reply(200, [ Req2 = cowboy_req:reply(200, [
{<<"content-type">>, <<"text/plain">>} {<<"content-type">>, <<"text/plain">>}
], <<"Hello world!">>, Req), ], <<"Hello world!">>, Req),
{ok, Req2, State}. {ok, Req2, Opts}.

View file

@ -4,16 +4,12 @@
-module(upload_handler). -module(upload_handler).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
{ok, Headers, Req2} = cowboy_req:part(Req), {ok, Headers, Req2} = cowboy_req:part(Req),
{ok, Data, Req3} = cowboy_req:part_body(Req2), {ok, Data, Req3} = cowboy_req:part_body(Req2),
{file, <<"inputfile">>, Filename, ContentType, _TE} {file, <<"inputfile">>, Filename, ContentType, _TE}
= cow_multipart:form_data(Headers), = cow_multipart:form_data(Headers),
io:format("Received file ~p of content-type ~p as follow:~n~p~n~n", io:format("Received file ~p of content-type ~p as follow:~n~p~n~n",
[Filename, ContentType, Data]), [Filename, ContentType, Data]),
{ok, Req3, State}. {ok, Req3, Opts}.

View file

@ -14,7 +14,7 @@
-export([list_html/2]). -export([list_html/2]).
init(Req, Paths) -> init(Req, Paths) ->
{rest, Req, Paths}. {cowboy_rest, Req, Paths}.
allowed_methods(Req, State) -> allowed_methods(Req, State) ->
{[<<"GET">>], Req, State}. {[<<"GET">>], Req, State}.

View file

@ -6,7 +6,7 @@
init(Req, Opts) -> init(Req, Opts) ->
erlang:start_timer(1000, self(), <<"Hello!">>), erlang:start_timer(1000, self(), <<"Hello!">>),
{ws, Req, Opts}. {cowboy_websocket, Req, Opts}.
websocket_handle({text, Msg}, Req, State) -> websocket_handle({text, Msg}, Req, State) ->
{reply, {text, << "That's what she said! ", Msg/binary >>}, Req, State}; {reply, {text, << "That's what she said! ", Msg/binary >>}, Req, State};

View file

@ -21,7 +21,15 @@
-behaviour(cowboy_middleware). -behaviour(cowboy_middleware).
-export([execute/2]). -export([execute/2]).
-export([terminate/5]). -export([terminate/4]).
-callback init(Req, any())
-> {ok | module(), Req, any()}
| {module(), Req, any(), hibernate}
| {module(), Req, any(), timeout()}
| {module(), Req, any(), timeout(), hibernate}
when Req::cowboy_req:req().
%% @todo optional -callback terminate(terminate_reason(), cowboy_req:req(), state()) -> ok.
-spec execute(Req, Env) -> {ok, Req, Env} -spec execute(Req, Env) -> {ok, Req, Env}
when Req::cowboy_req:req(), Env::cowboy_middleware:env(). when Req::cowboy_req:req(), Env::cowboy_middleware:env().
@ -29,21 +37,21 @@ execute(Req, Env) ->
{_, Handler} = lists:keyfind(handler, 1, Env), {_, Handler} = lists:keyfind(handler, 1, Env),
{_, HandlerOpts} = lists:keyfind(handler_opts, 1, Env), {_, HandlerOpts} = lists:keyfind(handler_opts, 1, Env),
try Handler:init(Req, HandlerOpts) of try Handler:init(Req, HandlerOpts) of
{http, Req2, State} -> {ok, Req2, State} ->
handle(Req2, Env, Handler, State); Result = terminate(normal, Req2, State, Handler),
{shutdown, Req2, State} -> {ok, Req2, [{result, Result}|Env]};
terminate(Req2, Env, Handler, State, {normal, shutdown});
{Mod, Req2, State} -> {Mod, Req2, State} ->
upgrade(Req2, Env, Handler, State, infinity, run, Mod); Mod:upgrade(Req2, Env, Handler, State, infinity, run);
{Mod, Req2, State, hibernate} -> {Mod, Req2, State, hibernate} ->
upgrade(Req2, Env, Handler, State, infinity, hibernate, Mod); Mod:upgrade(Req2, Env, Handler, State, infinity, hibernate);
{Mod, Req2, State, Timeout} -> {Mod, Req2, State, Timeout} ->
upgrade(Req2, Env, Handler, State, Timeout, run, Mod); Mod:upgrade(Req2, Env, Handler, State, Timeout, run);
{Mod, Req2, State, Timeout, hibernate} -> {Mod, Req2, State, Timeout, hibernate} ->
upgrade(Req2, Env, Handler, State, Timeout, hibernate, Mod) Mod:upgrade(Req2, Env, Handler, State, Timeout, hibernate)
catch Class:Reason -> catch Class:Reason ->
Stacktrace = erlang:get_stacktrace(), Stacktrace = erlang:get_stacktrace(),
cowboy_req:maybe_reply(Stacktrace, Req), cowboy_req:maybe_reply(Stacktrace, Req),
terminate({crash, Class, Reason}, Req, HandlerOpts, Handler),
erlang:Class([ erlang:Class([
{reason, Reason}, {reason, Reason},
{mfa, {Handler, init, 2}}, {mfa, {Handler, init, 2}},
@ -53,36 +61,9 @@ execute(Req, Env) ->
]) ])
end. end.
handle(Req, Env, Handler, State) -> -spec terminate(any(), Req, any(), module()) -> ok when Req::cowboy_req:req().
try Handler:handle(Req, State) of terminate(Reason, Req, State, Handler) ->
{ok, Req2, State2} -> case erlang:function_exported(Handler, terminate, 3) of
terminate(Req2, Env, Handler, State2, {normal, shutdown})
catch Class:Reason ->
Stacktrace = erlang:get_stacktrace(),
cowboy_req:maybe_reply(Stacktrace, Req),
_ = terminate(Req, Env, Handler, State, Reason),
erlang:Class([
{reason, Reason},
{mfa, {Handler, handle, 2}},
{stacktrace, Stacktrace},
{req, cowboy_req:to_list(Req)},
{state, State}
])
end.
upgrade(Req, Env, Handler, State, Timeout, Hibernate, Mod) ->
Mod2 = case Mod of
long_polling -> cowboy_long_polling;
rest -> cowboy_rest;
ws -> cowboy_websocket;
_ when Mod =/= http -> Mod
end,
Mod2:upgrade(Req, Env, Handler, State, Timeout, Hibernate).
-spec terminate(Req, Env, module(), any(), {normal, shutdown} | {error, atom()} | any())
-> {ok, Req, Env} when Req::cowboy_req:req(), Env::cowboy_middleware:env().
terminate(Req, Env, Handler, State, Reason) ->
Result = case erlang:function_exported(Handler, terminate, 3) of
true -> true ->
try try
Handler:terminate(Reason, cowboy_req:lock(Req), State) Handler:terminate(Reason, cowboy_req:lock(Req), State)
@ -98,5 +79,4 @@ terminate(Req, Env, Handler, State, Reason) ->
end; end;
false -> false ->
ok ok
end, end.
{ok, Req, [{result, Result}|Env]}.

View file

@ -1,36 +0,0 @@
%% Copyright (c) 2011-2014, Loïc Hoguin <essen@ninenines.eu>
%%
%% Permission to use, copy, modify, and/or distribute this software for any
%% purpose with or without fee is hereby granted, provided that the above
%% copyright notice and this permission notice appear in all copies.
%%
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-module(cowboy_http_handler).
-type opts() :: any().
-type state() :: any().
%% @todo see terminate
%-type terminate_reason() :: {normal, shutdown}
% | {normal, timeout} %% Only occurs in loop handlers.
% | {error, closed} %% Only occurs in loop handlers.
% | {error, overflow} %% Only occurs in loop handlers.
% | {error, atom()}.
-callback init(Req, opts())
-> {http, Req, state()}
| {long_polling | rest | ws | module(), Req, state()}
| {long_polling | rest | ws | module(), Req, state(), hibernate}
| {long_polling | rest | ws | module(), Req, state(), timeout()}
| {long_polling | rest | ws | module(), Req, state(), timeout(), hibernate}
| {shutdown, Req, state()}
when Req::cowboy_req:req().
-callback handle(Req, State) -> {ok, Req, State}
when Req::cowboy_req:req(), State::state().
%% @todo optional -callback terminate(terminate_reason(), cowboy_req:req(), state()) -> ok.

View file

@ -21,12 +21,25 @@
%% by default. This can be configured through the <em>loop_max_buffer</em> %% by default. This can be configured through the <em>loop_max_buffer</em>
%% environment value. The request will be terminated with an %% environment value. The request will be terminated with an
%% <em>{error, overflow}</em> reason if this threshold is reached. %% <em>{error, overflow}</em> reason if this threshold is reached.
-module(cowboy_long_polling). -module(cowboy_loop).
-behaviour(cowboy_sub_protocol). -behaviour(cowboy_sub_protocol).
-export([upgrade/6]). -export([upgrade/6]).
-export([loop/4]). -export([loop/4]).
-callback init(Req, any())
-> {ok | module(), Req, any()}
| {module(), Req, any(), hibernate}
| {module(), Req, any(), timeout()}
| {module(), Req, any(), timeout(), hibernate}
when Req::cowboy_req:req().
-callback info(any(), Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
| {shutdown, Req, State}
when Req::cowboy_req:req(), State::any().
%% @todo optional -callback terminate(terminate_reason(), cowboy_req:req(), state()) -> ok.
-record(state, { -record(state, {
env :: cowboy_middleware:env(), env :: cowboy_middleware:env(),
hibernate = false :: boolean(), hibernate = false :: boolean(),
@ -91,7 +104,7 @@ timeout(State=#state{timeout=Timeout,
-spec loop(Req, #state{}, module(), any()) -spec loop(Req, #state{}, module(), any())
-> {ok, Req, cowboy_middleware:env()} | {suspend, module(), atom(), [any()]} -> {ok, Req, cowboy_middleware:env()} | {suspend, module(), atom(), [any()]}
when Req::cowboy_req:req(). when Req::cowboy_req:req().
loop(Req, State=#state{env=Env, buffer_size=NbBytes, loop(Req, State=#state{buffer_size=NbBytes,
max_buffer=Threshold, timeout_ref=TRef, max_buffer=Threshold, timeout_ref=TRef,
resp_sent=RespSent}, Handler, HandlerState) -> resp_sent=RespSent}, Handler, HandlerState) ->
[Socket, Transport] = cowboy_req:get([socket, transport], Req), [Socket, Transport] = cowboy_req:get([socket, transport], Req),
@ -103,7 +116,7 @@ loop(Req, State=#state{env=Env, buffer_size=NbBytes,
_ = if RespSent -> ok; true -> _ = if RespSent -> ok; true ->
cowboy_req:reply(500, Req) cowboy_req:reply(500, Req)
end, end,
_ = cowboy_handler:terminate(Req, Env, Handler, HandlerState, {error, overflow}), cowboy_handler:terminate({error, overflow}, Req, HandlerState, Handler),
exit(normal); exit(normal);
true -> true ->
Req2 = cowboy_req:append_buffer(Data, Req), Req2 = cowboy_req:append_buffer(Data, Req),
@ -115,7 +128,7 @@ loop(Req, State=#state{env=Env, buffer_size=NbBytes,
{Error, Socket, Reason} -> {Error, Socket, Reason} ->
terminate(Req, State, Handler, HandlerState, {error, Reason}); terminate(Req, State, Handler, HandlerState, {error, Reason});
{timeout, TRef, ?MODULE} -> {timeout, TRef, ?MODULE} ->
after_loop(Req, State, Handler, HandlerState, {normal, timeout}); after_loop(Req, State, Handler, HandlerState, timeout);
{timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) -> {timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) ->
loop(Req, State, Handler, HandlerState); loop(Req, State, Handler, HandlerState);
Message -> Message ->
@ -133,21 +146,21 @@ loop(Req, State=#state{env=Env, buffer_size=NbBytes,
call(Req2, State, Handler, HandlerState, Message) call(Req2, State, Handler, HandlerState, Message)
end. end.
call(Req, State=#state{env=Env, resp_sent=RespSent}, call(Req, State=#state{resp_sent=RespSent},
Handler, HandlerState, Message) -> Handler, HandlerState, Message) ->
try Handler:info(Message, Req, HandlerState) of try Handler:info(Message, Req, HandlerState) of
{ok, Req2, HandlerState2} -> {ok, Req2, HandlerState2} ->
after_loop(Req2, State, Handler, HandlerState2, {normal, shutdown});
{loop, Req2, HandlerState2} ->
after_call(Req2, State, Handler, HandlerState2); after_call(Req2, State, Handler, HandlerState2);
{loop, Req2, HandlerState2, hibernate} -> {ok, Req2, HandlerState2, hibernate} ->
after_call(Req2, State#state{hibernate=true}, Handler, HandlerState2) after_call(Req2, State#state{hibernate=true}, Handler, HandlerState2);
{shutdown, Req2, HandlerState2} ->
after_loop(Req2, State, Handler, HandlerState2, shutdown)
catch Class:Reason -> catch Class:Reason ->
Stacktrace = erlang:get_stacktrace(), Stacktrace = erlang:get_stacktrace(),
if RespSent -> ok; true -> if RespSent -> ok; true ->
cowboy_req:maybe_reply(Stacktrace, Req) cowboy_req:maybe_reply(Stacktrace, Req)
end, end,
_ = cowboy_handler:terminate(Req, Env, Handler, HandlerState, {error, overflow}), cowboy_handler:terminate({crash, Class, Reason}, Req, HandlerState, Handler),
erlang:Class([ erlang:Class([
{reason, Reason}, {reason, Reason},
{mfa, {Handler, info, 3}}, {mfa, {Handler, info, 3}},
@ -180,7 +193,8 @@ terminate(Req, #state{env=Env, timeout_ref=TRef},
TRef -> erlang:cancel_timer(TRef) TRef -> erlang:cancel_timer(TRef)
end, end,
flush_timeouts(), flush_timeouts(),
cowboy_handler:terminate(Req, Env, Handler, HandlerState, Reason). Result = cowboy_handler:terminate(Reason, Req, HandlerState, Handler),
{ok, Req, [{result, Result}|Env]}.
flush_timeouts() -> flush_timeouts() ->
receive receive

View file

@ -1,39 +0,0 @@
%% Copyright (c) 2011-2014, Loïc Hoguin <essen@ninenines.eu>
%%
%% Permission to use, copy, modify, and/or distribute this software for any
%% purpose with or without fee is hereby granted, provided that the above
%% copyright notice and this permission notice appear in all copies.
%%
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-module(cowboy_loop_handler).
-type opts() :: any().
-type state() :: any().
%% @todo see terminate
%-type terminate_reason() :: {normal, shutdown}
% | {normal, timeout}
% | {error, closed}
% | {error, overflow}
% | {error, atom()}.
-callback init(Req, opts())
-> {http, Req, state()}
| {long_polling | rest | ws | module(), Req, state()}
| {long_polling | rest | ws | module(), Req, state(), hibernate}
| {long_polling | rest | ws | module(), Req, state(), timeout()}
| {long_polling | rest | ws | module(), Req, state(), timeout(), hibernate}
| {shutdown, Req, state()}
when Req::cowboy_req:req().
-callback info(any(), Req, State)
-> {ok, Req, State}
| {loop, Req, State}
| {loop, Req, State, hibernate}
when Req::cowboy_req:req(), State::state().
%% @todo optional -callback terminate(terminate_reason(), cowboy_req:req(), state()) -> ok.

View file

@ -19,6 +19,15 @@
-export([upgrade/6]). -export([upgrade/6]).
-callback init(Req, any())
-> {ok | module(), Req, any()}
| {module(), Req, any(), hibernate}
| {module(), Req, any(), timeout()}
| {module(), Req, any(), timeout(), hibernate}
when Req::cowboy_req:req().
%% @todo optional REST callbacks
%% @todo optional -callback terminate(terminate_reason(), cowboy_req:req(), state()) -> ok.
-record(state, { -record(state, {
env :: cowboy_middleware:env(), env :: cowboy_middleware:env(),
method = undefined :: binary(), method = undefined :: binary(),
@ -967,11 +976,11 @@ next(Req, State, StatusCode) when is_integer(StatusCode) ->
respond(Req, State, StatusCode) -> respond(Req, State, StatusCode) ->
terminate(cowboy_req:reply(StatusCode, Req), State). terminate(cowboy_req:reply(StatusCode, Req), State).
error_terminate(Req, State=#state{handler=Handler, handler_state=HandlerState}, error_terminate(Req, #state{handler=Handler, handler_state=HandlerState},
Class, Reason, Callback) -> Class, Reason, Callback) ->
_ = terminate(Req, State),
Stacktrace = erlang:get_stacktrace(), Stacktrace = erlang:get_stacktrace(),
cowboy_req:maybe_reply(Stacktrace, Req), cowboy_req:maybe_reply(Stacktrace, Req),
cowboy_handler:terminate({crash, Class, Reason}, Req, HandlerState, Handler),
erlang:Class([ erlang:Class([
{reason, Reason}, {reason, Reason},
{mfa, {Handler, Callback, 2}}, {mfa, {Handler, Callback, 2}},
@ -981,4 +990,5 @@ error_terminate(Req, State=#state{handler=Handler, handler_state=HandlerState},
]). ]).
terminate(Req, #state{env=Env, handler=Handler, handler_state=HandlerState}) -> terminate(Req, #state{env=Env, handler=Handler, handler_state=HandlerState}) ->
cowboy_handler:terminate(Req, Env, Handler, HandlerState, {normal, shutdown}). Result = cowboy_handler:terminate(normal, Req, HandlerState, Handler),
{ok, Req, [{result, Result}|Env]}.

View file

@ -42,7 +42,7 @@
%% If the handler is configured to manage a directory, check that the %% If the handler is configured to manage a directory, check that the
%% requested file is inside the configured directory. %% requested file is inside the configured directory.
-spec init(Req, opts()) -> {rest, Req, error | state()} when Req::cowboy_req:req(). -spec init(Req, opts()) -> {cowboy_rest, Req, error | state()} when Req::cowboy_req:req().
init(Req, {Name, Path}) -> init(Req, {Name, Path}) ->
init_opts(Req, {Name, Path, []}); init_opts(Req, {Name, Path, []});
init(Req, {Name, App, Path}) init(Req, {Name, App, Path})
@ -87,7 +87,7 @@ init_dir(Req, Path, Extra) ->
<< Dir:Len/binary, $/, _/binary >> -> << Dir:Len/binary, $/, _/binary >> ->
init_info(Req, Filepath, Extra); init_info(Req, Filepath, Extra);
_ -> _ ->
{rest, Req, error} {cowboy_rest, Req, error}
end. end.
fullpath(Path) -> fullpath(Path) ->
@ -105,7 +105,7 @@ fullpath([Segment|Tail], Acc) ->
init_info(Req, Path, Extra) -> init_info(Req, Path, Extra) ->
Info = file:read_file_info(Path, [{time, universal}]), Info = file:read_file_info(Path, [{time, universal}]),
{rest, Req, {Path, Info, Extra}}. {cowboy_rest, Req, {Path, Info, Extra}}.
-ifdef(TEST). -ifdef(TEST).
fullpath_test_() -> fullpath_test_() ->

View file

@ -33,8 +33,32 @@
-type frag_state() :: undefined -type frag_state() :: undefined
| {nofin, opcode(), binary()} | {fin, opcode(), binary()}. | {nofin, opcode(), binary()} | {fin, opcode(), binary()}.
-type rsv() :: << _:3 >>. -type rsv() :: << _:3 >>.
-type terminate_reason() :: {normal | error | remote, atom()} -type terminate_reason() :: normal | shutdown | timeout
| {remote, close_code(), binary()}. | remote | {remote, close_code(), binary()}
| {error, badencoding | badframe | closed | atom()}
| {crash, error | exit | throw, any()}.
-callback init(Req, any())
-> {ok | module(), Req, any()}
| {module(), Req, any(), hibernate}
| {module(), Req, any(), timeout()}
| {module(), Req, any(), timeout(), hibernate}
when Req::cowboy_req:req().
-callback websocket_handle({text | binary | ping | pong, binary()}, Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
| {reply, frame() | [frame()], Req, State}
| {reply, frame() | [frame()], Req, State, hibernate}
| {shutdown, Req, State}
when Req::cowboy_req:req(), State::any().
-callback websocket_info(any(), Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
| {reply, frame() | [frame()], Req, State}
| {reply, frame() | [frame()], Req, State, hibernate}
| {shutdown, Req, State}
when Req::cowboy_req:req(), State::any().
%% @todo optional -callback terminate(terminate_reason(), cowboy_req:req(), state()) -> ok.
-record(state, { -record(state, {
env :: cowboy_middleware:env(), env :: cowboy_middleware:env(),
@ -181,7 +205,7 @@ handler_loop(State=#state{socket=Socket, messages={OK, Closed, Error},
{Error, Socket, Reason} -> {Error, Socket, Reason} ->
handler_terminate(State, Req, HandlerState, {error, Reason}); handler_terminate(State, Req, HandlerState, {error, Reason});
{timeout, TRef, ?MODULE} -> {timeout, TRef, ?MODULE} ->
websocket_close(State, Req, HandlerState, {normal, timeout}); websocket_close(State, Req, HandlerState, timeout);
{timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) -> {timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) ->
handler_loop(State, Req, HandlerState, SoFar); handler_loop(State, Req, HandlerState, SoFar);
Message -> Message ->
@ -487,7 +511,7 @@ websocket_payload_loop(State=#state{socket=Socket, transport=Transport,
{Error, Socket, Reason} -> {Error, Socket, Reason} ->
handler_terminate(State, Req, HandlerState, {error, Reason}); handler_terminate(State, Req, HandlerState, {error, Reason});
{timeout, TRef, ?MODULE} -> {timeout, TRef, ?MODULE} ->
websocket_close(State, Req, HandlerState, {normal, timeout}); websocket_close(State, Req, HandlerState, timeout);
{timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) -> {timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) ->
websocket_payload_loop(State, Req, HandlerState, websocket_payload_loop(State, Req, HandlerState,
Opcode, Len, MaskKey, Unmasked, UnmaskedLen, Rsv); Opcode, Len, MaskKey, Unmasked, UnmaskedLen, Rsv);
@ -524,7 +548,7 @@ websocket_dispatch(State, Req, HandlerState, RemainingData, 2, Payload) ->
websocket_handle, {binary, Payload}, fun websocket_data/4); websocket_handle, {binary, Payload}, fun websocket_data/4);
%% Close control frame. %% Close control frame.
websocket_dispatch(State, Req, HandlerState, _RemainingData, 8, <<>>) -> websocket_dispatch(State, Req, HandlerState, _RemainingData, 8, <<>>) ->
websocket_close(State, Req, HandlerState, {remote, closed}); websocket_close(State, Req, HandlerState, remote);
websocket_dispatch(State, Req, HandlerState, _RemainingData, 8, websocket_dispatch(State, Req, HandlerState, _RemainingData, 8,
<< Code:16, Payload/bits >>) -> << Code:16, Payload/bits >>) ->
websocket_close(State, Req, HandlerState, {remote, Code, Payload}); websocket_close(State, Req, HandlerState, {remote, Code, Payload});
@ -558,8 +582,7 @@ handler_call(State=#state{handler=Handler}, Req, HandlerState,
{ok, State2} -> {ok, State2} ->
NextState(State2, Req2, HandlerState2, RemainingData); NextState(State2, Req2, HandlerState2, RemainingData);
{shutdown, State2} -> {shutdown, State2} ->
handler_terminate(State2, Req2, HandlerState2, handler_terminate(State2, Req2, HandlerState2, shutdown);
{normal, shutdown});
{{error, _} = Error, State2} -> {{error, _} = Error, State2} ->
handler_terminate(State2, Req2, HandlerState2, Error) handler_terminate(State2, Req2, HandlerState2, Error)
end; end;
@ -570,8 +593,7 @@ handler_call(State=#state{handler=Handler}, Req, HandlerState,
NextState(State2#state{hibernate=true}, NextState(State2#state{hibernate=true},
Req2, HandlerState2, RemainingData); Req2, HandlerState2, RemainingData);
{shutdown, State2} -> {shutdown, State2} ->
handler_terminate(State2, Req2, HandlerState2, handler_terminate(State2, Req2, HandlerState2, shutdown);
{normal, shutdown});
{{error, _} = Error, State2} -> {{error, _} = Error, State2} ->
handler_terminate(State2, Req2, HandlerState2, Error) handler_terminate(State2, Req2, HandlerState2, Error)
end; end;
@ -580,8 +602,7 @@ handler_call(State=#state{handler=Handler}, Req, HandlerState,
{ok, State2} -> {ok, State2} ->
NextState(State2, Req2, HandlerState2, RemainingData); NextState(State2, Req2, HandlerState2, RemainingData);
{shutdown, State2} -> {shutdown, State2} ->
handler_terminate(State2, Req2, HandlerState2, handler_terminate(State2, Req2, HandlerState2, shutdown);
{normal, shutdown});
{{error, _} = Error, State2} -> {{error, _} = Error, State2} ->
handler_terminate(State2, Req2, HandlerState2, Error) handler_terminate(State2, Req2, HandlerState2, Error)
end; end;
@ -591,15 +612,14 @@ handler_call(State=#state{handler=Handler}, Req, HandlerState,
NextState(State2#state{hibernate=true}, NextState(State2#state{hibernate=true},
Req2, HandlerState2, RemainingData); Req2, HandlerState2, RemainingData);
{shutdown, State2} -> {shutdown, State2} ->
handler_terminate(State2, Req2, HandlerState2, handler_terminate(State2, Req2, HandlerState2, shutdown);
{normal, shutdown});
{{error, _} = Error, State2} -> {{error, _} = Error, State2} ->
handler_terminate(State2, Req2, HandlerState2, Error) handler_terminate(State2, Req2, HandlerState2, Error)
end; end;
{shutdown, Req2, HandlerState2} -> {shutdown, Req2, HandlerState2} ->
websocket_close(State, Req2, HandlerState2, {normal, shutdown}) websocket_close(State, Req2, HandlerState2, shutdown)
catch Class:Reason -> catch Class:Reason ->
_ = websocket_close(State, Req, HandlerState, {error, handler}), _ = websocket_close(State, Req, HandlerState, {crash, Class, Reason}),
erlang:Class([ erlang:Class([
{reason, Reason}, {reason, Reason},
{mfa, {Handler, Callback, 3}}, {mfa, {Handler, Callback, 3}},
@ -696,15 +716,15 @@ websocket_send_many([Frame|Tail], State) ->
websocket_close(State=#state{socket=Socket, transport=Transport}, websocket_close(State=#state{socket=Socket, transport=Transport},
Req, HandlerState, Reason) -> Req, HandlerState, Reason) ->
case Reason of case Reason of
{normal, _} -> Normal when Normal =:= shutdown; Normal =:= timeout ->
Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>); Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>);
{error, badframe} -> {error, badframe} ->
Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1002:16 >>); Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1002:16 >>);
{error, badencoding} -> {error, badencoding} ->
Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1007:16 >>); Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1007:16 >>);
{error, handler} -> {crash, _, _} ->
Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1011:16 >>); Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1011:16 >>);
{remote, closed} -> remote ->
Transport:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>); Transport:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>);
{remote, Code, _} -> {remote, Code, _} ->
Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, Code:16 >>) Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, Code:16 >>)
@ -716,5 +736,5 @@ websocket_close(State=#state{socket=Socket, transport=Transport},
when Req::cowboy_req:req(). when Req::cowboy_req:req().
handler_terminate(#state{env=Env, handler=Handler}, handler_terminate(#state{env=Env, handler=Handler},
Req, HandlerState, Reason) -> Req, HandlerState, Reason) ->
_ = cowboy_handler:terminate(Req, Env, Handler, HandlerState, Reason), cowboy_handler:terminate(Reason, Req, HandlerState, Handler),
{ok, Req, [{result, closed}|Env]}. {ok, Req, [{result, closed}|Env]}.

View file

@ -1,51 +0,0 @@
%% Copyright (c) 2011-2014, Loïc Hoguin <essen@ninenines.eu>
%%
%% Permission to use, copy, modify, and/or distribute this software for any
%% purpose with or without fee is hereby granted, provided that the above
%% copyright notice and this permission notice appear in all copies.
%%
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-module(cowboy_websocket_handler).
-type opts() :: any().
-type state() :: any().
%% @todo see terminate
%-type terminate_reason() :: {normal, shutdown}
% | {normal, timeout}
% | {error, closed}
% | {remote, closed}
% | {remote, cowboy_websocket:close_code(), binary()}
% | {error, badencoding}
% | {error, badframe}
% | {error, atom()}.
-callback init(Req, opts())
-> {http, Req, state()}
| {long_polling | rest | ws | module(), Req, state()}
| {long_polling | rest | ws | module(), Req, state(), hibernate}
| {long_polling | rest | ws | module(), Req, state(), timeout()}
| {long_polling | rest | ws | module(), Req, state(), timeout(), hibernate}
| {shutdown, Req, state()}
when Req::cowboy_req:req().
-callback websocket_handle({text | binary | ping | pong, binary()}, Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
| {reply, cowboy_websocket:frame() | [cowboy_websocket:frame()], Req, State}
| {reply, cowboy_websocket:frame() | [cowboy_websocket:frame()], Req, State, hibernate}
| {shutdown, Req, State}
when Req::cowboy_req:req(), State::state().
-callback websocket_info(any(), Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
| {reply, cowboy_websocket:frame() | [cowboy_websocket:frame()], Req, State}
| {reply, cowboy_websocket:frame() | [cowboy_websocket:frame()], Req, State, hibernate}
| {shutdown, Req, State}
when Req::cowboy_req:req(), State::state().
%% @todo optional -callback terminate(terminate_reason(), cowboy_req:req(), state()) -> ok.

View file

@ -11,15 +11,15 @@
init(Req, _) -> init(Req, _) ->
erlang:send_after(200, self(), timeout), erlang:send_after(200, self(), timeout),
{long_polling, Req, 2, 5000, hibernate}. {cowboy_loop, Req, 2, 5000, hibernate}.
info(timeout, Req, 0) -> info(timeout, Req, 0) ->
{ok, cowboy_req:reply(102, Req), 0}; {shutdown, cowboy_req:reply(102, Req), 0};
info(timeout, Req, Count) -> info(timeout, Req, Count) ->
erlang:send_after(200, self(), timeout), erlang:send_after(200, self(), timeout),
{loop, Req, Count - 1, hibernate}. {ok, Req, Count - 1, hibernate}.
terminate({normal, shutdown}, _, 0) -> terminate(shutdown, _, 0) ->
ok; ok;
terminate({error, overflow}, _, _) -> terminate({error, overflow}, _, _) ->
ok. ok.

View file

@ -11,12 +11,12 @@
init(Req, _) -> init(Req, _) ->
self() ! timeout, self() ! timeout,
{long_polling, Req, undefined, 5000, hibernate}. {cowboy_loop, Req, undefined, 5000, hibernate}.
info(timeout, Req, State) -> info(timeout, Req, State) ->
{ok, Body, Req2} = cowboy_req:body(Req), {ok, Body, Req2} = cowboy_req:body(Req),
100000 = byte_size(Body), 100000 = byte_size(Body),
{ok, cowboy_req:reply(200, Req2), State}. {shutdown, cowboy_req:reply(200, Req2), State}.
terminate({normal, shutdown}, _, _) -> terminate(shutdown, _, _) ->
ok. ok.

View file

@ -1,7 +1,7 @@
%% This module implements a loop handler that sends %% This module implements a loop handler that sends
%% itself a timeout that will intentionally arrive %% itself a timeout that will intentionally arrive
%% too late, as it configures itself to only wait %% too late, as it configures itself to only wait
%% 200ms before closing the connection in init/3. %% 200ms before closing the connection in init/2.
%% This results in a 204 reply being sent back by Cowboy. %% This results in a 204 reply being sent back by Cowboy.
-module(loop_handler_timeout_h). -module(loop_handler_timeout_h).
@ -12,10 +12,10 @@
init(Req, _) -> init(Req, _) ->
erlang:send_after(1000, self(), timeout), erlang:send_after(1000, self(), timeout),
{long_polling, Req, undefined, 200, hibernate}. {cowboy_loop, Req, undefined, 200, hibernate}.
info(timeout, Req, State) -> info(timeout, Req, State) ->
{ok, cowboy_req:reply(500, Req), State}. {shutdown, cowboy_req:reply(500, Req), State}.
terminate({normal, timeout}, _, _) -> terminate(timeout, _, _) ->
ok. ok.

View file

@ -142,7 +142,6 @@ init_dispatch(Config) ->
{"localhost", [ {"localhost", [
{"/chunked_response", http_chunked, []}, {"/chunked_response", http_chunked, []},
{"/streamed_response", http_streamed, []}, {"/streamed_response", http_streamed, []},
{"/init_shutdown", http_init_shutdown, []},
{"/headers/dupe", http_handler, {"/headers/dupe", http_handler,
[{headers, [{<<"connection">>, <<"close">>}]}]}, [{headers, [{<<"connection">>, <<"close">>}]}]},
{"/set_resp/header", http_set_resp, {"/set_resp/header", http_set_resp,
@ -292,9 +291,7 @@ check_status(Config) ->
{403, "/static/unreadable"}, {403, "/static/unreadable"},
{404, "/not/found"}, {404, "/not/found"},
{404, "/static/not_found"}, {404, "/static/not_found"},
{500, "/handler_errors?case=handle_before_reply"}, {500, "/handler_errors?case=init_before_reply"}
{500, "/handler_errors?case=init_before_reply"},
{666, "/init_shutdown"}
], ],
_ = [{Status, URL} = begin _ = [{Status, URL} = begin
Ret = do_get(URL, Config), Ret = do_get(URL, Config),
@ -342,40 +339,12 @@ echo_body_qs_max_length(Config) ->
{response, nofin, 413, _} = gun:await(ConnPid, Ref), {response, nofin, 413, _} = gun:await(ConnPid, Ref),
ok. ok.
error_chain_handle_after_reply(Config) ->
{ConnPid, MRef} = gun_monitor_open(Config),
Ref1 = gun:get(ConnPid, "/"),
Ref2 = gun:get(ConnPid, "/handler_errors?case=handle_after_reply"),
{response, nofin, 200, _} = gun:await(ConnPid, Ref1, MRef),
{response, nofin, 200, _} = gun:await(ConnPid, Ref2, MRef),
gun_is_gone(ConnPid, MRef).
error_chain_handle_before_reply(Config) ->
{ConnPid, MRef} = gun_monitor_open(Config),
Ref1 = gun:get(ConnPid, "/"),
Ref2 = gun:get(ConnPid, "/handler_errors?case=handle_before_reply"),
{response, nofin, 200, _} = gun:await(ConnPid, Ref1, MRef),
{response, fin, 500, _} = gun:await(ConnPid, Ref2, MRef),
gun_is_gone(ConnPid, MRef).
error_handle_after_reply(Config) ->
{ConnPid, MRef} = gun_monitor_open(Config),
Ref = gun:get(ConnPid, "/handler_errors?case=handle_after_reply"),
{response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
gun_is_gone(ConnPid, MRef).
error_init_after_reply(Config) -> error_init_after_reply(Config) ->
{ConnPid, MRef} = gun_monitor_open(Config), {ConnPid, MRef} = gun_monitor_open(Config),
Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"), Ref = gun:get(ConnPid, "/handler_errors?case=init_after_reply"),
{response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef), {response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
gun_is_gone(ConnPid, MRef). gun_is_gone(ConnPid, MRef).
error_init_reply_handle_error(Config) ->
{ConnPid, MRef} = gun_monitor_open(Config),
Ref = gun:get(ConnPid, "/handler_errors?case=init_reply_handle_error"),
{response, nofin, 200, _} = gun:await(ConnPid, Ref, MRef),
gun_is_gone(ConnPid, MRef).
headers_dupe(Config) -> headers_dupe(Config) ->
{ConnPid, MRef} = gun_monitor_open(Config), {ConnPid, MRef} = gun_monitor_open(Config),
Ref = gun:get(ConnPid, "/headers/dupe"), Ref = gun:get(ConnPid, "/headers/dupe"),

View file

@ -3,15 +3,11 @@
-module(http_body_qs). -module(http_body_qs).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
Method = cowboy_req:method(Req), Method = cowboy_req:method(Req),
HasBody = cowboy_req:has_body(Req), HasBody = cowboy_req:has_body(Req),
{ok, maybe_echo(Method, HasBody, Req), State}. {ok, maybe_echo(Method, HasBody, Req), Opts}.
maybe_echo(<<"POST">>, true, Req) -> maybe_echo(<<"POST">>, true, Req) ->
case cowboy_req:body_qs(Req) of case cowboy_req:body_qs(Req) of

View file

@ -3,15 +3,11 @@
-module(http_chunked). -module(http_chunked).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
Req2 = cowboy_req:chunked_reply(200, Req), Req2 = cowboy_req:chunked_reply(200, Req),
timer:sleep(100), timer:sleep(100),
cowboy_req:chunk("chunked_handler\r\n", Req2), cowboy_req:chunk("chunked_handler\r\n", Req2),
timer:sleep(100), timer:sleep(100),
cowboy_req:chunk("works fine!", Req2), cowboy_req:chunk("works fine!", Req2),
{ok, Req2, State}. {ok, Req2, Opts}.

View file

@ -3,18 +3,14 @@
-module(http_echo_body). -module(http_echo_body).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
true = cowboy_req:has_body(Req), true = cowboy_req:has_body(Req),
Req3 = case cowboy_req:body(Req, [{length, 1000000}]) of Req3 = case cowboy_req:body(Req, [{length, 1000000}]) of
{ok, Body, Req2} -> handle_body(Req2, Body); {ok, Body, Req2} -> handle_body(Req2, Body);
{more, _, Req2} -> handle_badlength(Req2) {more, _, Req2} -> handle_badlength(Req2)
end, end,
{ok, Req3, State}. {ok, Req3, Opts}.
handle_badlength(Req) -> handle_badlength(Req) ->
cowboy_req:reply(413, [], <<"Request entity too large">>, Req). cowboy_req:reply(413, [], <<"Request entity too large">>, Req).

View file

@ -3,7 +3,6 @@
-module(http_errors). -module(http_errors).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, _Opts) -> init(Req, _Opts) ->
#{'case' := Case} = cowboy_req:match_qs(Req, ['case']), #{'case' := Case} = cowboy_req:match_qs(Req, ['case']),
@ -15,22 +14,4 @@ case_init(<<"init_before_reply">> = Case, _Req) ->
case_init(<<"init_after_reply">> = Case, Req) -> case_init(<<"init_after_reply">> = Case, Req) ->
cowboy_error_h:ignore(?MODULE, case_init, 2), cowboy_error_h:ignore(?MODULE, case_init, 2),
_ = cowboy_req:reply(200, [], "http_handler_crashes", Req), _ = cowboy_req:reply(200, [], "http_handler_crashes", Req),
error(Case);
case_init(<<"init_reply_handle_error">> = Case, Req) ->
Req1 = cowboy_req:reply(200, [], "http_handler_crashes", Req),
{http, Req1, Case};
case_init(<<"handle_before_reply">> = Case, Req) ->
{http, Req, Case};
case_init(<<"handle_after_reply">> = Case, Req) ->
{http, Req, Case}.
handle(_Req, <<"init_reply_handle_error">> = Case) ->
cowboy_error_h:ignore(?MODULE, handle, 2),
error(Case);
handle(_Req, <<"handle_before_reply">> = Case) ->
cowboy_error_h:ignore(?MODULE, handle, 2),
error(Case);
handle(Req, <<"handle_after_reply">> = Case) ->
cowboy_error_h:ignore(?MODULE, handle, 2),
_ = cowboy_req:reply(200, [], "http_handler_crashes", Req),
error(Case). error(Case).

View file

@ -3,12 +3,8 @@
-module(http_handler). -module(http_handler).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}. Headers = proplists:get_value(headers, Opts, []),
Body = proplists:get_value(body, Opts, "http_handler"),
handle(Req, State) -> {ok, cowboy_req:reply(200, Headers, Body, Req), Opts}.
Headers = proplists:get_value(headers, State, []),
Body = proplists:get_value(body, State, "http_handler"),
{ok, cowboy_req:reply(200, Headers, Body, Req), State}.

View file

@ -1,10 +0,0 @@
%% Feel free to use, reuse and abuse the code in this file.
-module(http_init_shutdown).
-export([init/2]).
init(Req, _) ->
Req2 = cowboy_req:reply(<<"666 Init Shutdown Testing">>,
[{<<"connection">>, <<"close">>}], Req),
{shutdown, Req2, undefined}.

View file

@ -9,7 +9,7 @@
init(Req, _) -> init(Req, _) ->
receive after 100 -> ok end, receive after 100 -> ok end,
self() ! stream, self() ! stream,
{long_polling, Req, undefined, 100}. {cowboy_loop, Req, undefined, 100}.
info(stream, Req, undefined) -> info(stream, Req, undefined) ->
stream(Req, 1, <<>>). stream(Req, 1, <<>>).
@ -17,7 +17,7 @@ info(stream, Req, undefined) ->
stream(Req, ID, Acc) -> stream(Req, ID, Acc) ->
case cowboy_req:body(Req) of case cowboy_req:body(Req) of
{ok, <<>>, Req2} -> {ok, <<>>, Req2} ->
{ok, cowboy_req:reply(200, Req2), undefined}; {shutdown, cowboy_req:reply(200, Req2), undefined};
{_, Data, Req2} -> {_, Data, Req2} ->
parse_id(Req2, ID, << Acc/binary, Data/binary >>) parse_id(Req2, ID, << Acc/binary, Data/binary >>)
end. end.
@ -30,5 +30,5 @@ parse_id(Req, ID, Data) ->
stream(Req, ID, Data) stream(Req, ID, Data)
end. end.
terminate({normal, shutdown}, _, _) -> terminate(shutdown, _, _) ->
ok. ok.

View file

@ -3,14 +3,10 @@
-module(http_multipart). -module(http_multipart).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
{Result, Req2} = acc_multipart(Req, []), {Result, Req2} = acc_multipart(Req, []),
{ok, cowboy_req:reply(200, [], term_to_binary(Result), Req2), State}. {ok, cowboy_req:reply(200, [], term_to_binary(Result), Req2), Opts}.
acc_multipart(Req, Acc) -> acc_multipart(Req, Acc) ->
case cowboy_req:part(Req) of case cowboy_req:part(Req) of

View file

@ -3,14 +3,10 @@
-module(http_multipart_stream). -module(http_multipart_stream).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
Req2 = multipart(Req), Req2 = multipart(Req),
{ok, cowboy_req:reply(200, Req2), State}. {ok, cowboy_req:reply(200, Req2), Opts}.
multipart(Req) -> multipart(Req) ->
case cowboy_req:part(Req) of case cowboy_req:part(Req) of

View file

@ -5,15 +5,11 @@
-module(http_req_attr). -module(http_req_attr).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
#{attr := Attr} = cowboy_req:match_qs(Req, [attr]), #{attr := Attr} = cowboy_req:match_qs(Req, [attr]),
<<"host_and_port">> = Attr, <<"host_and_port">> = Attr,
Host = cowboy_req:host(Req), Host = cowboy_req:host(Req),
Port = cowboy_req:port(Req), Port = cowboy_req:port(Req),
Value = [Host, "\n", integer_to_list(Port)], Value = [Host, "\n", integer_to_list(Port)],
{ok, cowboy_req:reply(200, [], Value, Req), State}. {ok, cowboy_req:reply(200, [], Value, Req), Opts}.

View file

@ -3,7 +3,6 @@
-module(http_set_resp). -module(http_set_resp).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
Headers = proplists:get_value(headers, Opts, []), Headers = proplists:get_value(headers, Opts, []),
@ -14,16 +13,13 @@ init(Req, Opts) ->
Req3 = cowboy_req:set_resp_body(Body, Req2), Req3 = cowboy_req:set_resp_body(Body, Req2),
Req4 = cowboy_req:set_resp_header(<<"x-cowboy-test">>, <<"ok">>, Req3), Req4 = cowboy_req:set_resp_header(<<"x-cowboy-test">>, <<"ok">>, Req3),
Req5 = cowboy_req:set_resp_cookie(<<"cake">>, <<"lie">>, [], Req4), Req5 = cowboy_req:set_resp_cookie(<<"cake">>, <<"lie">>, [], Req4),
{http, Req5, undefined}. case cowboy_req:has_resp_header(<<"x-cowboy-test">>, Req5) of
false -> {ok, Req5, Opts};
handle(Req, State) ->
case cowboy_req:has_resp_header(<<"x-cowboy-test">>, Req) of
false -> {ok, Req, State};
true -> true ->
case cowboy_req:has_resp_body(Req) of case cowboy_req:has_resp_body(Req5) of
false -> false ->
{ok, Req, State}; {ok, Req5, Opts};
true -> true ->
{ok, cowboy_req:reply(200, Req), State} {ok, cowboy_req:reply(200, Req5), Opts}
end end
end. end.

View file

@ -3,14 +3,10 @@
-module(http_stream_body). -module(http_stream_body).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}. Body = proplists:get_value(body, Opts, "http_handler_stream_body"),
Reply = proplists:get_value(reply, Opts),
handle(Req, State) ->
Body = proplists:get_value(body, State, "http_handler_stream_body"),
Reply = proplists:get_value(reply, State),
SFun = fun(Socket, Transport) -> Transport:send(Socket, Body) end, SFun = fun(Socket, Transport) -> Transport:send(Socket, Body) end,
Req2 = case Reply of Req2 = case Reply of
set_resp -> set_resp ->
@ -23,4 +19,4 @@ handle(Req, State) ->
SFun2 = fun(SendFun) -> lists:foreach(SendFun, Body) end, SFun2 = fun(SendFun) -> lists:foreach(SendFun, Body) end,
cowboy_req:set_resp_body_fun(chunked, SFun2, Req) cowboy_req:set_resp_body_fun(chunked, SFun2, Req)
end, end,
{ok, cowboy_req:reply(200, Req2), State}. {ok, cowboy_req:reply(200, Req2), Opts}.

View file

@ -3,16 +3,12 @@
-module(http_streamed). -module(http_streamed).
-export([init/2]). -export([init/2]).
-export([handle/2]).
init(Req, Opts) -> init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
Req2 = cowboy_req:set([{resp_state, waiting_stream}], Req), Req2 = cowboy_req:set([{resp_state, waiting_stream}], Req),
Req3 = cowboy_req:chunked_reply(200, Req2), Req3 = cowboy_req:chunked_reply(200, Req2),
timer:sleep(100), timer:sleep(100),
cowboy_req:chunk("streamed_handler\r\n", Req3), cowboy_req:chunk("streamed_handler\r\n", Req3),
timer:sleep(100), timer:sleep(100),
cowboy_req:chunk("works fine!", Req3), cowboy_req:chunk("works fine!", Req3),
{ok, Req3, State}. {ok, Req3, Opts}.

View file

@ -3,4 +3,4 @@
-export([init/2]). -export([init/2]).
init(Req, Opts) -> init(Req, Opts) ->
{rest, Req, Opts}. {cowboy_rest, Req, Opts}.

View file

@ -7,7 +7,7 @@
-export([last_modified/2]). -export([last_modified/2]).
init(Req, Opts) -> init(Req, Opts) ->
{rest, Req, Opts}. {cowboy_rest, Req, Opts}.
content_types_provided(Req, State) -> content_types_provided(Req, State) ->
{[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}.

View file

@ -6,7 +6,7 @@
-export([expires/2]). -export([expires/2]).
init(Req, Opts) -> init(Req, Opts) ->
{rest, Req, Opts}. {cowboy_rest, Req, Opts}.
content_types_provided(Req, State) -> content_types_provided(Req, State) ->
{[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}.

View file

@ -9,7 +9,7 @@
-export([from_text/2]). -export([from_text/2]).
init(Req, [Forbidden]) -> init(Req, [Forbidden]) ->
{rest, Req, Forbidden}. {cowboy_rest, Req, Forbidden}.
allowed_methods(Req, State) -> allowed_methods(Req, State) ->
{[<<"GET">>, <<"HEAD">>, <<"POST">>], Req, State}. {[<<"GET">>, <<"HEAD">>, <<"POST">>], Req, State}.

View file

@ -6,7 +6,7 @@
-export([content_types_provided/2]). -export([content_types_provided/2]).
init(Req, Opts) -> init(Req, Opts) ->
{rest, Req, Opts}. {cowboy_rest, Req, Opts}.
allowed_methods(Req, State) -> allowed_methods(Req, State) ->
{[<<"GET">>, <<"PUT">>], Req, State}. {[<<"GET">>, <<"PUT">>], Req, State}.

View file

@ -6,7 +6,7 @@
-export([get_text_plain/2]). -export([get_text_plain/2]).
init(Req, Opts) -> init(Req, Opts) ->
{rest, Req, Opts}. {cowboy_rest, Req, Opts}.
allowed_methods(Req, State) -> allowed_methods(Req, State) ->
{[<<"GET">>, <<"HEAD">>, <<"DELETE">>], Req, State}. {[<<"GET">>, <<"HEAD">>, <<"DELETE">>], Req, State}.

View file

@ -8,7 +8,7 @@
-export([put_text_plain/2]). -export([put_text_plain/2]).
init(Req, Opts) -> init(Req, Opts) ->
{rest, Req, Opts}. {cowboy_rest, Req, Opts}.
allowed_methods(Req, State) -> allowed_methods(Req, State) ->
{[<<"GET">>, <<"PUT">>], Req, State}. {[<<"GET">>, <<"PUT">>], Req, State}.

View file

@ -8,7 +8,7 @@
-export([patch_text_plain/2]). -export([patch_text_plain/2]).
init(Req, Opts) -> init(Req, Opts) ->
{rest, Req, Opts}. {cowboy_rest, Req, Opts}.
allowed_methods(Req, State) -> allowed_methods(Req, State) ->
{[<<"HEAD">>, <<"GET">>, <<"PATCH">>], Req, State}. {[<<"HEAD">>, <<"GET">>, <<"PATCH">>], Req, State}.

View file

@ -6,7 +6,7 @@
-export([from_text/2]). -export([from_text/2]).
init(Req, Opts) -> init(Req, Opts) ->
{rest, Req, Opts}. {cowboy_rest, Req, Opts}.
allowed_methods(Req, State) -> allowed_methods(Req, State) ->
{[<<"POST">>], Req, State}. {[<<"POST">>], Req, State}.

View file

@ -6,7 +6,7 @@
-export([from_text/2]). -export([from_text/2]).
init(Req, Opts) -> init(Req, Opts) ->
{rest, Req, Opts}. {cowboy_rest, Req, Opts}.
allowed_methods(Req, State) -> allowed_methods(Req, State) ->
{[<<"POST">>], Req, State}. {[<<"POST">>], Req, State}.

View file

@ -6,7 +6,7 @@
-export([get_text_plain/2]). -export([get_text_plain/2]).
init(Req, Opts) -> init(Req, Opts) ->
{rest, Req, Opts}. {cowboy_rest, Req, Opts}.
generate_etag(Req, State) -> generate_etag(Req, State) ->
#{type := Type} = cowboy_req:match_qs(Req, [type]), #{type := Type} = cowboy_req:match_qs(Req, [type]),

View file

@ -5,7 +5,7 @@
-export([get_text_plain/2]). -export([get_text_plain/2]).
init(Req, Opts) -> init(Req, Opts) ->
{rest, Req, Opts}. {cowboy_rest, Req, Opts}.
content_types_provided(Req, State) -> content_types_provided(Req, State) ->
{[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}. {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}.

View file

@ -7,7 +7,7 @@
-export([websocket_info/3]). -export([websocket_info/3]).
init(Req, _) -> init(Req, _) ->
{ws, Req, undefined}. {cowboy_websocket, Req, undefined}.
websocket_handle({text, Data}, Req, State) -> websocket_handle({text, Data}, Req, State) ->
{reply, {text, Data}, Req, State}; {reply, {text, Data}, Req, State};

View file

@ -8,7 +8,7 @@
init(Req, _) -> init(Req, _) ->
erlang:start_timer(1000, self(), <<"websocket_init">>), erlang:start_timer(1000, self(), <<"websocket_init">>),
{ws, Req, undefined}. {cowboy_websocket, Req, undefined}.
websocket_handle({text, Data}, Req, State) -> websocket_handle({text, Data}, Req, State) ->
{reply, {text, Data}, Req, State}; {reply, {text, Data}, Req, State};

View file

@ -4,5 +4,5 @@
-export([init/2]). -export([init/2]).
init(Req, _) -> init(Req, Opts) ->
{shutdown, cowboy_req:reply(403, Req), undefined}. {ok, cowboy_req:reply(403, Req), Opts}.

View file

@ -8,7 +8,7 @@
init(Req, Opts) -> init(Req, Opts) ->
erlang:send_after(10, self(), send_many), erlang:send_after(10, self(), send_many),
{ws, Req, Opts}. {cowboy_websocket, Req, Opts}.
websocket_handle(_Frame, Req, State) -> websocket_handle(_Frame, Req, State) ->
{ok, Req, State}. {ok, Req, State}.

View file

@ -8,7 +8,7 @@
init(Req, _) -> init(Req, _) ->
erlang:start_timer(500, self(), should_not_cancel_timer), erlang:start_timer(500, self(), should_not_cancel_timer),
{ws, Req, undefined, 1000}. {cowboy_websocket, Req, undefined, 1000}.
websocket_handle({text, Data}, Req, State) -> websocket_handle({text, Data}, Req, State) ->
{reply, {text, Data}, Req, State}; {reply, {text, Data}, Req, State};

View file

@ -7,7 +7,7 @@
-export([websocket_info/3]). -export([websocket_info/3]).
init(Req, _) -> init(Req, _) ->
{ws, Req, undefined, 1000, hibernate}. {cowboy_websocket, Req, undefined, 1000, hibernate}.
websocket_handle(_Frame, Req, State) -> websocket_handle(_Frame, Req, State) ->
{ok, Req, State, hibernate}. {ok, Req, State, hibernate}.