From 5ce4c2bfb40ecc4b687a2941e612025a1c4ff913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 26 Sep 2014 15:58:44 +0300 Subject: 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. --- test/handlers/input_crash_h.erl | 4 ++-- test/handlers/long_polling_h.erl | 7 +++---- test/handlers/loop_handler_body_h.erl | 7 +++---- test/handlers/loop_handler_timeout_h.erl | 7 +++---- 4 files changed, 11 insertions(+), 14 deletions(-) (limited to 'test/handlers') diff --git a/test/handlers/input_crash_h.erl b/test/handlers/input_crash_h.erl index 668d053..e941cca 100644 --- a/test/handlers/input_crash_h.erl +++ b/test/handlers/input_crash_h.erl @@ -3,8 +3,8 @@ -module(input_crash_h). --export([init/3]). +-export([init/2]). -init(_, Req, content_length) -> +init(Req, content_length) -> cowboy_error_h:ignore(cow_http_hd, number, 2), cowboy_req:parse_header(<<"content-length">>, Req). diff --git a/test/handlers/long_polling_h.erl b/test/handlers/long_polling_h.erl index 1c86aed..1b240fd 100644 --- a/test/handlers/long_polling_h.erl +++ b/test/handlers/long_polling_h.erl @@ -4,15 +4,14 @@ %% When it receives the last message, it sends a 102 reply back. -module(long_polling_h). --behaviour(cowboy_loop_handler). --export([init/3]). +-export([init/2]). -export([info/3]). -export([terminate/3]). -init(_, Req, _) -> +init(Req, _) -> erlang:send_after(200, self(), timeout), - {loop, Req, 2, 5000, hibernate}. + {long_polling, Req, 2, 5000, hibernate}. info(timeout, Req, 0) -> {ok, cowboy_req:reply(102, Req), 0}; diff --git a/test/handlers/loop_handler_body_h.erl b/test/handlers/loop_handler_body_h.erl index 559ef90..ac75773 100644 --- a/test/handlers/loop_handler_body_h.erl +++ b/test/handlers/loop_handler_body_h.erl @@ -4,15 +4,14 @@ %% then sends a 200 reply back. -module(loop_handler_body_h). --behaviour(cowboy_loop_handler). --export([init/3]). +-export([init/2]). -export([info/3]). -export([terminate/3]). -init(_, Req, _) -> +init(Req, _) -> self() ! timeout, - {loop, Req, undefined, 5000, hibernate}. + {long_polling, Req, undefined, 5000, hibernate}. info(timeout, Req, State) -> {ok, Body, Req2} = cowboy_req:body(Req), diff --git a/test/handlers/loop_handler_timeout_h.erl b/test/handlers/loop_handler_timeout_h.erl index 3b158bf..8e24d33 100644 --- a/test/handlers/loop_handler_timeout_h.erl +++ b/test/handlers/loop_handler_timeout_h.erl @@ -5,15 +5,14 @@ %% This results in a 204 reply being sent back by Cowboy. -module(loop_handler_timeout_h). --behaviour(cowboy_loop_handler). --export([init/3]). +-export([init/2]). -export([info/3]). -export([terminate/3]). -init(_, Req, _) -> +init(Req, _) -> erlang:send_after(1000, self(), timeout), - {loop, Req, undefined, 200, hibernate}. + {long_polling, Req, undefined, 200, hibernate}. info(timeout, Req, State) -> {ok, cowboy_req:reply(500, Req), State}. -- cgit v1.2.3