aboutsummaryrefslogtreecommitdiffstats
path: root/src/gun_content_handler.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2017-04-27 15:23:57 +0200
committerLoïc Hoguin <[email protected]>2017-04-27 15:23:57 +0200
commit32db544782f2528ed0916eecb200f75924dcc407 (patch)
treeef8746ab021a8172ac84e9f72062922ec4263619 /src/gun_content_handler.erl
parente8c08c95b896bf9d2dd299e5fdbff50f714e8749 (diff)
downloadgun-32db544782f2528ed0916eecb200f75924dcc407.tar.gz
gun-32db544782f2528ed0916eecb200f75924dcc407.tar.bz2
gun-32db544782f2528ed0916eecb200f75924dcc407.zip
Add content handlers and built-in SSE support
Content handlers are a chain of modules implementing callbacks that receive the body of responses and may modify it (for example for decompressing the content) or act upon it (like sending a message to the owner process. The gun_sse content handler module can be used to translate text/event-stream events on the fly and deliver them to the owner process as a {gun_sse...} message. This feature is currently not documented and is only tested against a public server. It requires an up to date Cowlib.
Diffstat (limited to 'src/gun_content_handler.erl')
-rw-r--r--src/gun_content_handler.erl69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/gun_content_handler.erl b/src/gun_content_handler.erl
new file mode 100644
index 0000000..b268f56
--- /dev/null
+++ b/src/gun_content_handler.erl
@@ -0,0 +1,69 @@
+%% Copyright (c) 2017, 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(), reference(), 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(), State} | {done, State} when State::any().
+
+-spec init(pid(), reference(), cow_http:status(),
+ cow_http:headers(), State) -> State when State::state().
+init(_, _, _, _, []) ->
+ [];
+init(ReplyTo, Owner, Status, Headers, [Handler|Tail]) ->
+ {Mod, Opts} = case Handler of
+ Tuple = {_, _} -> Tuple;
+ Atom -> {Atom, #{}}
+ end,
+ case Mod:init(ReplyTo, Owner, Status, Headers, Opts) of
+ {ok, State} -> [{Mod, State}|init(ReplyTo, Owner, Status, Headers, Tail)];
+ disable -> init(ReplyTo, Owner, Status, Headers, Tail)
+ end.
+
+-spec handle(fin | nofin, any(), State) -> State when State::state().
+handle(_, _, []) ->
+ [];
+handle(IsFin, Data0, [{Mod, State0}|Tail]) ->
+ case Mod:handle(IsFin, Data0, State0) of
+ {ok, Data, State} -> [{Mod, State}|handle(IsFin, Data, Tail)];
+ {done, State} -> [{Mod, State}|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.