From a45813c60f0f983a24ea29d491b37f0590fdd087 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Sat, 18 Feb 2017 18:26:20 +0100 Subject: Allow passing options to sub protocols Before this commit we had an issue where configuring a Websocket connection was simply not possible without doing magic, adding callbacks or extra return values. The init/2 function only allowed setting hibernate and timeout options. After this commit, when switching to a different type of handler you can either return {module, Req, State} or {module, Req, State, Opts} where Opts is any value (as far as the sub protocol interface is concerned) and is ultimately checked by the custom handlers. A large protocol like Websocket would accept only a map there, with many different options, while a small interface like loop handlers would allow passing hibernate and nothing else. For Websocket, hibernate must be set from the websocket_init/1 callback, because init/2 executes in a separate process. Sub protocols now have two callbacks: one with the Opts value, one without. The loop handler code was largely reworked and simplified. It does not need to manage a timeout or read from the socket anymore, it's the job of the protocol code. A lot of unnecessary stuff was therefore removed. Websocket compression must now be enabled from the handler options instead of per listener. This means that a project can have two separate Websocket handlers with different options. Compression is still disabled by default, and the idle_timeout value was changed from inifnity to 60000 (60 seconds), as that's safer and is also a good value for mobile devices. --- src/cowboy_rest.erl | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src/cowboy_rest.erl') diff --git a/src/cowboy_rest.erl b/src/cowboy_rest.erl index 49e1946..21d56a5 100644 --- a/src/cowboy_rest.erl +++ b/src/cowboy_rest.erl @@ -17,15 +17,14 @@ -module(cowboy_rest). -behaviour(cowboy_sub_protocol). --export([upgrade/6]). +-export([upgrade/4]). +-export([upgrade/5]). %% Common handler callbacks. -callback init(Req, any()) -> {ok | module(), Req, any()} - | {module(), Req, any(), hibernate} - | {module(), Req, any(), timeout()} - | {module(), Req, any(), timeout(), hibernate} + | {module(), Req, any(), any()} when Req::cowboy_req:req(). -callback terminate(any(), cowboy_req:req(), any()) -> ok. @@ -232,14 +231,20 @@ expires :: undefined | no_call | calendar:datetime() | binary() }). --spec upgrade(Req, Env, module(), any(), infinity, run) +-spec upgrade(Req, Env, module(), any()) -> {ok, Req, Env} when Req::cowboy_req:req(), Env::cowboy_middleware:env(). -upgrade(Req0, Env, Handler, HandlerState, infinity, run) -> +upgrade(Req0, Env, Handler, HandlerState) -> Method = cowboy_req:method(Req0), {ok, Req, Result} = service_available(Req0, #state{method=Method, handler=Handler, handler_state=HandlerState}), {ok, Req, Env#{result => Result}}. +-spec upgrade(Req, Env, module(), any(), any()) + -> {ok, Req, Env} when Req::cowboy_req:req(), Env::cowboy_middleware:env(). +%% cowboy_rest takes no options. +upgrade(Req, Env, Handler, HandlerState, _Opts) -> + upgrade(Req, Env, Handler, HandlerState). + service_available(Req, State) -> expect(Req, State, service_available, true, fun known_methods/2, 503). -- cgit v1.2.3