0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Unify the init and terminate callbacks

This set of changes is the first step to simplify the
writing of handlers, by removing some extraneous
callbacks and making others optional.

init/3 is now init/2, its first argument being removed.

rest_init/2 and rest_terminate/2 have been removed.

websocket_init/3 and websocket_terminate/3 have been removed.

terminate/3 is now optional. It is called regardless of
the type of handler, including rest and websocket.

The return value of init/2 changed. It now returns
{Mod, Req, Opts} with Mod being either one of the four
handler type or a custom module. It can also return extra
timeout and hibernate options.

The signature for sub protocols has changed, they now
receive these extra timeout and hibernate options.

Loop handlers are now implemented in cowboy_long_polling,
and will be renamed throughout the project in a future commit.
This commit is contained in:
Loïc Hoguin 2014-09-26 15:58:44 +03:00
parent fd37fad592
commit 5ce4c2bfb4
70 changed files with 668 additions and 1015 deletions

View file

@ -3,12 +3,11 @@
%% @doc Chunked hello world handler.
-module(toppage_handler).
-export([init/3]).
-export([init/2]).
-export([handle/2]).
-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
Req2 = cowboy_req:chunked_reply(200, Req),
@ -18,6 +17,3 @@ handle(Req, State) ->
timer:sleep(1000),
cowboy_req:chunk("Chunked!\r\n", Req2),
{ok, Req2, State}.
terminate(_Reason, _Req, _State) ->
ok.

View file

@ -3,12 +3,11 @@
%% @doc Compress response handler.
-module(toppage_handler).
-export([init/3]).
-export([init/2]).
-export([handle/2]).
-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
BigBody =
@ -26,6 +25,3 @@ in many other parts of the world, particularly South America and Australia,
who perform work similar to the cowboy in their respective nations.\n">>,
Req2 = cowboy_req:reply(200, [], BigBody, Req),
{ok, Req2, State}.
terminate(_Reason, _Req, _State) ->
ok.

View file

@ -3,12 +3,11 @@
%% @doc Cookie handler.
-module(toppage_handler).
-export([init/3]).
-export([init/2]).
-export([handle/2]).
-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
NewValue = integer_to_list(random:uniform(1000000)),
@ -24,6 +23,3 @@ handle(Req, State) ->
[{<<"content-type">>, <<"text/html">>}],
Body, Req2),
{ok, Req3, State}.
terminate(_Reason, _Req, _State) ->
ok.

View file

@ -3,12 +3,11 @@
%% @doc GET echo handler.
-module(toppage_handler).
-export([init/3]).
-export([init/2]).
-export([handle/2]).
-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
Method = cowboy_req:method(Req),
@ -25,6 +24,3 @@ echo(<<"GET">>, Echo, Req) ->
echo(_, _, Req) ->
%% Method not allowed.
cowboy_req:reply(405, Req).
terminate(_Reason, _Req, _State) ->
ok.

View file

@ -3,12 +3,11 @@
%% @doc POST echo handler.
-module(toppage_handler).
-export([init/3]).
-export([init/2]).
-export([handle/2]).
-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
Method = cowboy_req:method(Req),
@ -32,6 +31,3 @@ echo(Echo, Req) ->
cowboy_req:reply(200, [
{<<"content-type">>, <<"text/plain; charset=utf-8">>}
], Echo, Req).
terminate(_Reason, _Req, _State) ->
ok.

View file

@ -3,24 +3,20 @@
%% @doc EventSource emitter.
-module(eventsource_handler).
-export([init/3]).
-export([init/2]).
-export([info/3]).
-export([terminate/3]).
init(_Transport, Req, []) ->
init(Req, Opts) ->
Headers = [{<<"content-type">>, <<"text/event-stream">>}],
Req2 = cowboy_req:chunked_reply(200, Headers, Req),
erlang:send_after(1000, self(), {message, "Tick"}),
{loop, Req2, undefined, 5000}.
{long_polling, Req2, Opts, 5000}.
info({message, Msg}, Req, State) ->
cowboy_req:chunk(["id: ", id(), "\ndata: ", Msg, "\n\n"], Req),
erlang:send_after(1000, self(), {message, "Tick"}),
{loop, Req, State}.
terminate(_Reason, _Req, _State) ->
ok.
id() ->
{Mega, Sec, Micro} = erlang:now(),
Id = (Mega * 1000000 + Sec) * 1000000 + Micro,

View file

@ -3,18 +3,14 @@
%% @doc Hello world handler.
-module(toppage_handler).
-export([init/3]).
-export([init/2]).
-export([handle/2]).
-export([terminate/3]).
init(_Type, Req, []) ->
{ok, Req, undefined}.
init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
Req2 = cowboy_req:reply(200, [
{<<"content-type">>, <<"text/plain">>}
], <<"Hello world!">>, Req),
{ok, Req2, State}.
terminate(_Reason, _Req, _State) ->
ok.

View file

