2017-01-02 19:36:36 +01:00
|
|
|
%% Copyright (c) 2011-2017, Loïc Hoguin <essen@ninenines.eu>
|
2013-01-03 22:47:51 +01:00
|
|
|
%%
|
|
|
|
%% 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.
|
|
|
|
|
2014-03-26 19:05:59 +01:00
|
|
|
%% Handler middleware.
|
2013-01-03 22:47:51 +01:00
|
|
|
%%
|
|
|
|
%% Execute the handler given by the <em>handler</em> and <em>handler_opts</em>
|
|
|
|
%% environment values. The result of this execution is added to the
|
|
|
|
%% environment under the <em>result</em> value.
|
|
|
|
-module(cowboy_handler).
|
|
|
|
-behaviour(cowboy_middleware).
|
|
|
|
|
|
|
|
-export([execute/2]).
|
2014-09-30 20:12:13 +03:00
|
|
|
-export([terminate/4]).
|
|
|
|
|
|
|
|
-callback init(Req, any())
|
|
|
|
-> {ok | module(), Req, any()}
|
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.
2017-02-18 18:26:20 +01:00
|
|
|
| {module(), Req, any(), any()}
|
2014-09-30 20:12:13 +03:00
|
|
|
when Req::cowboy_req:req().
|
2015-07-27 23:58:58 +02:00
|
|
|
|
2017-09-04 14:33:44 +02:00
|
|
|
-callback terminate(any(), map(), any()) -> ok.
|
2015-07-27 23:58:58 +02:00
|
|
|
-optional_callbacks([terminate/3]).
|
2013-01-03 22:47:51 +01:00
|
|
|
|
2014-09-26 15:58:44 +03:00
|
|
|
-spec execute(Req, Env) -> {ok, Req, Env}
|
2013-01-03 22:47:51 +01:00
|
|
|
when Req::cowboy_req:req(), Env::cowboy_middleware:env().
|
Initial commit with connection/streams
Breaking changes with previous commit. This is a very large change,
and I am giving up on making a single commit that fixes everything.
More commits will follow slowly adding back features, introducing
new tests and fixing the documentation.
This change contains most of the work toward unifying the interface
for handling both HTTP/1.1 and HTTP/2. HTTP/1.1 connections are now
no longer 1 process per connection; instead by default 1 process per
request is also created. This has a number of pros and cons.
Because it has cons, we also allow users to use a lower-level API
that acts on "streams" (requests/responses) directly at the connection
process-level. If performance is a concern, one can always write a
stream handler. The performance in this case will be even greater
than with Cowboy 1, although all the special handlers are unavailable.
When switching to Websocket, after the handler returns from init/2,
Cowboy stops the stream and the Websocket protocol takes over the
connection process. Websocket then calls websocket_init/2 for any
additional initialization such as timers, because the process is
different in init/2 and websocket_*/* functions. This however would
allow us to use websocket_init/2 for sending messages on connect,
instead of sending ourselves a message and be subject to races.
Note that websocket_init/2 is optional.
This is all a big change and while most of the tests pass, some
functionality currently doesn't. SPDY is broken and will be removed
soon in favor of HTTP/2. Automatic compression is currently disabled.
The cowboy_req interface probably still have a few functions that
need to be updated. The docs and examples do not refer the current
functionality anymore.
Everything will be fixed over time. Feedback is more than welcome.
Open a ticket!
2016-02-10 17:28:32 +01:00
|
|
|
execute(Req, Env=#{handler := Handler, handler_opts := HandlerOpts}) ->
|
2016-08-10 17:45:28 +02:00
|
|
|
try Handler:init(Req, HandlerOpts) of
|
2014-09-30 20:12:13 +03:00
|
|
|
{ok, Req2, State} ->
|
|
|
|
Result = terminate(normal, Req2, State, Handler),
|
2017-01-02 16:47:16 +01:00
|
|
|
{ok, Req2, Env#{result => Result}};
|
2014-09-26 15:58:44 +03:00
|
|
|
{Mod, Req2, State} ->
|
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.
2017-02-18 18:26:20 +01:00
|
|
|
Mod:upgrade(Req2, Env, Handler, State);
|
|
|
|
{Mod, Req2, State, Opts} ->
|
|
|
|
Mod:upgrade(Req2, Env, Handler, State, Opts)
|
2019-12-31 15:10:38 +01:00
|
|
|
catch Class:Reason:Stacktrace ->
|
2016-08-10 17:45:28 +02:00
|
|
|
terminate({crash, Class, Reason}, Req, HandlerOpts, Handler),
|
2019-12-31 15:10:38 +01:00
|
|
|
erlang:raise(Class, Reason, Stacktrace)
|
2013-01-03 22:47:51 +01:00
|
|
|
end.
|
|
|
|
|
2017-01-02 16:47:16 +01:00
|
|
|
-spec terminate(any(), Req | undefined, any(), module()) -> ok when Req::cowboy_req:req().
|
2014-09-30 20:12:13 +03:00
|
|
|
terminate(Reason, Req, State, Handler) ->
|
|
|
|
case erlang:function_exported(Handler, terminate, 3) of
|
2014-09-26 15:58:44 +03:00
|
|
|
true ->
|
2016-06-22 10:42:23 +02:00
|
|
|
Handler:terminate(Reason, Req, State);
|
2014-09-26 15:58:44 +03:00
|
|
|
false ->
|
|
|
|
ok
|
2014-09-30 20:12:13 +03:00
|
|
|
end.
|