aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_http_websocket.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/cowboy_http_websocket.erl')
-rw-r--r--src/cowboy_http_websocket.erl43
1 files changed, 18 insertions, 25 deletions
diff --git a/src/cowboy_http_websocket.erl b/src/cowboy_http_websocket.erl
index 4e12358..9a1a17a 100644
--- a/src/cowboy_http_websocket.erl
+++ b/src/cowboy_http_websocket.erl
@@ -19,22 +19,21 @@
-record(state, {
handler :: module(),
- opts :: term(),
+ opts :: any(),
origin = undefined :: undefined | binary(),
challenge = undefined :: undefined | binary(),
timeout = infinity :: timeout(),
messages = undefined :: undefined | {atom(), atom(), atom()}
}).
--spec upgrade(Handler::module(), Opts::term(), Req::#http_req{}) -> ok.
+-spec upgrade(module(), any(), #http_req{}) -> ok.
upgrade(Handler, Opts, Req) ->
case catch websocket_upgrade(#state{handler=Handler, opts=Opts}, Req) of
{ok, State, Req2} -> handler_init(State, Req2);
{'EXIT', _Reason} -> upgrade_error(Req)
end.
--spec websocket_upgrade(State::#state{}, Req::#http_req{})
- -> {ok, State::#state{}, Req::#http_req{}}.
+-spec websocket_upgrade(#state{}, #http_req{}) -> {ok, #state{}, #http_req{}}.
websocket_upgrade(State, Req) ->
{<<"Upgrade">>, Req2} = cowboy_http_req:header('Connection', Req),
{<<"WebSocket">>, Req3} = cowboy_http_req:header('Upgrade', Req2),
@@ -46,19 +45,19 @@ websocket_upgrade(State, Req) ->
Challenge = challenge(Key1, Key2, Key3),
{ok, State#state{origin=Origin, challenge=Challenge}, Req7}.
--spec challenge(Key1::binary(), Key2::binary(), Key3::binary()) -> binary().
+-spec challenge(binary(), binary(), binary()) -> binary().
challenge(Key1, Key2, Key3) ->
IntKey1 = key_to_integer(Key1),
IntKey2 = key_to_integer(Key2),
erlang:md5(<< IntKey1:32, IntKey2:32, Key3/binary >>).
--spec key_to_integer(Key::binary()) -> integer().
+-spec key_to_integer(binary()) -> integer().
key_to_integer(Key) ->
Number = list_to_integer([C || << C >> <= Key, C >= $0, C =< $9]),
Spaces = length([C || << C >> <= Key, C =:= 32]),
Number div Spaces.
--spec handler_init(State::#state{}, Req::#http_req{}) -> ok.
+-spec handler_init(#state{}, #http_req{}) -> ok.
handler_init(State=#state{handler=Handler, opts=Opts},
Req=#http_req{transport=Transport}) ->
case catch Handler:websocket_init(Transport:name(), Req, Opts) of
@@ -71,14 +70,13 @@ handler_init(State=#state{handler=Handler, opts=Opts},
upgrade_error(Req)
end.
--spec upgrade_error(Req::#http_req{}) -> ok.
+-spec upgrade_error(#http_req{}) -> ok.
upgrade_error(Req=#http_req{socket=Socket, transport=Transport}) ->
{ok, _Req} = cowboy_http_req:reply(400, [], [],
Req#http_req{resp_state=waiting}),
Transport:close(Socket).
--spec websocket_handshake(State::#state{}, Req::#http_req{},
- HandlerState::term()) -> ok.
+-spec websocket_handshake(#state{}, #http_req{}, any()) -> ok.
websocket_handshake(State=#state{origin=Origin, challenge=Challenge},
Req=#http_req{transport=Transport, raw_host=Host, port=Port,
raw_path=Path}, HandlerState) ->
@@ -93,8 +91,8 @@ websocket_handshake(State=#state{origin=Origin, challenge=Challenge},
handler_loop(State#state{messages=Transport:messages()},
Req2, HandlerState, <<>>).
--spec websocket_location(TransportName::atom(), Host::binary(),
- Port::ip_port(), Path::binary()) -> binary().
+-spec websocket_location(atom(), binary(), inet:ip_port(), binary())
+ -> binary().
websocket_location(ssl, Host, Port, Path) ->
<< "wss://", Host/binary, ":",
(list_to_binary(integer_to_list(Port)))/binary, Path/binary >>;
@@ -102,8 +100,7 @@ websocket_location(_Any, Host, Port, Path) ->
<< "ws://", Host/binary, ":",
(list_to_binary(integer_to_list(Port)))/binary, Path/binary >>.
--spec handler_loop(State::#state{}, Req::#http_req{},
- HandlerState::term(), SoFar::binary()) -> ok.
+-spec handler_loop(#state{}, #http_req{}, any(), binary()) -> ok.
handler_loop(State=#state{messages={OK, Closed, Error}, timeout=Timeout},
Req=#http_req{socket=Socket, transport=Transport},
HandlerState, SoFar) ->
@@ -123,8 +120,7 @@ handler_loop(State=#state{messages={OK, Closed, Error}, timeout=Timeout},
websocket_close(State, Req, HandlerState, {normal, timeout})
end.
--spec websocket_data(State::#state{}, Req::#http_req{},
- HandlerState::term(), Data::binary()) -> ok.
+-spec websocket_data(#state{}, #http_req{}, any(), binary()) -> ok.
websocket_data(State, Req, HandlerState, << 255, 0, _Rest/bits >>) ->
websocket_close(State, Req, HandlerState, {normal, closed});
websocket_data(State, Req, HandlerState, Data) when byte_size(Data) < 3 ->
@@ -133,8 +129,7 @@ websocket_data(State, Req, HandlerState, Data) ->
websocket_frame(State, Req, HandlerState, Data, binary:first(Data)).
%% We do not support any frame type other than 0 yet. Just like the specs.
--spec websocket_frame(State::#state{}, Req::#http_req{},
- HandlerState::term(), Data::binary(), FrameType::byte()) -> ok.
+-spec websocket_frame(#state{}, #http_req{}, any(), binary(), byte()) -> ok.
websocket_frame(State, Req, HandlerState, Data, 0) ->
case binary:match(Data, << 255 >>) of
{Pos, 1} ->
@@ -149,8 +144,7 @@ websocket_frame(State, Req, HandlerState, Data, 0) ->
websocket_frame(State, Req, HandlerState, _Data, _FrameType) ->
websocket_close(State, Req, HandlerState, {error, badframe}).
--spec handler_call(State::#state{}, Req::#http_req{}, HandlerState::term(),
- RemainingData::binary(), Message::term(), NextState::fun()) -> ok.
+-spec handler_call(#state{}, #http_req{}, any(), binary(), any(), fun()) -> ok.
handler_call(State=#state{handler=Handler}, Req, HandlerState,
RemainingData, Message, NextState) ->
case catch Handler:websocket_handle(Message, Req, HandlerState) of
@@ -165,19 +159,18 @@ handler_call(State=#state{handler=Handler}, Req, HandlerState,
websocket_close(State, Req, HandlerState, {error, handler})
end.
--spec websocket_send(Data::binary(), Req::#http_req{}) -> ok.
+-spec websocket_send(binary(), #http_req{}) -> ok.
websocket_send(Data, #http_req{socket=Socket, transport=Transport}) ->
Transport:send(Socket, << 0, Data/binary, 255 >>).
--spec websocket_close(State::#state{}, Req::#http_req{},
- HandlerState::term(), Reason::{atom(), atom()}) -> ok.
+-spec websocket_close(#state{}, #http_req{}, any(), {atom(), atom()}) -> ok.
websocket_close(State, Req=#http_req{socket=Socket, transport=Transport},
HandlerState, Reason) ->
Transport:send(Socket, << 255, 0 >>),
Transport:close(Socket),
handler_terminate(State, Req, HandlerState, Reason).
--spec handler_terminate(State::#state{}, Req::#http_req{},
- HandlerState::term(), Reason::atom() | {atom(), atom()}) -> ok.
+-spec handler_terminate(#state{}, #http_req{},
+ any(), atom() | {atom(), atom()}) -> ok.
handler_terminate(#state{handler=Handler}, Req, HandlerState, Reason) ->
Handler:websocket_terminate(Reason, Req, HandlerState).