From d8e841c8a2c3afc45dad41490c72d82203019d68 Mon Sep 17 00:00:00 2001 From: Michiel Hakvoort Date: Thu, 29 Sep 2011 13:38:17 +0200 Subject: Add cowboy_protocol behaviour --- src/cowboy_http_protocol.erl | 1 + src/cowboy_protocol.erl | 61 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/cowboy_protocol.erl diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl index fe5f375..40dc51c 100644 --- a/src/cowboy_http_protocol.erl +++ b/src/cowboy_http_protocol.erl @@ -30,6 +30,7 @@ %% @see cowboy_dispatcher %% @see cowboy_http_handler -module(cowboy_http_protocol). +-behaviour(cowboy_protocol). -export([start_link/4]). %% API. -export([init/4, parse_request/1]). %% FSM. diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl new file mode 100644 index 0000000..4d2480a --- /dev/null +++ b/src/cowboy_protocol.erl @@ -0,0 +1,61 @@ +%% Copyright (c) 2011, Loïc Hoguin +%% Copyright (c) 2011, Michiel Hakvoort +%% +%% 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. + +%% @doc Cowboy protocol. +%% +%% A Cowboy protocol must implement one callback: start_link/4. +%% +%% start_link/4 is meant for the initialization of the +%% protocol process. +%% It receives the pid to the listener's gen_server, the client socket, +%% the module name of the chosen transport and the options defined when +%% starting the listener. The start_link/4 function must follow +%% the supervisor start function specification. +%% +%% After initializing your protocol, it is recommended to wait to +%% receive a message containing the atom 'shoot', as it will ensure +%% Cowboy has been able to fully initialize the socket. +%% Anything you do past this point is up to you! +%% +%% If you need to change some socket options, like enabling raw mode +%% for example, you can call the Transport:setopts/2 function. +%% It is the protocol's responsability to manage the socket usage, +%% there should be no need for an user to specify that kind of options +%% while starting a listener. +%% +%% You should definitely look at the cowboy_http_protocol module for +%% a great example of fast request handling if you need to. +%% Otherwise it's probably safe to use {active, once} mode +%% and handle everything as it comes. +%% +%% Note that while you technically can run a protocol handler directly +%% as a gen_server or a gen_fsm, it's probably not a good idea, +%% as the only call you'll ever receive from Cowboy is the +%% start_link/4 call. On the other hand, feel free to write +%% a very basic protocol handler which then forwards requests to a +%% gen_server or gen_fsm. By doing so however you must take care to +%% supervise their processes as Cowboy only knows about the protocol +%% handler itself. +-module(cowboy_protocol). + +-export([behaviour_info/1]). + +%% @private +-spec behaviour_info(_) + -> undefined | [{start_link, 4}, ...]. +behaviour_info(callbacks) -> + [{start_link, 4}]; +behaviour_info(_Other) -> + undefined. -- cgit v1.2.3