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

Support OTP 25 #56

Open
wants to merge 3 commits into
base: master
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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include erlang.mk

test-deps:
git clone https://github.com/extend/cowboy.git test-deps/cowboy
pushd test-deps/cowboy; git checkout 0.9.0; make; popd
pushd test-deps/cowboy; git checkout 2.9.0; make; popd

test: test-deps all
mkdir -p .ct_results
Expand Down
68 changes: 34 additions & 34 deletions ct/echo_server.erl
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
-module(echo_server).

-behaviour(cowboy_websocket_handler).
-behaviour(cowboy_websocket).

-export([
start/0
]).

-export([
init/3,
websocket_init/3,
websocket_handle/3,
websocket_info/3,
websocket_terminate/3
init/2,
websocket_init/1,
websocket_handle/2,
websocket_info/2,
terminate/3
]).

-record(state, {}).
-record(state, {
req :: cowboy_req:req(),
query_params :: proplists:proplist()
}).

start() ->
io:format("Starting echo server.~n"),
Dispatch = cowboy_router:compile([{'_', [
{"/hello", ?MODULE, []},
{'_', ?MODULE, []}
]}]),
{ok, _} = cowboy:start_http(echo_listener, 2, [
{ok, _} = cowboy:start_clear(echo_listener,[
{port, 8080},
{max_connections, 100}
],
[{env, [{dispatch, Dispatch}]}]),
#{env => #{dispatch => Dispatch}}),
ok.

init(_, _Req, _Opts) ->
{upgrade, protocol, cowboy_websocket}.

websocket_init(_Transport, Req, _Opts) ->
case cowboy_req:qs_val(<<"code">>, Req) of
{undefined, Req2} ->
case cowboy_req:qs_val(<<"q">>, Req2) of
{undefined, Req3} ->
{ok, Req3, #state{}};
{Text, Req3} ->
self() ! {send, Text},
{ok, Req3, #state{}}
end;
{Code, Req2} ->
init(Req, _Opts) ->
QueryParams = cowboy_req:parse_qs(Req),
State = #state{req = Req, query_params = QueryParams},
Result = case proplists:lookup(<<"code">>, QueryParams) of
none ->
{cowboy_websocket, Req, State};
{<<"code">>, Code} ->
IntegerCode = list_to_integer(binary_to_list(Code)),
io:format("Shuting down on init using '~p' status code~n", [IntegerCode]),
{ok, Req3} = cowboy_req:reply(IntegerCode, Req2),
{shutdown, Req3}
{ok, cowboy_req:reply(IntegerCode, Req), State}
end,
Result.

websocket_init(#state{query_params = QueryParams} = State) ->
case proplists:lookup(<<"q">>, QueryParams) of
none ->
{ok, State};
{<<"q">>, Text} ->
{[{text, Text}], State}
end.

websocket_handle(Frame, Req, State) ->
websocket_handle(Frame, State) ->
io:format("Received frame~n"),
{reply, Frame, Req, State}.

websocket_info({send, Text}, Req, State) ->
io:format("Sent frame~n"),
{reply, {text, Text}, Req, State};
{reply, Frame, State}.

websocket_info(_Msg, Req, State) ->
{ok, Req, State}.
websocket_info(_Msg, State) ->
{ok, State}.

websocket_terminate(Reason, _Req, _State) ->
terminate(_State, _PartialReq, Reason) ->
io:format("Server terminating with reason ~p~n", [Reason]),
ok.
39 changes: 29 additions & 10 deletions src/websocket_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ start_link(URL, Handler, HandlerArgs, AsyncStart) when is_boolean(AsyncStart) ->
start_link(URL, Handler, HandlerArgs, Opts) when is_binary(URL) ->
start_link(erlang:binary_to_list(URL), Handler, HandlerArgs, Opts);
start_link(URL, Handler, HandlerArgs, Opts) when is_list(Opts) ->
case http_uri:parse(URL, [{scheme_defaults, [{ws,80},{wss,443}]}]) of
{ok, {Protocol, _, Host, Port, Path, Query}} ->
case url_parse(URL) of
{ok, {Protocol, Host, Port, Path}} ->
proc_lib:start_link(?MODULE, ws_client_init,
[Handler, Protocol, Host, Port, Path ++ Query, HandlerArgs, Opts]);
{error, _} = Error ->
Error
[Handler, Protocol, Host, Port, Path, HandlerArgs, Opts]);
{error, Reason, _} ->
{error, Reason}
end.

%% Send a frame asynchronously
Expand Down Expand Up @@ -220,22 +220,22 @@ websocket_close(WSReq, HandlerState, Reason) ->
_ ->
case Reason of
normal -> ok;
_ -> error_info(Handler, Reason, HandlerState)
_ -> error_info(Handler, Reason, HandlerState, [])
end,
exit(Reason)
catch
_:Reason2 ->
error_info(Handler, Reason2, HandlerState),
_:Reason2:Stacktrace ->
error_info(Handler, Reason2, HandlerState, Stacktrace),
exit(Reason2)
end.

error_info(Handler, Reason, State) ->
error_info(Handler, Reason, State, Stacktrace) ->
error_logger:error_msg(
"** Websocket handler ~p terminating~n"
"** for the reason ~p~n"
"** Handler state was ~p~n"
"** Stacktrace: ~p~n~n",
[Handler, Reason, State, erlang:get_stacktrace()]).
[Handler, Reason, State, Stacktrace]).

%% @doc Key sent in initial handshake
-spec generate_ws_key() ->
Expand Down Expand Up @@ -482,3 +482,22 @@ set_continuation_if_empty(WSReq, Opcode) ->
_ ->
WSReq
end.

url_parse(URL) ->
url_normalize(uri_string:parse(URL)).

url_normalize(#{scheme := Scheme}) when Scheme =/= "ws" andalso Scheme =/= "wss" ->
{error, invalid_scheme, Scheme};
url_normalize(#{path := ""} = UriMap)->
url_normalize(UriMap#{path := "/"});
url_normalize(#{scheme := Scheme, host := Host, path := Path} = UriMap) ->
Protocol = list_to_existing_atom(Scheme),
Port = maps:get(port, UriMap, default_port(Protocol)),
QueryString = case UriMap of
#{query := Query} -> "?" ++ Query;
_ -> ""
end,
{ok, {Protocol, Host, Port, Path ++ QueryString}}.

default_port(ws) -> 80;
default_port(wss) -> 443.