0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00
Small, fast, modern HTTP server for Erlang/OTP.
Find a file
Loïc Hoguin 29e71cf4da Switch the HTTP protocol to use binary packets instead of lists.
The server now does a single recv (or more, but only if needed)
which is then sent to erlang:decode_packet/3 multiple times. Since
most requests are smaller than the default MTU on many platforms,
we benefit from this greatly.

In the case of requests with a body, the server usually read at
least part of the body on the first recv. This is bufferized
properly and used when later retrieving the body.

In the case of pipelined requests, we can end up reading many
requests in a single recv, which are then handled properly using
only the buffer containing the received data.
2011-05-05 14:03:39 +02:00
include Switch the HTTP protocol to use binary packets instead of lists. 2011-05-05 14:03:39 +02:00
src Switch the HTTP protocol to use binary packets instead of lists. 2011-05-05 14:03:39 +02:00
test Switch the HTTP protocol to use binary packets instead of lists. 2011-05-05 14:03:39 +02:00
.gitignore Initial work on a ct test suite for the HTTP protocol. 2011-04-08 16:30:37 +02:00
AUTHORS Add an AUTHORS file. 2011-04-03 15:04:40 +02:00
LICENSE Initial commit. 2011-03-17 00:29:35 +01:00
Makefile Makefile: Add separate eunit and ct test rules. 2011-05-04 11:50:46 +02:00
README.md Merge pull request #5 from yrashk/patch-1. 2011-04-30 13:32:37 -07:00
rebar.config Don't enable binary optimization warnings by default. 2011-04-12 16:23:52 +02:00

Cowboy

Cowboy is a small, fast and modular HTTP server written in Erlang.

Goals

Cowboy aims to provide the following advantages:

  • Small codebase.
  • Damn fast.
  • Modular: transport, protocol and handlers are replaceable. (see below)
  • Easy to embed inside another application.
  • Selectively dispatch requests to handlers, allowing you to send some requests to your embedded code and others to a FastCGI application in PHP or Ruby.
  • No parameterized module. No process dictionary. Clean Erlang code.

The server is currently in early development stage. Comments, suggestions are more than welcome. To contribute, either open bug reports, or fork the project and send us pull requests with new or improved functionality. Of course you might want to discuss your plans with us before you do any serious work so we can share ideas and save everyone time.

Embedding Cowboy

  • Add Cowboy as a rebar or agner dependency to your application.
  • Start Cowboy and add one or more listeners.
  • Write handlers.

Getting Started

Cowboy can be started and stopped like any other application. However, the Cowboy application does not start any listener, those must be started manually.

A listener is a special kind of supervisor that handles a pool of acceptor processes. It also manages all its associated request processes. This allows you to shutdown all processes related to a listener by stopping the supervisor.

An acceptor simply accepts connections and forwards them to a protocol module, for example HTTP. You must thus define the transport and protocol module to use for the listener, their options and the number of acceptors in the pool before you can start a listener supervisor.

For HTTP applications the transport can be either TCP or SSL for HTTP and HTTPS respectively. On the other hand, the protocol is of course HTTP.

You can start and stop listeners by calling cowboy:start_listener and cowboy:stop_listener respectively. It is your responsability to give each listener a unique name.

Code speaks more than words:

application:start(cowboy),
Dispatch = [
    %% {Host, list({Path, Handler, Opts})}
    {'_', [{'_', my_handler, []}]}
],
%% Name, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts
cowboy:start_listener(http, 100,
    cowboy_tcp_transport, [{port, 8080}],
    cowboy_http_protocol, [{dispatch, Dispatch}]
).

You must also write the my_handler module to process requests. You can use one of the predefined handlers or write your own. An hello world HTTP handler could be written like this:

-module(my_handler).
-export([init/3, handle/2, terminate/2]).

init({tcp, http}, Req, Opts) ->
    {ok, Req, undefined}.

handle(Req, State) ->
    {ok, Req2} = cowboy_http_req:reply(200, [], "Hello World!", Req),
    {ok, Req2, State}.

terminate(Req, State) ->
    ok.