From 32db544782f2528ed0916eecb200f75924dcc407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 27 Apr 2017 15:23:57 +0200 Subject: 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. --- src/gun_content_handler.erl | 69 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/gun_content_handler.erl (limited to 'src/gun_content_handler.erl') 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 +%% +%% 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. -- cgit v1.2.3