Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade to cowboy 2.8.0 #1

Open
wants to merge 12 commits into
base: palaver
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
]}.

{deps, [
{cowboy, "1.0.3", {git, "https://github.com/ninenines/cowboy.git", {tag, "1.0.3"}}}
{cowboy, "2.8.0", {git, "https://github.com/ninenines/cowboy.git", {tag, "2.8.0"}}}
]}.

{project_plugins, [erlfmt]}.
125 changes: 31 additions & 94 deletions src/sockjs_cowboy_handler.erl
Original file line number Diff line number Diff line change
@@ -1,91 +1,50 @@
-module(sockjs_cowboy_handler).

-behaviour(cowboy_http_handler).

-behaviour(cowboy_websocket_handler).
-behaviour(cowboy_handler).

%% Cowboy http callbacks
-export([handle/2, init/3, terminate/3]).
-export([init/2, terminate/3]).

%% Cowboy ws callbacks
-export([
websocket_handle/3,
websocket_info/3,
websocket_init/3,
websocket_terminate/3
websocket_handle/2,
websocket_info/2,
websocket_init/1,
websocket_terminate/2
]).

-include("sockjs_internal.hrl").

%% --------------------------------------------------------------------------

init({_Any, http}, Req, Service) ->
init(#{ref := http} = Req, Service) ->
case sockjs_handler:is_valid_ws(Service, {cowboy, Req}) of
{true, {cowboy, _Req1}, _Reason} ->
{upgrade, protocol, cowboy_websocket};
{false, {cowboy, Req1}, _Reason} ->
{ok, Req1, Service}
{true, _Reason} ->
Service1 = Service#service{disconnect_delay = 5 * 60 * 1000},
Info = sockjs_handler:extract_info({cowboy, Req}),
SessionPid = sockjs_session:maybe_create(undefined, Service1, Info),
RawWebSocket =
case sockjs_handler:get_action(Service, {cowboy, Req}) of
{match, WS} when WS =:= websocket orelse WS =:= rawwebsocket -> WS
end,

{cowboy_websocket, Req, {RawWebSocket, SessionPid}};
{false, _Reason} ->
{ok, Req, {}}
end.

handle(Req, Service) ->
{cowboy, Req3} = sockjs_handler:handle_req(
Service,
{cowboy, Req}
),
{ok, Req3, Service}.

terminate(_Reason, _Req, _Service) -> ok.

%% --------------------------------------------------------------------------

websocket_init(
_TransportName,
Req,
Service = #service{
logger = Logger,
subproto_pref = SubProtocolPref
}
S
) ->
Req3 =
case cowboy_req:header(<<"Sec-Websocket-Protocol">>, Req) of
{undefined, Req1} ->
Req1;
{SubProtocols, Req1} ->
SelectedSubProtocol =
choose_subprotocol_bin(SubProtocols, SubProtocolPref),
{ok, Req2} =
cowboy_req:set_resp_header(
<<"Sec-Websocket-Protocol">>,
SelectedSubProtocol,
Req1
),
Req2
end,
Req4 = Logger(Service, {cowboy, Req3}, websocket),
Service1 = Service#service{
disconnect_delay =
5 * 60 * 1000
},
{Info, Req5} = sockjs_handler:extract_info(Req4),
SessionPid = sockjs_session:maybe_create(
undefined,
Service1,
Info
),
{RawWebsocket, {cowboy, Req7}} =
case sockjs_handler:get_action(Service, Req5) of
{{match, WS}, Req6} when
WS =:= websocket orelse
WS =:= rawwebsocket
->
{WS, Req6}
end,
self() ! go,
{ok, Req7, {RawWebsocket, SessionPid}}.
{ok, S}.

websocket_handle(
{text, Data},
Req,
{RawWebsocket, SessionPid} = S
) ->
case
Expand All @@ -95,56 +54,34 @@ websocket_handle(
Data
)
of
ok -> {ok, Req, S};
shutdown -> {shutdown, Req, S}
ok -> {[], S};
shutdown -> {stop, S}
end;
websocket_handle(_Unknown, Req, S) ->
{shutdown, Req, S}.
websocket_handle(_Unknown, S) ->
{stop, S}.

websocket_info(
go,
Req,
{RawWebsocket, SessionPid} = S
) ->
case sockjs_ws_handler:reply(RawWebsocket, SessionPid) of
wait ->
{ok, Req, S};
{[], S};
{ok, Data} ->
self() ! go,
{reply, {text, Data}, Req, S};
{reply, {text, Data}, S};
{close, <<>>} ->
{shutdown, Req, S};
{stop, S};
{close, Data} ->
self() ! shutdown,
{reply, {text, Data}, Req, S}
{reply, {text, Data}, S}
end;
websocket_info(shutdown, Req, S) ->
{shutdown, Req, S}.
websocket_info(shutdown, S) ->
{stop, S}.

websocket_terminate(
_Reason,
_Req,
{RawWebsocket, SessionPid}
) ->
sockjs_ws_handler:close(RawWebsocket, SessionPid),
ok.

%% --------------------------------------------------------------------------

choose_subprotocol_bin(SubProtocols, Pref) ->
choose_subprotocol(re:split(SubProtocols, ", *"), Pref).

choose_subprotocol(SubProtocols, undefined) ->
erlang:hd(lists:reverse(lists:sort(SubProtocols)));
choose_subprotocol(SubProtocols, Pref) ->
case
lists:filter(
fun(E) ->
lists:member(E, SubProtocols)
end,
Pref
)
of
[Hd | _] -> Hd;
[] -> choose_subprotocol(SubProtocols, undefined)
end.
6 changes: 3 additions & 3 deletions src/sockjs_filters.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
cache_for(Req, Headers) ->
Expires =
calendar:gregorian_seconds_to_datetime(
calendar:datetime_to_gregorian_seconds(calendar:now_to_datetime(now())) +
calendar:datetime_to_gregorian_seconds(calendar:now_to_datetime(erlang:timestamp())) +
(?YEAR)
),
H = [
Expand All @@ -36,15 +36,15 @@ h_sid(Req, Headers) ->
%% a JSESSIONID cookie. If this cookie isn't yet set, we shall
%% set it to a dumb value. It doesn't really matter what, as
%% session information is usually added by the load balancer.
{C, Req2} = sockjs_http:jsessionid(Req),
C = sockjs_http:jsessionid(Req),
H =
case C of
undefined ->
[{"Set-Cookie", "JSESSIONID=dummy; path=/"}];
Jsid ->
[{"Set-Cookie", "JSESSIONID=" ++ Jsid ++ "; path=/"}]
end,
{H ++ Headers, Req2}.
H ++ Headers.

-spec h_no_cache(req(), headers()) -> {headers(), req()}.

Expand Down
Loading