2014-02-06 19:57:23 +01:00
|
|
|
%% Copyright (c) 2011-2014, 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()}
|
|
|
|
| {module(), Req, any(), hibernate}
|
|
|
|
| {module(), Req, any(), timeout()}
|
|
|
|
| {module(), Req, any(), timeout(), hibernate}
|
|
|
|
when Req::cowboy_req:req().
|
|
|
|
%% @todo optional -callback terminate(terminate_reason(), cowboy_req:req(), state()) -> ok.
|
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().
|
|
|
|
execute(Req, Env) ->
|
|
|
|
{_, Handler} = lists:keyfind(handler, 1, Env),
|
|
|
|
{_, HandlerOpts} = lists:keyfind(handler_opts, 1, Env),
|
2014-09-26 15:58:44 +03:00
|
|
|
try Handler:init(Req, HandlerOpts) of
|
2014-09-30 20:12:13 +03:00
|
|
|
{ok, Req2, State} ->
|
|
|
|
Result = terminate(normal, Req2, State, Handler),
|
|
|
|
{ok, Req2, [{result, Result}|Env]};
|
2014-09-26 15:58:44 +03:00
|
|
|
{Mod, Req2, State} ->
|
2014-09-30 20:12:13 +03:00
|
|
|
Mod:upgrade(Req2, Env, Handler, State, infinity, run);
|
2014-09-26 15:58:44 +03:00
|
|
|
{Mod, Req2, State, hibernate} ->
|
2014-09-30 20:12:13 +03:00
|
|
|
Mod:upgrade(Req2, Env, Handler, State, infinity, hibernate);
|
2014-09-26 15:58:44 +03:00
|
|
|
{Mod, Req2, State, Timeout} ->
|
2014-09-30 20:12:13 +03:00
|
|
|
Mod:upgrade(Req2, Env, Handler, State, Timeout, run);
|
2014-09-26 15:58:44 +03:00
|
|
|
{Mod, Req2, State, Timeout, hibernate} ->
|
2014-09-30 20:12:13 +03:00
|
|
|
Mod:upgrade(Req2, Env, Handler, State, Timeout, hibernate)
|
2013-01-03 22:47:51 +01:00
|
|
|
catch Class:Reason ->
|
2014-07-12 12:09:43 +02:00
|
|
|
Stacktrace = erlang:get_stacktrace(),
|
|
|
|
cowboy_req:maybe_reply(Stacktrace, Req),
|
2014-09-30 20:12:13 +03:00
|
|
|
terminate({crash, Class, Reason}, Req, HandlerOpts, Handler),
|
2013-08-24 20:36:23 +02:00
|
|
|
erlang:Class([
|
|
|
|
{reason, Reason},
|
2014-09-26 15:58:44 +03:00
|
|
|
{mfa, {Handler, init, 2}},
|
2014-07-12 12:09:43 +02:00
|
|
|
{stacktrace, Stacktrace},
|
2013-08-24 20:36:23 +02:00
|
|
|
{req, cowboy_req:to_list(Req)},
|
|
|
|
{opts, HandlerOpts}
|
|
|
|
])
|
2013-01-03 22:47:51 +01:00
|
|
|
end.
|
|
|
|
|
2014-09-30 20:12:13 +03:00
|
|
|
-spec terminate(any(), Req, any(), module()) -> ok when Req::cowboy_req:req().
|
|
|
|
terminate(Reason, Req, State, Handler) ->
|
|
|
|
case erlang:function_exported(Handler, terminate, 3) of
|
2014-09-26 15:58:44 +03:00
|
|
|
true ->
|
|
|
|
try
|
|
|
|
Handler:terminate(Reason, cowboy_req:lock(Req), State)
|
|
|
|
catch Class:Reason2 ->
|
|
|
|
erlang:Class([
|
|
|
|
{reason, Reason2},
|
|
|
|
{mfa, {Handler, terminate, 3}},
|
|
|
|
{stacktrace, erlang:get_stacktrace()},
|
|
|
|
{req, cowboy_req:to_list(Req)},
|
|
|
|
{state, State},
|
|
|
|
{terminate_reason, Reason}
|
|
|
|
])
|
2013-02-11 09:03:13 +01:00
|
|
|
end;
|
2014-09-26 15:58:44 +03:00
|
|
|
false ->
|
|
|
|
ok
|
2014-09-30 20:12:13 +03:00
|
|
|
end.
|