From 1be0151ec7b6a98064e648d5598f56cbdec65dc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Sun, 3 Jun 2018 21:11:28 +0200 Subject: Rename gun_data and gun_sse to gun_data_h and gun_sse_h --- doc/src/manual/gun.asciidoc | 4 ++-- ebin/gun.app | 2 +- src/gun_data.erl | 33 --------------------------- src/gun_data_h.erl | 33 +++++++++++++++++++++++++++ src/gun_http.erl | 2 +- src/gun_http2.erl | 2 +- src/gun_sse.erl | 55 --------------------------------------------- src/gun_sse_h.erl | 55 +++++++++++++++++++++++++++++++++++++++++++++ test/sse_SUITE.erl | 4 ++-- 9 files changed, 95 insertions(+), 95 deletions(-) delete mode 100644 src/gun_data.erl create mode 100644 src/gun_data_h.erl delete mode 100644 src/gun_sse.erl create mode 100644 src/gun_sse_h.erl diff --git a/doc/src/manual/gun.asciidoc b/doc/src/manual/gun.asciidoc index 45f65ab..b5ed039 100644 --- a/doc/src/manual/gun.asciidoc +++ b/doc/src/manual/gun.asciidoc @@ -94,7 +94,7 @@ Configuration for the HTTP protocol. The default value is given next to the option name: -// @todo Document content_handlers and gun_sse. +// @todo Document content_handlers and gun_sse_h. keepalive (5000):: @@ -129,7 +129,7 @@ Configuration for the HTTP/2 protocol. The default value is given next to the option name: -// @todo Document content_handlers and gun_sse. +// @todo Document content_handlers and gun_sse_h. keepalive (5000):: diff --git a/ebin/gun.app b/ebin/gun.app index de3896c..f40aefd 100644 --- a/ebin/gun.app +++ b/ebin/gun.app @@ -1,7 +1,7 @@ {application, 'gun', [ {description, "HTTP/1.1, HTTP/2 and Websocket client for Erlang/OTP."}, {vsn, "1.0.0-pre.5"}, - {modules, ['gun','gun_app','gun_content_handler','gun_data','gun_http','gun_http2','gun_sse','gun_sup','gun_ws','gun_ws_h']}, + {modules, ['gun','gun_app','gun_content_handler','gun_data_h','gun_http','gun_http2','gun_sse_h','gun_sup','gun_ws','gun_ws_h']}, {registered, [gun_sup]}, {applications, [kernel,stdlib,ssl,cowlib,ranch]}, {mod, {gun_app, []}}, diff --git a/src/gun_data.erl b/src/gun_data.erl deleted file mode 100644 index e70c25e..0000000 --- a/src/gun_data.erl +++ /dev/null @@ -1,33 +0,0 @@ -%% 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_data). --behavior(gun_content_handler). - --export([init/5]). --export([handle/3]). - --record(state, { - reply_to :: pid(), - stream_ref :: reference() -}). - --spec init(pid(), reference(), _, _, _) -> {ok, #state{}}. -init(ReplyTo, StreamRef, _, _, _) -> - {ok, #state{reply_to=ReplyTo, stream_ref=StreamRef}}. - --spec handle(fin | nofin, binary(), State) -> {done, State} when State::#state{}. -handle(IsFin, Data, State=#state{reply_to=ReplyTo, stream_ref=StreamRef}) -> - ReplyTo ! {gun_data, self(), StreamRef, IsFin, Data}, - {done, State}. diff --git a/src/gun_data_h.erl b/src/gun_data_h.erl new file mode 100644 index 0000000..826d70c --- /dev/null +++ b/src/gun_data_h.erl @@ -0,0 +1,33 @@ +%% 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_data_h). +-behavior(gun_content_handler). + +-export([init/5]). +-export([handle/3]). + +-record(state, { + reply_to :: pid(), + stream_ref :: reference() +}). + +-spec init(pid(), reference(), _, _, _) -> {ok, #state{}}. +init(ReplyTo, StreamRef, _, _, _) -> + {ok, #state{reply_to=ReplyTo, stream_ref=StreamRef}}. + +-spec handle(fin | nofin, binary(), State) -> {done, State} when State::#state{}. +handle(IsFin, Data, State=#state{reply_to=ReplyTo, stream_ref=StreamRef}) -> + ReplyTo ! {gun_data, self(), StreamRef, IsFin, Data}, + {done, State}. diff --git a/src/gun_http.erl b/src/gun_http.erl index 4fc6a17..2fd0aac 100644 --- a/src/gun_http.erl +++ b/src/gun_http.erl @@ -80,7 +80,7 @@ name() -> http. init(Owner, Socket, Transport, Opts) -> Version = maps:get(version, Opts, 'HTTP/1.1'), - Handlers = maps:get(content_handlers, Opts, [gun_data]), + Handlers = maps:get(content_handlers, Opts, [gun_data_h]), TransformHeaderName = maps:get(transform_header_name, Opts, fun (N) -> N end), #http_state{owner=Owner, socket=Socket, transport=Transport, version=Version, content_handlers=Handlers, transform_header_name=TransformHeaderName}. diff --git a/src/gun_http2.erl b/src/gun_http2.erl index 558dce5..1f25e83 100644 --- a/src/gun_http2.erl +++ b/src/gun_http2.erl @@ -96,7 +96,7 @@ do_check_options([Opt|_]) -> name() -> http2. init(Owner, Socket, Transport, Opts) -> - Handlers = maps:get(content_handlers, Opts, [gun_data]), + Handlers = maps:get(content_handlers, Opts, [gun_data_h]), State = #http2_state{owner=Owner, socket=Socket, transport=Transport, opts=Opts, content_handlers=Handlers}, #http2_state{local_settings=Settings} = State, diff --git a/src/gun_sse.erl b/src/gun_sse.erl deleted file mode 100644 index 4a898d4..0000000 --- a/src/gun_sse.erl +++ /dev/null @@ -1,55 +0,0 @@ -%% 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_sse). --behavior(gun_content_handler). - --export([init/5]). --export([handle/3]). - --record(state, { - reply_to :: pid(), - stream_ref :: reference(), - sse_state :: cow_sse:state() -}). - -%% @todo In the future we want to allow different media types. -%% @todo For text/event-stream specifically, the parameters must be ignored. - --spec init(pid(), reference(), _, cow_http:headers(), _) - -> {ok, #state{}} | disable. -init(ReplyTo, StreamRef, _, Headers, _) -> - case lists:keyfind(<<"content-type">>, 1, Headers) of - {_, <<"text/event-stream">>} -> - {ok, #state{reply_to=ReplyTo, stream_ref=StreamRef, - sse_state=cow_sse:init()}}; - _ -> - disable - end. - --spec handle(_, binary(), State) -> {done, State} when State::#state{}. -handle(IsFin, Data, State=#state{reply_to=ReplyTo, stream_ref=StreamRef, sse_state=SSE0}) -> - case cow_sse:parse(Data, SSE0) of - {event, Event, SSE} -> - ReplyTo ! {gun_sse, self(), StreamRef, Event}, - handle(IsFin, <<>>, State#state{sse_state=SSE}); - {more, SSE} -> - _ = case IsFin of - fin -> - ReplyTo ! {gun_sse, self(), StreamRef, fin}; - _ -> - ok - end, - {done, State#state{sse_state=SSE}} - end. diff --git a/src/gun_sse_h.erl b/src/gun_sse_h.erl new file mode 100644 index 0000000..a2ac809 --- /dev/null +++ b/src/gun_sse_h.erl @@ -0,0 +1,55 @@ +%% 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_sse_h). +-behavior(gun_content_handler). + +-export([init/5]). +-export([handle/3]). + +-record(state, { + reply_to :: pid(), + stream_ref :: reference(), + sse_state :: cow_sse:state() +}). + +%% @todo In the future we want to allow different media types. +%% @todo For text/event-stream specifically, the parameters must be ignored. + +-spec init(pid(), reference(), _, cow_http:headers(), _) + -> {ok, #state{}} | disable. +init(ReplyTo, StreamRef, _, Headers, _) -> + case lists:keyfind(<<"content-type">>, 1, Headers) of + {_, <<"text/event-stream">>} -> + {ok, #state{reply_to=ReplyTo, stream_ref=StreamRef, + sse_state=cow_sse:init()}}; + _ -> + disable + end. + +-spec handle(_, binary(), State) -> {done, State} when State::#state{}. +handle(IsFin, Data, State=#state{reply_to=ReplyTo, stream_ref=StreamRef, sse_state=SSE0}) -> + case cow_sse:parse(Data, SSE0) of + {event, Event, SSE} -> + ReplyTo ! {gun_sse, self(), StreamRef, Event}, + handle(IsFin, <<>>, State#state{sse_state=SSE}); + {more, SSE} -> + _ = case IsFin of + fin -> + ReplyTo ! {gun_sse, self(), StreamRef, fin}; + _ -> + ok + end, + {done, State#state{sse_state=SSE}} + end. diff --git a/test/sse_SUITE.erl b/test/sse_SUITE.erl index b218fa2..21180be 100644 --- a/test/sse_SUITE.erl +++ b/test/sse_SUITE.erl @@ -22,7 +22,7 @@ all() -> http(_) -> {ok, Pid} = gun:open("sse.now.sh", 443, #{ protocols => [http], - http_opts => #{content_handlers => [gun_sse, gun_data]} + http_opts => #{content_handlers => [gun_sse_h, gun_data_h]} }), {ok, http} = gun:await_up(Pid), common(Pid). @@ -30,7 +30,7 @@ http(_) -> http2(_) -> {ok, Pid} = gun:open("sse.now.sh", 443, #{ protocols => [http2], - http2_opts => #{content_handlers => [gun_sse, gun_data]} + http2_opts => #{content_handlers => [gun_sse_h, gun_data_h]} }), {ok, http2} = gun:await_up(Pid), common(Pid). -- cgit v1.2.3