@ -3,13 +3,13 @@
%% @doc Handler with basic HTTP authorization.
-module(toppage_handler).
-export([init/3]).
-export([init/2]).
-export([content_types_provided/2]).
-export([is_authorized/2]).
-export([to_text/2]).
init(_Transport, _Req, []) ->
{upgrade, protocol, cowboy_rest}.
init(Req, Opts) ->
{rest, Req, Opts}.
is_authorized(Req, State) ->
case cowboy_req:parse_header(<<"authorization">>, Req) of

View file

@ -3,14 +3,14 @@
%% @doc Hello world handler.
-module(toppage_handler).
-export([init/3]).
-export([init/2]).
-export([content_types_provided/2]).
-export([hello_to_html/2]).
-export([hello_to_json/2]).
-export([hello_to_text/2]).
init(_Transport, _Req, []) ->
{upgrade, protocol, cowboy_rest}.
init(Req, Opts) ->
{rest, Req, Opts}.
content_types_provided(Req, State) ->
{[

View file

@ -4,7 +4,7 @@
-module(toppage_handler).
%% Standard callbacks.
-export([init/3]).
-export([init/2]).
-export([allowed_methods/2]).
-export([content_types_provided/2]).
-export([content_types_accepted/2]).
@ -15,11 +15,9 @@
-export([paste_html/2]).
-export([paste_text/2]).
init(_Transport, _Req, []) ->
% For the random number generator:
{X, Y, Z} = now(),
random:seed(X, Y, Z),
{upgrade, protocol, cowboy_rest}.
init(Req, Opts) ->
random:seed(now()),
{rest, Req, Opts}.
allowed_methods(Req, State) ->
{[<<"GET">>, <<"POST">>], Req, State}.

View file

@ -3,16 +3,12 @@
%% @doc Streaming handler.
-module(toppage_handler).
-export([init/3]).
-export([rest_init/2]).
-export([init/2]).
-export([content_types_provided/2]).
-export([streaming_csv/2]).
init(_Transport, _Req, _Table) ->
{upgrade, protocol, cowboy_rest}.
rest_init(Req, Table) ->
{ok, Req, Table}.
init(Req, Table) ->
{rest, Req, Table}.
content_types_provided(Req, State) ->
{[

View file

@ -3,18 +3,14 @@
%% @doc Hello world handler.
-module(toppage_handler).
-export([init/3]).
-export([init/2]).
-export([handle/2]).
-export([terminate/3]).
init(_Transport, Req, []) ->
{ok, Req, undefined}.
init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
Req2 = cowboy_req:reply(200, [
{<<"content-type">>, <<"text/plain">>}
], <<"Hello world!">>, Req),
{ok, Req2, State}.
terminate(_Reason, _Req, _State) ->
ok.

View file

@ -1,12 +1,13 @@
%% Feel free to use, reuse and abuse the code in this file.
%% @doc Upload handler.
-module(upload_handler).
-behaviour(cowboy_http_handler).
-export([init/3]).
-export([init/2]).
-export([handle/2]).
-export([terminate/3]).
init(_, Req, _Opts) ->
{ok, Req, undefined}.
init(Req, Opts) ->
{http, Req, Opts}.
handle(Req, State) ->
{ok, Headers, Req2} = cowboy_req:part(Req),
@ -16,6 +17,3 @@ handle(Req, State) ->
io:format("Received file ~p of content-type ~p as follow:~n~p~n~n",
[Filename, ContentType, Data]),
{ok, Req3, State}.
terminate(_Reason, _Req, _State) ->
ok.

View file

@ -4,8 +4,7 @@
-module(directory_handler).
%% REST Callbacks
-export([init/3]).
-export([rest_init/2]).
-export([init/2]).
-export([allowed_methods/2]).
-export([resource_exists/2]).
-export([content_types_provided/2]).
@ -14,11 +13,8 @@
-export([list_json/2]).
-export([list_html/2]).
init(_Transport, _Req, _Paths) ->
{upgrade, protocol, cowboy_rest}.
rest_init(Req, Paths) ->
{ok, Req, Paths}.
init(Req, Paths) ->
{rest, Req, Paths}.
allowed_methods(Req, State) ->
{[<<"GET">>], Req, State}.

View file

@ -1,18 +1,12 @@
-module(ws_handler).
-behaviour(cowboy_websocket_handler).
-export([init/3]).
-export([websocket_init/3]).
-export([init/2]).
-export([websocket_handle/3]).
-export([websocket_info/3]).
-export([websocket_terminate/3]).
init({tcp, http}, _Req, _Opts) ->
{upgrade, protocol, cowboy_websocket}.
websocket_init(_TransportName, Req, _Opts) ->
init(Req, Opts) ->
erlang:start_timer(1000, self(), <<"Hello!">>),
{ok, Req, undefined_state}.
{ws, Req, Opts}.
websocket_handle({text, Msg}, Req, State) ->
{reply, {text, << "That's what she said! ", Msg/binary >>}, Req, State};
@ -24,6 +18,3 @@ websocket_info({timeout, _Ref, Msg}, Req, State) ->
{reply, {text, Msg}, Req, State};
websocket_info(_Info, Req, State) ->
{ok, Req, State}.
websocket_terminate(_Reason, _Req, _State) ->
ok.