aboutsummaryrefslogtreecommitdiffstats
path: root/src/gun_content_handler.erl
blob: fa180d88c550ef9e21000b14b8f75b594de4b1c5 (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
%% Copyright (c) 2017-2019, 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.

-module(gun_content_handler).

-export([init/5]).
-export([handle/3]).
-export([check_option/1]).

-type opt() :: [module() | {module(), map()}].
-export_type([opt/0]).

-type state() :: opt() | [{module(), any()}].
-export_type([state/0]).

-callback init(pid(), any(), cow_http:status(),
	cow_http:headers(), map()) -> {ok, any()} | disable.
%% @todo Make fin | nofin its own type.
-callback handle(fin | nofin, any(), State)
	-> {ok, any(), non_neg_integer(), State}
	| {done, non_neg_integer(), State}
	when State::any().

-spec init(pid(), any(), cow_http:status(),
	cow_http:headers(), State) -> State when State::state().
init(_, _, _, _, []) ->
	[];
init(ReplyTo, StreamRef, Status, Headers, [Handler|Tail]) ->
	{Mod, Opts} = case Handler of
		Tuple = {_, _} -> Tuple;
		Atom -> {Atom, #{}}
	end,
	case Mod:init(ReplyTo, StreamRef, Status, Headers, Opts) of
		{ok, State} -> [{Mod, State}|init(ReplyTo, StreamRef, Status, Headers, Tail)];
		disable -> init(ReplyTo, StreamRef, Status, Headers, Tail)
	end.

-spec handle(fin | nofin, any(), State) -> {ok, non_neg_integer(), State} when State::state().
handle(IsFin, Data, State) ->
	handle(IsFin, Data, State, 0, []).

handle(_, _, [], Flow, Acc) ->
	{ok, Flow, lists:reverse(Acc)};
handle(IsFin, Data0, [{Mod, State0}|Tail], Flow, Acc) ->
	case Mod:handle(IsFin, Data0, State0) of
		{ok, Data, Inc, State} ->
			handle(IsFin, Data, Tail, Flow + Inc, [{Mod, State}|Acc]);
		{done, Inc, State} ->
			{ok, Flow + Inc, lists:reverse([{Mod, State}|Acc], Tail)}
	end.

-spec check_option(list()) -> ok | error.
check_option([]) ->
	error;
check_option(Opt) ->
	check_option1(Opt).

check_option1([]) ->
	ok;
check_option1([Atom|Tail]) when is_atom(Atom) ->
	check_option1(Tail);
check_option1([{Atom, #{}}|Tail]) when is_atom(Atom) ->
	check_option1(Tail);
check_option1(_) ->
	error.