diff options
-rw-r--r-- | README.md | 42 | ||||
-rw-r--r-- | src/cowboy_http_protocol.erl | 8 | ||||
-rw-r--r-- | src/cowboy_ssl_transport.erl | 3 | ||||
-rw-r--r-- | src/cowboy_tcp_transport.erl | 3 |
4 files changed, 28 insertions, 28 deletions
@@ -54,30 +54,34 @@ 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}] - ). +``` erlang +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]). +``` erlang +-module(my_handler). +-export([init/3, handle/2, terminate/2]). - init({tcp, http}, Req, Opts) -> - {ok, Req, undefined}. +init({tcp, http}, Req, Opts) -> + {ok, Req, undefined}. - handle(Req, State) -> - {ok, Req2} = cowboy_http_req:reply(200, [], "Hello World!", Req), - {ok, Req2, State}. +handle(Req, State) -> + {ok, Req2} = cowboy_http_req:reply(200, [], "Hello World!", Req), + {ok, Req2, State}. - terminate(Req, State) -> - ok. +terminate(Req, State) -> + ok. +``` diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl index a25f87a..b68980d 100644 --- a/src/cowboy_http_protocol.erl +++ b/src/cowboy_http_protocol.erl @@ -169,7 +169,7 @@ handler_terminate(HandlerState, Req, State=#state{handler={Handler, _Opts}}) -> BodyRes = ensure_body_processed(Req), ensure_response(Req, State), case {HandlerRes, BodyRes, State#state.connection} of - {ok, ok, keepalive} -> next_request(State); + {ok, ok, keepalive} -> ?MODULE:wait_request(State); _Closed -> terminate(State) end. @@ -210,12 +210,6 @@ terminate(#state{socket=Socket, transport=Transport}) -> Transport:close(Socket), ok. --spec next_request(State::#state{}) -> ok. -next_request(State=#state{connection=keepalive}) -> - ?MODULE:wait_request(State); -next_request(State=#state{connection=close}) -> - terminate(State). - %% Internal. -spec version_to_connection(Version::http_version()) -> keepalive | close. diff --git a/src/cowboy_ssl_transport.erl b/src/cowboy_ssl_transport.erl index 0e80e4e..41577fc 100644 --- a/src/cowboy_ssl_transport.erl +++ b/src/cowboy_ssl_transport.erl @@ -31,11 +31,12 @@ messages() -> {ssl, ssl_closed, ssl_error}. -> {ok, LSocket::ssl:sslsocket()} | {error, Reason::atom()}. listen(Opts) -> {port, Port} = lists:keyfind(port, 1, Opts), + Backlog = proplists:get_value(backlog, Opts, 128), {certfile, CertFile} = lists:keyfind(certfile, 1, Opts), {keyfile, KeyFile} = lists:keyfind(keyfile, 1, Opts), {password, Password} = lists:keyfind(password, 1, Opts), ssl:listen(Port, [binary, {active, false}, - {packet, raw}, {reuseaddr, true}, + {backlog, Backlog}, {packet, raw}, {reuseaddr, true}, {certfile, CertFile}, {keyfile, KeyFile}, {password, Password}]). -spec accept(LSocket::ssl:sslsocket(), Timeout::timeout()) diff --git a/src/cowboy_tcp_transport.erl b/src/cowboy_tcp_transport.erl index 45c87df..49003bd 100644 --- a/src/cowboy_tcp_transport.erl +++ b/src/cowboy_tcp_transport.erl @@ -30,8 +30,9 @@ messages() -> {tcp, tcp_closed, tcp_error}. -> {ok, LSocket::inet:socket()} | {error, Reason::atom()}. listen(Opts) -> {port, Port} = lists:keyfind(port, 1, Opts), + Backlog = proplists:get_value(backlog, Opts, 128), gen_tcp:listen(Port, [binary, {active, false}, - {packet, raw}, {reuseaddr, true}]). + {backlog, Backlog}, {packet, raw}, {reuseaddr, true}]). -spec accept(LSocket::inet:socket(), Timeout::timeout()) -> {ok, Socket::inet:socket()} |