1%% Copyright (c) 2017-2018, Loïc Hoguin <essen@ninenines.eu>
2%%
3%% Permission to use, copy, modify, and/or distribute this software for any
4%% purpose with or without fee is hereby granted, provided that the above
5%% copyright notice and this permission notice appear in all copies.
6%%
7%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15-module(gun_content_handler).
16
17-export([init/5]).
18-export([handle/3]).
19-export([check_option/1]).
20
21-type opt() :: [module() | {module(), map()}].
22-export_type([opt/0]).
23
24-type state() :: opt() | [{module(), any()}].
25-export_type([state/0]).
26
27-callback init(pid(), any(), cow_http:status(),
28	cow_http:headers(), map()) -> {ok, any()} | disable.
29%% @todo Make fin | nofin its own type.
30-callback handle(fin | nofin, any(), State)
31	-> {ok, any(), State} | {done, State} when State::any().
32
33-spec init(pid(), any(), cow_http:status(),
34	cow_http:headers(), State) -> State when State::state().
35init(_, _, _, _, []) ->
36	[];
37init(ReplyTo, StreamRef, Status, Headers, [Handler|Tail]) ->
38	{Mod, Opts} = case Handler of
39		Tuple = {_, _} -> Tuple;
40		Atom -> {Atom, #{}}
41	end,
42	case Mod:init(ReplyTo, StreamRef, Status, Headers, Opts) of
43		{ok, State} -> [{Mod, State}|init(ReplyTo, StreamRef, Status, Headers, Tail)];
44		disable -> init(ReplyTo, StreamRef, Status, Headers, Tail)
45	end.
46
47-spec handle(fin | nofin, any(), State) -> State when State::state().
48handle(_, _, []) ->
49	[];
50handle(IsFin, Data0, [{Mod, State0}|Tail]) ->
51	case Mod:handle(IsFin, Data0, State0) of
52		{ok, Data, State} -> [{Mod, State}|handle(IsFin, Data, Tail)];
53		{done, State} -> [{Mod, State}|Tail]
54	end.
55
56-spec check_option(list()) -> ok | error.
57check_option([]) ->
58	error;
59check_option(Opt) ->
60	check_option1(Opt).
61
62check_option1([]) ->
63	ok;
64check_option1([Atom|Tail]) when is_atom(Atom) ->
65	check_option1(Tail);
66check_option1([{Atom, #{}}|Tail]) when is_atom(Atom) ->
67	check_option1(Tail);
68check_option1(_) ->
69	error.
70