2011-03-07 22:59:22 +01:00
|
|
|
Cowboy
|
|
|
|
======
|
|
|
|
|
|
|
|
Cowboy is a small, fast and modular HTTP server written in Erlang.
|
|
|
|
|
|
|
|
Goals
|
|
|
|
-----
|
|
|
|
|
2012-10-15 14:35:52 +02:00
|
|
|
Cowboy aims to provide a **complete** HTTP stack in a **small** code base.
|
|
|
|
It is optimized for **low latency** and **low memory usage**, in parts
|
|
|
|
because it uses **binary strings**.
|
|
|
|
|
|
|
|
Cowboy provides **routing** capabilities, selectively dispatching requests
|
|
|
|
to handlers written in Erlang.
|
|
|
|
|
|
|
|
Because it uses Ranch for managing connections, Cowboy can easily be
|
|
|
|
**embedded** in any other application.
|
|
|
|
|
|
|
|
No parameterized module. No process dictionary. **Clean** Erlang code.
|
2011-03-07 22:59:22 +01:00
|
|
|
|
2011-05-07 12:46:23 +02:00
|
|
|
Quick start
|
|
|
|
-----------
|
2011-03-07 22:59:22 +01:00
|
|
|
|
2012-11-27 16:24:08 +01:00
|
|
|
* Add Cowboy as a rebar dependency to your application
|
|
|
|
* Start Cowboy and add one or more listeners
|
|
|
|
* Write handlers for your application
|
2011-03-07 22:59:22 +01:00
|
|
|
|
2011-03-20 14:15:58 +01:00
|
|
|
Getting Started
|
|
|
|
---------------
|
2011-03-07 22:59:22 +01:00
|
|
|
|
2012-11-13 17:52:02 +01:00
|
|
|
* [Read the guide](http://ninenines.eu/docs/en/cowboy/HEAD/guide/introduction)
|
|
|
|
* Look at the examples in the `examples/` directory
|
|
|
|
* Build API documentation with `make docs`; open `doc/index.html`
|
2012-10-15 14:35:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Old README
|
|
|
|
----------
|
|
|
|
|
|
|
|
This and all following sections will be removed as soon as their
|
|
|
|
equivalent appear in the Cowboy guide.
|
|
|
|
|
2012-08-27 11:50:35 +02:00
|
|
|
Cowboy does nothing by default.
|
2011-04-02 20:27:16 +02:00
|
|
|
|
2012-08-27 11:50:35 +02:00
|
|
|
Cowboy uses Ranch for handling connections, and provides convenience
|
|
|
|
functions to start and stop Ranch listeners. The Ranch application
|
2012-10-11 20:52:29 +02:00
|
|
|
must always be started before Cowboy. The crypto application must
|
|
|
|
also be started.
|
2011-03-07 22:59:22 +01:00
|
|
|
|
2012-08-27 11:50:35 +02:00
|
|
|
The `cowboy:start_http/4` function will handle HTTP connections
|
|
|
|
using the TCP transport. Similarly, `cowboy:start_https/4` will
|
|
|
|
handle HTTP connections using the SSL transport.
|
2011-03-07 22:59:22 +01:00
|
|
|
|
2012-08-27 11:50:35 +02:00
|
|
|
You can start as many listeners as you need to. To allow this, you
|
|
|
|
are required to give a name to your listeners. It is the first
|
|
|
|
argument to the start functions. The name can be of any type.
|
2011-05-07 12:46:23 +02:00
|
|
|
|
2012-08-27 11:50:35 +02:00
|
|
|
You can stop listeners using `cowboy:stop_listener/1`, giving it
|
|
|
|
the name of the listener to be stopped.
|
2011-04-03 00:21:47 +02:00
|
|
|
|
2012-08-27 11:50:35 +02:00
|
|
|
The following example demonstrates the startup of a very simple
|
|
|
|
HTTP listener. It redirects all requests to the `my_handler`
|
|
|
|
module.
|
2011-03-07 22:59:22 +01:00
|
|
|
|
2011-04-30 12:28:07 +02:00
|
|
|
``` erlang
|
2012-10-11 20:52:29 +02:00
|
|
|
application:start(crypto),
|
2012-08-27 11:50:35 +02:00
|
|
|
application:start(ranch),
|
2011-05-07 16:19:01 +02:00
|
|
|
application:start(cowboy),
|
|
|
|
Dispatch = [
|
2012-08-27 11:50:35 +02:00
|
|
|
%% {URIHost, list({URIPath, Handler, Opts})}
|
2011-05-07 16:19:01 +02:00
|
|
|
{'_', [{'_', my_handler, []}]}
|
|
|
|
],
|
2012-08-27 11:50:35 +02:00
|
|
|
%% Name, NbAcceptors, TransOpts, ProtoOpts
|
|
|
|
cowboy:start_http(my_http_listener, 100, [{port, 8080}],
|
|
|
|
[{dispatch, Dispatch}]
|
2011-05-07 16:19:01 +02:00
|
|
|
).
|
2011-04-30 12:28:07 +02:00
|
|
|
```
|
2011-03-07 22:59:22 +01:00
|
|
|
|
2012-08-27 11:50:35 +02:00
|
|
|
This is not enough though, you must also write the `my_handler`
|
|
|
|
module to process the incoming HTTP requests. Of course Cowboy
|
|
|
|
comes with predefined handlers for specific tasks but most of
|
|
|
|
the time you'll need to write the handlers appropriate for your
|
|
|
|
application.
|
2011-05-07 12:46:23 +02:00
|
|
|
|
2011-05-07 15:35:07 +02:00
|
|
|
Following is an example of a "Hello World!" HTTP handler.
|
2011-03-07 22:59:22 +01:00
|
|
|
|
2011-04-30 12:28:07 +02:00
|
|
|
``` erlang
|
|
|
|
-module(my_handler).
|
|
|
|
-export([init/3, handle/2, terminate/2]).
|
2011-03-07 22:59:22 +01:00
|
|
|
|
2011-04-30 12:28:07 +02:00
|
|
|
init({tcp, http}, Req, Opts) ->
|
2011-05-07 12:46:23 +02:00
|
|
|
{ok, Req, undefined_state}.
|
2011-03-18 00:15:46 +01:00
|
|
|
|
2011-04-30 12:28:07 +02:00
|
|
|
handle(Req, State) ->
|
2012-08-27 13:28:57 +02:00
|
|
|
{ok, Req2} = cowboy_req:reply(200, [], <<"Hello World!">>, Req),
|
2011-04-30 12:28:07 +02:00
|
|
|
{ok, Req2, State}.
|
2011-03-18 13:47:37 +01:00
|
|
|
|
2011-04-30 12:28:07 +02:00
|
|
|
terminate(Req, State) ->
|
|
|
|
ok.
|
|
|
|
```
|
2012-01-09 07:40:43 +01:00
|
|
|
|
|
|
|
You can also write handlers that do not reply directly. Instead, such handlers
|
|
|
|
will wait for an Erlang message from another process and only reply when
|
|
|
|
receiving such message, or timeout if it didn't arrive in time.
|
|
|
|
|
|
|
|
This is especially useful for long-polling functionality, as Cowboy will handle
|
|
|
|
process hibernation and timeouts properly, preventing mistakes if you were to
|
2012-08-27 11:50:35 +02:00
|
|
|
write the code yourself. A handler of that kind can be defined like this:
|
2012-01-09 07:40:43 +01:00
|
|
|
|
|
|
|
``` erlang
|
|
|
|
-module(my_loop_handler).
|
|
|
|
-export([init/3, info/3, terminate/2]).
|
|
|
|
|
|
|
|
-define(TIMEOUT, 60000).
|
|
|
|
|
|
|
|
init({tcp, http}, Req, Opts) ->
|
|
|
|
{loop, Req, undefined_state, ?TIMEOUT, hibernate}.
|
|
|
|
|
|
|
|
info({reply, Body}, Req, State) ->
|
2012-08-27 13:28:57 +02:00
|
|
|
{ok, Req2} = cowboy_req:reply(200, [], Body, Req),
|
2012-01-09 07:40:43 +01:00
|
|
|
{ok, Req2, State};
|
|
|
|
info(Message, Req, State) ->
|
|
|
|
{loop, Req, State, hibernate}.
|
|
|
|
|
|
|
|
terminate(Req, State) ->
|
|
|
|
ok.
|
|
|
|
```
|
|
|
|
|
|
|
|
It is of course possible to combine both type of handlers together as long as
|
|
|
|
you return the proper tuple from init/3.
|
|
|
|
|
2011-05-07 12:46:23 +02:00
|
|
|
Continue reading to learn how to dispatch rules and handle requests.
|
|
|
|
|
|
|
|
Dispatch rules
|
|
|
|
--------------
|
|
|
|
|
|
|
|
Cowboy allows you to dispatch HTTP requests directly to a specific handler
|
|
|
|
based on the hostname and path information from the request. It also lets
|
|
|
|
you define static options for the handler directly in the rules.
|
|
|
|
|
|
|
|
To match the hostname and path, Cowboy requires a list of tokens. For
|
2012-05-23 14:53:48 +02:00
|
|
|
example, to match the "ninenines.eu" domain name, you must specify
|
|
|
|
`[<<"ninenines">>, <<"eu">>]`. Or, to match the "/path/to/my/resource"
|
2011-05-07 15:35:07 +02:00
|
|
|
you must use `[<<"path">>, <<"to">>, <<"my">>, <<"resource">>]`. All the
|
2011-05-07 12:46:23 +02:00
|
|
|
tokens must be given as binary.
|
|
|
|
|
2011-05-07 15:35:07 +02:00
|
|
|
You can use the special token `'_'` (the atom underscore) to indicate that
|
2011-05-07 12:46:23 +02:00
|
|
|
you accept anything in that position. For example if you have both
|
2012-05-23 14:53:48 +02:00
|
|
|
"ninenines.eu" and "ninenines.fr" domains, you can use the match spec
|
|
|
|
`[<<"ninenines">>, '_']` to match any top level extension.
|
2011-05-07 12:46:23 +02:00
|
|
|
|
2011-05-16 11:36:28 +02:00
|
|
|
Finally, you can also match multiple leading segments of the domain name and
|
|
|
|
multiple trailing segments of the request path using the atom `'...'` (the atom
|
|
|
|
ellipsis) respectively as the first host token or the last path token. For
|
2012-05-23 14:53:48 +02:00
|
|
|
example, host rule `['...', <<"ninenines">>, <<"eu">>]` can match both
|
|
|
|
"cowboy.bugs.ninenines.eu" and "ninenines.eu" and path rule
|
2011-10-25 00:05:31 +03:00
|
|
|
`[<<"projects">>, '...']` can match both "/projects" and
|
2011-05-16 11:36:28 +02:00
|
|
|
"/projects/cowboy/issues/42". The host leading segments and the path trailing
|
2012-08-27 13:28:57 +02:00
|
|
|
segments can later be retrieved through `cowboy_req:host_info/1` and
|
|
|
|
`cowboy_req:path_info/1`.
|
2011-05-16 11:36:28 +02:00
|
|
|
|
2011-05-07 12:46:23 +02:00
|
|
|
Any other atom used as a token will bind the value to this atom when
|
2012-05-23 14:53:48 +02:00
|
|
|
matching. To follow on our hostnames example, `[<<"ninenines">>, ext]`
|
2011-05-07 15:35:07 +02:00
|
|
|
would bind the values `<<"eu">>` and `<<"fr">>` to the ext atom, that you
|
2012-08-27 13:28:57 +02:00
|
|
|
can later retrieve in your handler by calling `cowboy_req:binding/{2,3}`.
|
2011-05-07 12:46:23 +02:00
|
|
|
|
2011-05-07 15:35:07 +02:00
|
|
|
You can also accept any match spec by using the atom `'_'` directly instead of
|
2011-05-07 12:46:23 +02:00
|
|
|
a list of tokens. Our hello world example above uses this to forward all
|
|
|
|
requests to a single handler.
|
|
|
|
|
|
|
|
There is currently no way to match multiple tokens at once.
|
|
|
|
|
|
|
|
Requests handling
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
Requests are passed around in the Request variable. Although they are
|
|
|
|
defined as a record, it is recommended to access them only through the
|
2012-08-27 13:28:57 +02:00
|
|
|
cowboy_req module API.
|
2011-05-07 12:46:23 +02:00
|
|
|
|
|
|
|
You can retrieve the HTTP method, HTTP version, peer address and port,
|
|
|
|
host tokens, raw host, used port, path tokens, raw path, query string
|
|
|
|
values, bound values from the dispatch step, header values from the
|
|
|
|
request. You can also read the request body, if any, optionally parsing
|
|
|
|
it as a query string. Finally, the request allows you to send a response
|
|
|
|
to the client.
|
|
|
|
|
2012-08-27 13:28:57 +02:00
|
|
|
See the cowboy_req module for more information.
|
2011-05-07 12:46:23 +02:00
|
|
|
|
|
|
|
Websockets
|
|
|
|
----------
|
|
|
|
|
|
|
|
The Websocket protocol is built upon the HTTP protocol. It first sends
|
|
|
|
an HTTP request for an handshake, performs it and then switches
|
|
|
|
to Websocket. Therefore you need to write a standard HTTP handler to
|
|
|
|
confirm the handshake should be completed and then the Websocket-specific
|
|
|
|
callbacks.
|
|
|
|
|
|
|
|
A simple handler doing nothing but sending a repetitive message using
|
|
|
|
Websocket would look like this:
|
|
|
|
|
|
|
|
``` erlang
|
|
|
|
-module(my_ws_handler).
|
2012-01-09 07:40:43 +01:00
|
|
|
-export([init/3]).
|
2011-07-19 12:12:25 +02:00
|
|
|
-export([websocket_init/3, websocket_handle/3,
|
|
|
|
websocket_info/3, websocket_terminate/3]).
|
2011-05-07 12:46:23 +02:00
|
|
|
|
|
|
|
init({tcp, http}, Req, Opts) ->
|
2012-08-27 14:00:28 +02:00
|
|
|
{upgrade, protocol, cowboy_websocket}.
|
2011-05-07 12:46:23 +02:00
|
|
|
|
|
|
|
websocket_init(TransportName, Req, _Opts) ->
|
|
|
|
erlang:start_timer(1000, self(), <<"Hello!">>),
|
|
|
|
{ok, Req, undefined_state}.
|
|
|
|
|
2011-08-23 16:24:02 +02:00
|
|
|
websocket_handle({text, Msg}, Req, State) ->
|
|
|
|
{reply, {text, << "That's what she said! ", Msg/binary >>}, Req, State};
|
|
|
|
websocket_handle(_Data, Req, State) ->
|
|
|
|
{ok, Req, State}.
|
2011-07-19 12:12:25 +02:00
|
|
|
|
|
|
|
websocket_info({timeout, _Ref, Msg}, Req, State) ->
|
2011-05-07 12:46:23 +02:00
|
|
|
erlang:start_timer(1000, self(), <<"How' you doin'?">>),
|
2011-08-23 16:24:02 +02:00
|
|
|
{reply, {text, Msg}, Req, State};
|
2011-07-19 12:12:25 +02:00
|
|
|
websocket_info(_Info, Req, State) ->
|
|
|
|
{ok, Req, State}.
|
2011-05-07 12:46:23 +02:00
|
|
|
|
|
|
|
websocket_terminate(_Reason, _Req, _State) ->
|
|
|
|
ok.
|
|
|
|
```
|
|
|
|
|
|
|
|
Of course you can have an HTTP handler doing both HTTP and Websocket
|
|
|
|
handling, but for the sake of this example we're ignoring the HTTP
|
|
|
|
part entirely.
|
|
|
|
|
2011-08-23 16:24:02 +02:00
|
|
|
As the Websocket protocol is still a draft the API is subject to change
|
|
|
|
regularly when support to the most recent drafts gets added. Features may
|
|
|
|
be added, changed or removed before the protocol gets finalized. Cowboy
|
|
|
|
tries to implement all drafts transparently and give a single interface to
|
|
|
|
handle them all, however.
|