aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_handler.erl
blob: a6667143e868355a38665c2e03add0a9f4598ed4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]>
%%
%% 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.

%% Handler middleware.
%%
%% 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]).
-export([terminate/5]).

-spec execute(Req, Env) -> {ok, Req, Env}
	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),
	try Handler:init(Req, HandlerOpts) of
		{http, Req2, State} ->
			handle(Req2, Env, Handler, State);
		{shutdown, Req2, State} ->
			terminate(Req2, Env, Handler, State, {normal, shutdown});
		{Mod, Req2, State} ->
			upgrade(Req2, Env, Handler, State, infinity, run, Mod);
		{Mod, Req2, State, hibernate} ->
			upgrade(Req2, Env, Handler, State, infinity, hibernate, Mod);
		{Mod, Req2, State, Timeout} ->
			upgrade(Req2, Env, Handler, State, Timeout, run, Mod);
		{Mod, Req2, State, Timeout, hibernate} ->
			upgrade(Req2, Env, Handler, State, Timeout, hibernate, Mod)
	catch Class:Reason ->
		Stacktrace = erlang:get_stacktrace(),
		cowboy_req:maybe_reply(Stacktrace, Req),
		erlang:Class([
			{reason, Reason},
			{mfa, {Handler, init, 2}},
			{stacktrace, Stacktrace},
			{req, cowboy_req:to_list(Req)},
			{opts, HandlerOpts}
		])
	end.

handle(Req, Env, Handler, State) ->
	try Handler:handle(Req, State) of
		{ok, Req2, State2} ->
			terminate(Req2, Env, Handler, State2, {normal, shutdown})
	catch Class:Reason ->
		Stacktrace = erlang:get_stacktrace(),
		cowboy_req:maybe_reply(Stacktrace, Req),
		_ = terminate(Req, Env, Handler, State, Reason),
		erlang:Class([
			{reason, Reason},
			{mfa, {Handler, handle, 2}},
			{stacktrace, Stacktrace},
			{req, cowboy_req:to_list(Req)},
			{state, State}
		])
	end.

upgrade(Req, Env, Handler, State, Timeout, Hibernate, Mod) ->
	Mod2 = case Mod of
		long_polling -> cowboy_long_polling;
		rest -> cowboy_rest;
		ws -> cowboy_websocket;
		_ when Mod =/= http -> Mod
	end,
	Mod2:upgrade(Req, Env, Handler, State, Timeout, Hibernate).

-spec terminate(Req, Env, module(), any(), {normal, shutdown} | {error, atom()} | any())
	-> {ok, Req, Env} when Req::cowboy_req:req(), Env::cowboy_middleware:env().
terminate(Req, Env, Handler, State, Reason) ->
	Result = case erlang:function_exported(Handler, terminate, 3) of
		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}
				])
			end;
		false ->
			ok
	end,
	{ok, Req, [{result, Result}|Env]}.