2014-02-06 19:57:23 +01:00
|
|
|
%% Copyright (c) 2011-2014, Loïc Hoguin <essen@ninenines.eu>
|
2011-04-14 21:21:17 +02:00
|
|
|
%%
|
|
|
|
%% 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.
|
|
|
|
|
2013-01-10 21:58:38 +01:00
|
|
|
%% Cowboy supports versions 7 through 17 of the Websocket drafts.
|
|
|
|
%% It also supports RFC6455, the proposed standard for Websocket.
|
2012-08-27 14:00:28 +02:00
|
|
|
-module(cowboy_websocket).
|
2013-02-15 00:53:40 +00:00
|
|
|
-behaviour(cowboy_sub_protocol).
|
2011-07-06 17:42:20 +02:00
|
|
|
|
2014-09-26 15:58:44 +03:00
|
|
|
-export([upgrade/6]).
|
2012-08-27 12:16:07 +02:00
|
|
|
-export([handler_loop/4]).
|
2011-04-14 21:21:17 +02:00
|
|
|
|
2013-01-17 16:22:05 +01:00
|
|
|
-type close_code() :: 1000..4999.
|
|
|
|
-export_type([close_code/0]).
|
|
|
|
|
2012-12-02 21:37:24 +01:00
|
|
|
-type frame() :: close | ping | pong
|
2013-05-15 14:53:45 +02:00
|
|
|
| {text | binary | close | ping | pong, iodata()}
|
|
|
|
| {close, close_code(), iodata()}.
|
2012-12-02 21:37:24 +01:00
|
|
|
-export_type([frame/0]).
|
|
|
|
|
2011-08-23 16:24:02 +02:00
|
|
|
-type opcode() :: 0 | 1 | 2 | 8 | 9 | 10.
|
|
|
|
-type mask_key() :: 0..16#ffffffff.
|
2013-01-12 22:31:23 +01:00
|
|
|
-type frag_state() :: undefined
|
|
|
|
| {nofin, opcode(), binary()} | {fin, opcode(), binary()}.
|
2013-04-15 18:36:33 +02:00
|
|
|
-type rsv() :: << _:3 >>.
|
2014-09-30 20:12:13 +03:00
|
|
|
-type terminate_reason() :: normal | shutdown | timeout
|
|
|
|
| remote | {remote, close_code(), binary()}
|
|
|
|
| {error, badencoding | badframe | closed | atom()}
|
|
|
|
| {crash, error | exit | throw, any()}.
|
|
|
|
|
|
|
|
-callback init(Req, any())
|
|
|
|
-> {ok | module(), Req, any()}
|
|
|
|
| {module(), Req, any(), hibernate}
|
|
|
|
| {module(), Req, any(), timeout()}
|
|
|
|
| {module(), Req, any(), timeout(), hibernate}
|
|
|
|
when Req::cowboy_req:req().
|
|
|
|
-callback websocket_handle({text | binary | ping | pong, binary()}, Req, State)
|
|
|
|
-> {ok, Req, State}
|
|
|
|
| {ok, Req, State, hibernate}
|
|
|
|
| {reply, frame() | [frame()], Req, State}
|
|
|
|
| {reply, frame() | [frame()], Req, State, hibernate}
|
|
|
|
| {shutdown, Req, State}
|
|
|
|
when Req::cowboy_req:req(), State::any().
|
|
|
|
-callback websocket_info(any(), Req, State)
|
|
|
|
-> {ok, Req, State}
|
|
|
|
| {ok, Req, State, hibernate}
|
|
|
|
| {reply, frame() | [frame()], Req, State}
|
|
|
|
| {reply, frame() | [frame()], Req, State, hibernate}
|
|
|
|
| {shutdown, Req, State}
|
|
|
|
when Req::cowboy_req:req(), State::any().
|
|
|
|
%% @todo optional -callback terminate(terminate_reason(), cowboy_req:req(), state()) -> ok.
|
2012-04-01 18:58:28 -07:00
|
|
|
|
2011-04-14 21:21:17 +02:00
|
|
|
-record(state, {
|
2013-01-03 22:47:51 +01:00
|
|
|
env :: cowboy_middleware:env(),
|
2012-09-15 21:09:12 +02:00
|
|
|
socket = undefined :: inet:socket(),
|
|
|
|
transport = undefined :: module(),
|
2011-04-14 21:21:17 +02:00
|
|
|
handler :: module(),
|
2013-01-10 21:58:38 +01:00
|
|
|
key = undefined :: undefined | binary(),
|
2011-04-14 21:21:17 +02:00
|
|
|
timeout = infinity :: timeout(),
|
2011-09-22 21:33:56 +02:00
|
|
|
timeout_ref = undefined :: undefined | reference(),
|
2011-06-06 13:48:41 +02:00
|
|
|
messages = undefined :: undefined | {atom(), atom(), atom()},
|
2011-08-23 16:24:02 +02:00
|
|
|
hibernate = false :: boolean(),
|
2013-01-13 00:10:32 +01:00
|
|
|
frag_state = undefined :: frag_state(),
|
2013-04-15 18:36:33 +02:00
|
|
|
utf8_state = <<>> :: binary(),
|
|
|
|
deflate_frame = false :: boolean(),
|
2013-07-02 10:58:12 +02:00
|
|
|
inflate_state :: undefined | port(),
|
|
|
|
deflate_state :: undefined | port()
|
2011-04-14 21:21:17 +02:00
|
|
|
}).
|
|
|
|
|
2014-09-26 15:58:44 +03:00
|
|
|
-spec upgrade(Req, Env, module(), any(), timeout(), run | hibernate)
|
|
|
|
-> {ok, Req, Env} | {suspend, module(), atom(), [any()]}
|
2013-01-03 22:47:51 +01:00
|
|
|
when Req::cowboy_req:req(), Env::cowboy_middleware:env().
|
2014-09-26 15:58:44 +03:00
|
|
|
upgrade(Req, Env, Handler, HandlerState, Timeout, Hibernate) ->
|
2013-04-03 13:47:12 +02:00
|
|
|
{_, Ref} = lists:keyfind(listener, 1, Env),
|
|
|
|
ranch:remove_connection(Ref),
|
2013-01-05 22:04:52 +01:00
|
|
|
[Socket, Transport] = cowboy_req:get([socket, transport], Req),
|
2014-09-26 15:58:44 +03:00
|
|
|
State = #state{env=Env, socket=Socket, transport=Transport, handler=Handler,
|
|
|
|
timeout=Timeout, hibernate=Hibernate =:= hibernate},
|
2013-08-24 20:36:23 +02:00
|
|
|
try websocket_upgrade(State, Req) of
|
|
|
|
{ok, State2, Req2} ->
|
2014-09-26 15:58:44 +03:00
|
|
|
websocket_handshake(State2, Req2, HandlerState)
|
2013-08-24 20:36:23 +02:00
|
|
|
catch _:_ ->
|
2014-07-12 12:09:43 +02:00
|
|
|
receive
|
|
|
|
{cowboy_req, resp_sent} -> ok
|
|
|
|
after 0 ->
|
2014-07-12 14:19:29 +02:00
|
|
|
_ = cowboy_req:reply(400, Req),
|
2014-07-12 12:09:43 +02:00
|
|
|
exit(normal)
|
|
|
|
end
|
2011-04-14 21:21:17 +02:00
|
|
|
end.
|
|
|
|
|
2012-09-15 01:31:51 +02:00
|
|
|
-spec websocket_upgrade(#state{}, Req)
|
|
|
|
-> {ok, #state{}, Req} when Req::cowboy_req:req().
|
2011-05-05 14:03:39 +02:00
|
|
|
websocket_upgrade(State, Req) ->
|
2014-09-23 16:43:29 +03:00
|
|
|
ConnTokens = cowboy_req:parse_header(<<"connection">>, Req),
|
2011-10-20 19:04:49 +02:00
|
|
|
true = lists:member(<<"upgrade">>, ConnTokens),
|
2011-12-22 22:09:08 +01:00
|
|
|
%% @todo Should probably send a 426 if the Upgrade header is missing.
|
2014-09-23 16:43:29 +03:00
|
|
|
[<<"websocket">>] = cowboy_req:parse_header(<<"upgrade">>, Req),
|
|
|
|
Version = cowboy_req:header(<<"sec-websocket-version">>, Req),
|
2011-10-24 15:06:51 +01:00
|
|
|
IntVersion = list_to_integer(binary_to_list(Version)),
|
2013-01-10 21:58:38 +01:00
|
|
|
true = (IntVersion =:= 7) orelse (IntVersion =:= 8)
|
|
|
|
orelse (IntVersion =:= 13),
|
2014-09-23 16:43:29 +03:00
|
|
|
Key = cowboy_req:header(<<"sec-websocket-key">>, Req),
|
2013-01-10 21:58:38 +01:00
|
|
|
false = Key =:= undefined,
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_extensions(State#state{key=Key},
|
2014-09-23 16:43:29 +03:00
|
|
|
cowboy_req:set_meta(websocket_version, IntVersion, Req)).
|
2013-04-15 18:36:33 +02:00
|
|
|
|
|
|
|
-spec websocket_extensions(#state{}, Req)
|
|
|
|
-> {ok, #state{}, Req} when Req::cowboy_req:req().
|
|
|
|
websocket_extensions(State, Req) ->
|
|
|
|
case cowboy_req:parse_header(<<"sec-websocket-extensions">>, Req) of
|
2014-09-23 16:43:29 +03:00
|
|
|
undefined ->
|
|
|
|
{ok, State, cowboy_req:set_meta(websocket_compress, false, Req)};
|
|
|
|
Extensions ->
|
2013-04-15 18:36:33 +02:00
|
|
|
[Compress] = cowboy_req:get([resp_compress], Req),
|
|
|
|
case lists:keyfind(<<"x-webkit-deflate-frame">>, 1, Extensions) of
|
|
|
|
{<<"x-webkit-deflate-frame">>, []} when Compress =:= true ->
|
|
|
|
Inflate = zlib:open(),
|
|
|
|
Deflate = zlib:open(),
|
|
|
|
% Since we are negotiating an unconstrained deflate-frame
|
|
|
|
% then we must be willing to accept frames using the
|
|
|
|
% maximum window size which is 2^15. The negative value
|
|
|
|
% indicates that zlib headers are not used.
|
|
|
|
ok = zlib:inflateInit(Inflate, -15),
|
|
|
|
% Initialize the deflater with a window size of 2^15 bits and disable
|
|
|
|
% the zlib headers.
|
|
|
|
ok = zlib:deflateInit(Deflate, best_compression, deflated, -15, 8, default),
|
|
|
|
{ok, State#state{
|
|
|
|
deflate_frame = true,
|
|
|
|
inflate_state = Inflate,
|
|
|
|
deflate_state = Deflate
|
2014-09-23 16:43:29 +03:00
|
|
|
}, cowboy_req:set_meta(websocket_compress, true, Req)};
|
2013-04-15 18:36:33 +02:00
|
|
|
_ ->
|
2014-09-23 16:43:29 +03:00
|
|
|
{ok, State, cowboy_req:set_meta(websocket_compress, false, Req)}
|
|
|
|
end
|
2013-04-15 18:36:33 +02:00
|
|
|
end.
|
2011-04-14 21:21:17 +02:00
|
|
|
|
2013-01-03 22:47:51 +01:00
|
|
|
-spec websocket_handshake(#state{}, Req, any())
|
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
| {suspend, module(), atom(), [any()]}
|
|
|
|
when Req::cowboy_req:req().
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_handshake(State=#state{
|
|
|
|
transport=Transport, key=Key, deflate_frame=DeflateFrame},
|
2012-09-15 21:09:12 +02:00
|
|
|
Req, HandlerState) ->
|
2014-07-12 14:19:29 +02:00
|
|
|
Challenge = base64:encode(crypto:hash(sha,
|
2013-01-10 21:58:38 +01:00
|
|
|
<< Key/binary, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" >>)),
|
2013-04-15 18:36:33 +02:00
|
|
|
Extensions = case DeflateFrame of
|
|
|
|
false -> [];
|
|
|
|
true -> [{<<"sec-websocket-extensions">>, <<"x-webkit-deflate-frame">>}]
|
|
|
|
end,
|
2014-09-23 16:43:29 +03:00
|
|
|
Req2 = cowboy_req:upgrade_reply(101, [{<<"upgrade">>, <<"websocket">>},
|
|
|
|
{<<"sec-websocket-accept">>, Challenge}|Extensions], Req),
|
2012-01-02 09:41:30 +01:00
|
|
|
%% Flush the resp_sent message before moving on.
|
2012-08-27 13:28:57 +02:00
|
|
|
receive {cowboy_req, resp_sent} -> ok after 0 -> ok end,
|
2012-12-19 11:34:44 -08:00
|
|
|
State2 = handler_loop_timeout(State),
|
2013-01-10 21:58:38 +01:00
|
|
|
handler_before_loop(State2#state{key=undefined,
|
|
|
|
messages=Transport:messages()}, Req2, HandlerState, <<>>).
|
2011-04-14 21:21:17 +02:00
|
|
|
|
2013-01-03 22:47:51 +01:00
|
|
|
-spec handler_before_loop(#state{}, Req, any(), binary())
|
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
| {suspend, module(), atom(), [any()]}
|
|
|
|
when Req::cowboy_req:req().
|
2012-09-15 21:09:12 +02:00
|
|
|
handler_before_loop(State=#state{
|
|
|
|
socket=Socket, transport=Transport, hibernate=true},
|
|
|
|
Req, HandlerState, SoFar) ->
|
2011-04-14 21:21:17 +02:00
|
|
|
Transport:setopts(Socket, [{active, once}]),
|
2013-01-03 22:47:51 +01:00
|
|
|
{suspend, ?MODULE, handler_loop,
|
|
|
|
[State#state{hibernate=false}, Req, HandlerState, SoFar]};
|
2012-09-15 21:09:12 +02:00
|
|
|
handler_before_loop(State=#state{socket=Socket, transport=Transport},
|
|
|
|
Req, HandlerState, SoFar) ->
|
2011-06-06 18:15:36 +02:00
|
|
|
Transport:setopts(Socket, [{active, once}]),
|
2012-12-19 11:34:44 -08:00
|
|
|
handler_loop(State, Req, HandlerState, SoFar).
|
2011-09-22 21:33:56 +02:00
|
|
|
|
|
|
|
-spec handler_loop_timeout(#state{}) -> #state{}.
|
|
|
|
handler_loop_timeout(State=#state{timeout=infinity}) ->
|
|
|
|
State#state{timeout_ref=undefined};
|
|
|
|
handler_loop_timeout(State=#state{timeout=Timeout, timeout_ref=PrevRef}) ->
|
|
|
|
_ = case PrevRef of undefined -> ignore; PrevRef ->
|
|
|
|
erlang:cancel_timer(PrevRef) end,
|
2012-04-24 11:38:27 -07:00
|
|
|
TRef = erlang:start_timer(Timeout, self(), ?MODULE),
|
2011-09-22 21:33:56 +02:00
|
|
|
State#state{timeout_ref=TRef}.
|
2011-06-06 18:15:36 +02:00
|
|
|
|
2013-01-03 22:47:51 +01:00
|
|
|
-spec handler_loop(#state{}, Req, any(), binary())
|
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
| {suspend, module(), atom(), [any()]}
|
|
|
|
when Req::cowboy_req:req().
|
2013-01-12 22:31:23 +01:00
|
|
|
handler_loop(State=#state{socket=Socket, messages={OK, Closed, Error},
|
|
|
|
timeout_ref=TRef}, Req, HandlerState, SoFar) ->
|
2011-04-14 21:21:17 +02:00
|
|
|
receive
|
|
|
|
{OK, Socket, Data} ->
|
2012-12-19 11:34:44 -08:00
|
|
|
State2 = handler_loop_timeout(State),
|
|
|
|
websocket_data(State2, Req, HandlerState,
|
2011-04-14 21:21:17 +02:00
|
|
|
<< SoFar/binary, Data/binary >>);
|
|
|
|
{Closed, Socket} ->
|
|
|
|
handler_terminate(State, Req, HandlerState, {error, closed});
|
|
|
|
{Error, Socket, Reason} ->
|
|
|
|
handler_terminate(State, Req, HandlerState, {error, Reason});
|
2012-04-24 11:38:27 -07:00
|
|
|
{timeout, TRef, ?MODULE} ->
|
2014-09-30 20:12:13 +03:00
|
|
|
websocket_close(State, Req, HandlerState, timeout);
|
2012-04-24 11:38:27 -07:00
|
|
|
{timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) ->
|
2011-09-22 21:33:56 +02:00
|
|
|
handler_loop(State, Req, HandlerState, SoFar);
|
2011-04-14 21:21:17 +02:00
|
|
|
Message ->
|
|
|
|
handler_call(State, Req, HandlerState,
|
2011-07-19 12:12:25 +02:00
|
|
|
SoFar, websocket_info, Message, fun handler_before_loop/4)
|
2011-04-14 21:21:17 +02:00
|
|
|
end.
|
|
|
|
|
2013-01-14 16:20:33 +01:00
|
|
|
%% All frames passing through this function are considered valid,
|
|
|
|
%% with the only exception of text and close frames with a payload
|
|
|
|
%% which may still contain errors.
|
2013-01-03 22:47:51 +01:00
|
|
|
-spec websocket_data(#state{}, Req, any(), binary())
|
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
| {suspend, module(), atom(), [any()]}
|
|
|
|
when Req::cowboy_req:req().
|
2013-01-12 22:31:23 +01:00
|
|
|
%% RSV bits MUST be 0 unless an extension is negotiated
|
|
|
|
%% that defines meanings for non-zero values.
|
|
|
|
websocket_data(State, Req, HandlerState, << _:1, Rsv:3, _/bits >>)
|
2013-04-15 18:36:33 +02:00
|
|
|
when Rsv =/= 0, State#state.deflate_frame =:= false ->
|
2013-01-12 22:31:23 +01:00
|
|
|
websocket_close(State, Req, HandlerState, {error, badframe});
|
2013-01-14 16:20:33 +01:00
|
|
|
%% Invalid opcode. Note that these opcodes may be used by extensions.
|
|
|
|
websocket_data(State, Req, HandlerState, << _:4, Opcode:4, _/bits >>)
|
|
|
|
when Opcode > 2, Opcode =/= 8, Opcode =/= 9, Opcode =/= 10 ->
|
|
|
|
websocket_close(State, Req, HandlerState, {error, badframe});
|
2013-01-12 22:31:23 +01:00
|
|
|
%% Control frames MUST NOT be fragmented.
|
|
|
|
websocket_data(State, Req, HandlerState, << 0:1, _:3, Opcode:4, _/bits >>)
|
|
|
|
when Opcode >= 8 ->
|
|
|
|
websocket_close(State, Req, HandlerState, {error, badframe});
|
2013-01-14 16:20:33 +01:00
|
|
|
%% A frame MUST NOT use the zero opcode unless fragmentation was initiated.
|
2013-01-12 22:31:23 +01:00
|
|
|
websocket_data(State=#state{frag_state=undefined}, Req, HandlerState,
|
2013-01-14 16:20:33 +01:00
|
|
|
<< _:4, 0:4, _/bits >>) ->
|
2013-01-12 22:31:23 +01:00
|
|
|
websocket_close(State, Req, HandlerState, {error, badframe});
|
|
|
|
%% Non-control opcode when expecting control message or next fragment.
|
|
|
|
websocket_data(State=#state{frag_state={nofin, _, _}}, Req, HandlerState,
|
|
|
|
<< _:4, Opcode:4, _/bits >>)
|
|
|
|
when Opcode =/= 0, Opcode < 8 ->
|
|
|
|
websocket_close(State, Req, HandlerState, {error, badframe});
|
2013-01-14 16:20:33 +01:00
|
|
|
%% Close control frame length MUST be 0 or >= 2.
|
|
|
|
websocket_data(State, Req, HandlerState, << _:4, 8:4, _:1, 1:7, _/bits >>) ->
|
|
|
|
websocket_close(State, Req, HandlerState, {error, badframe});
|
|
|
|
%% Close control frame with incomplete close code. Need more data.
|
|
|
|
websocket_data(State, Req, HandlerState,
|
|
|
|
Data = << _:4, 8:4, 1:1, Len:7, _/bits >>)
|
|
|
|
when Len > 1, byte_size(Data) < 8 ->
|
|
|
|
handler_before_loop(State, Req, HandlerState, Data);
|
2013-01-12 22:31:23 +01:00
|
|
|
%% 7 bits payload length.
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_data(State, Req, HandlerState, << Fin:1, Rsv:3/bits, Opcode:4, 1:1,
|
2013-01-12 22:31:23 +01:00
|
|
|
Len:7, MaskKey:32, Rest/bits >>)
|
|
|
|
when Len < 126 ->
|
2012-04-01 18:58:28 -07:00
|
|
|
websocket_data(State, Req, HandlerState,
|
2013-04-15 18:36:33 +02:00
|
|
|
Opcode, Len, MaskKey, Rest, Rsv, Fin);
|
2013-01-12 22:31:23 +01:00
|
|
|
%% 16 bits payload length.
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_data(State, Req, HandlerState, << Fin:1, Rsv:3/bits, Opcode:4, 1:1,
|
2013-01-12 22:31:23 +01:00
|
|
|
126:7, Len:16, MaskKey:32, Rest/bits >>)
|
|
|
|
when Len > 125, Opcode < 8 ->
|
2012-04-01 18:58:28 -07:00
|
|
|
websocket_data(State, Req, HandlerState,
|
2013-04-15 18:36:33 +02:00
|
|
|
Opcode, Len, MaskKey, Rest, Rsv, Fin);
|
2013-01-12 22:31:23 +01:00
|
|
|
%% 63 bits payload length.
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_data(State, Req, HandlerState, << Fin:1, Rsv:3/bits, Opcode:4, 1:1,
|
2013-01-12 22:31:23 +01:00
|
|
|
127:7, 0:1, Len:63, MaskKey:32, Rest/bits >>)
|
|
|
|
when Len > 16#ffff, Opcode < 8 ->
|
2012-04-01 18:58:28 -07:00
|
|
|
websocket_data(State, Req, HandlerState,
|
2013-04-15 18:36:33 +02:00
|
|
|
Opcode, Len, MaskKey, Rest, Rsv, Fin);
|
2013-01-12 22:31:23 +01:00
|
|
|
%% When payload length is over 63 bits, the most significant bit MUST be 0.
|
2013-02-09 16:27:24 -06:00
|
|
|
websocket_data(State, Req, HandlerState, << _:8, 1:1, 127:7, 1:1, _:7, _/binary >>) ->
|
2012-04-01 18:58:28 -07:00
|
|
|
websocket_close(State, Req, HandlerState, {error, badframe});
|
2013-01-12 22:31:23 +01:00
|
|
|
%% All frames sent from the client to the server are masked.
|
|
|
|
websocket_data(State, Req, HandlerState, << _:8, 0:1, _/bits >>) ->
|
2012-04-01 18:58:28 -07:00
|
|
|
websocket_close(State, Req, HandlerState, {error, badframe});
|
2013-01-12 22:31:23 +01:00
|
|
|
%% For the next two clauses, it can be one of the following:
|
|
|
|
%%
|
|
|
|
%% * The minimal number of bytes MUST be used to encode the length
|
|
|
|
%% * All control frames MUST have a payload length of 125 bytes or less
|
|
|
|
websocket_data(State, Req, HandlerState, << _:9, 126:7, _:48, _/bits >>) ->
|
2012-04-01 18:58:28 -07:00
|
|
|
websocket_close(State, Req, HandlerState, {error, badframe});
|
2013-01-12 22:31:23 +01:00
|
|
|
websocket_data(State, Req, HandlerState, << _:9, 127:7, _:96, _/bits >>) ->
|
|
|
|
websocket_close(State, Req, HandlerState, {error, badframe});
|
|
|
|
%% Need more data.
|
|
|
|
websocket_data(State, Req, HandlerState, Data) ->
|
|
|
|
handler_before_loop(State, Req, HandlerState, Data).
|
2012-04-01 18:58:28 -07:00
|
|
|
|
2013-01-12 22:31:23 +01:00
|
|
|
%% Initialize or update fragmentation state.
|
|
|
|
-spec websocket_data(#state{}, Req, any(),
|
2013-04-15 18:36:33 +02:00
|
|
|
opcode(), non_neg_integer(), mask_key(), binary(), rsv(), 0 | 1)
|
2013-01-03 22:47:51 +01:00
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
| {suspend, module(), atom(), [any()]}
|
|
|
|
when Req::cowboy_req:req().
|
2013-01-12 22:31:23 +01:00
|
|
|
%% The opcode is only included in the first frame fragment.
|
|
|
|
websocket_data(State=#state{frag_state=undefined}, Req, HandlerState,
|
2013-04-15 18:36:33 +02:00
|
|
|
Opcode, Len, MaskKey, Data, Rsv, 0) ->
|
2013-01-12 22:31:23 +01:00
|
|
|
websocket_payload(State#state{frag_state={nofin, Opcode, <<>>}},
|
2013-07-02 11:09:27 +02:00
|
|
|
Req, HandlerState, 0, Len, MaskKey, <<>>, 0, Data, Rsv);
|
2013-01-12 22:31:23 +01:00
|
|
|
%% Subsequent frame fragments.
|
|
|
|
websocket_data(State=#state{frag_state={nofin, _, _}}, Req, HandlerState,
|
2013-04-15 18:36:33 +02:00
|
|
|
0, Len, MaskKey, Data, Rsv, 0) ->
|
2013-01-12 22:31:23 +01:00
|
|
|
websocket_payload(State, Req, HandlerState,
|
2013-07-02 11:09:27 +02:00
|
|
|
0, Len, MaskKey, <<>>, 0, Data, Rsv);
|
2013-01-12 22:31:23 +01:00
|
|
|
%% Final frame fragment.
|
|
|
|
websocket_data(State=#state{frag_state={nofin, Opcode, SoFar}},
|
2013-04-15 18:36:33 +02:00
|
|
|
Req, HandlerState, 0, Len, MaskKey, Data, Rsv, 1) ->
|
2013-01-12 22:31:23 +01:00
|
|
|
websocket_payload(State#state{frag_state={fin, Opcode, SoFar}},
|
2013-07-02 11:09:27 +02:00
|
|
|
Req, HandlerState, 0, Len, MaskKey, <<>>, 0, Data, Rsv);
|
2013-01-12 22:31:23 +01:00
|
|
|
%% Unfragmented frame.
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_data(State, Req, HandlerState, Opcode, Len, MaskKey, Data, Rsv, 1) ->
|
2013-01-12 22:31:23 +01:00
|
|
|
websocket_payload(State, Req, HandlerState,
|
2013-07-02 11:09:27 +02:00
|
|
|
Opcode, Len, MaskKey, <<>>, 0, Data, Rsv).
|
2011-04-14 21:21:17 +02:00
|
|
|
|
2013-01-12 22:31:23 +01:00
|
|
|
-spec websocket_payload(#state{}, Req, any(),
|
2013-07-02 11:09:27 +02:00
|
|
|
opcode(), non_neg_integer(), mask_key(), binary(), non_neg_integer(),
|
|
|
|
binary(), rsv())
|
2013-01-03 22:47:51 +01:00
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
| {suspend, module(), atom(), [any()]}
|
|
|
|
when Req::cowboy_req:req().
|
2013-01-14 16:20:33 +01:00
|
|
|
%% Close control frames with a payload MUST contain a valid close code.
|
|
|
|
websocket_payload(State, Req, HandlerState,
|
2013-07-02 11:09:27 +02:00
|
|
|
Opcode=8, Len, MaskKey, <<>>, 0,
|
|
|
|
<< MaskedCode:2/binary, Rest/bits >>, Rsv) ->
|
2013-01-14 16:20:33 +01:00
|
|
|
Unmasked = << Code:16 >> = websocket_unmask(MaskedCode, MaskKey, <<>>),
|
|
|
|
if Code < 1000; Code =:= 1004; Code =:= 1005; Code =:= 1006;
|
|
|
|
(Code > 1011) and (Code < 3000); Code > 4999 ->
|
|
|
|
websocket_close(State, Req, HandlerState, {error, badframe});
|
|
|
|
true ->
|
|
|
|
websocket_payload(State, Req, HandlerState,
|
2013-07-02 11:09:27 +02:00
|
|
|
Opcode, Len - 2, MaskKey, Unmasked, byte_size(MaskedCode),
|
|
|
|
Rest, Rsv)
|
2013-01-14 16:20:33 +01:00
|
|
|
end;
|
|
|
|
%% Text frames and close control frames MUST have a payload that is valid UTF-8.
|
2013-01-13 00:10:32 +01:00
|
|
|
websocket_payload(State=#state{utf8_state=Incomplete},
|
2013-07-02 11:09:27 +02:00
|
|
|
Req, HandlerState, Opcode, Len, MaskKey, Unmasked, UnmaskedLen,
|
|
|
|
Data, Rsv)
|
2013-01-14 16:20:33 +01:00
|
|
|
when (byte_size(Data) < Len) andalso ((Opcode =:= 1) orelse
|
|
|
|
((Opcode =:= 8) andalso (Unmasked =/= <<>>))) ->
|
2013-01-13 00:10:32 +01:00
|
|
|
Unmasked2 = websocket_unmask(Data,
|
2013-07-02 11:09:27 +02:00
|
|
|
rotate_mask_key(MaskKey, UnmaskedLen), <<>>),
|
2013-04-15 18:36:33 +02:00
|
|
|
{Unmasked3, State2} = websocket_inflate_frame(Unmasked2, Rsv, false, State),
|
|
|
|
case is_utf8(<< Incomplete/binary, Unmasked3/binary >>) of
|
2013-01-13 00:10:32 +01:00
|
|
|
false ->
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_close(State2, Req, HandlerState, {error, badencoding});
|
2013-01-13 00:10:32 +01:00
|
|
|
Utf8State ->
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_payload_loop(State2#state{utf8_state=Utf8State},
|
2013-01-13 00:10:32 +01:00
|
|
|
Req, HandlerState, Opcode, Len - byte_size(Data), MaskKey,
|
2013-07-02 11:09:27 +02:00
|
|
|
<< Unmasked/binary, Unmasked3/binary >>,
|
|
|
|
UnmaskedLen + byte_size(Data), Rsv)
|
2013-01-13 00:10:32 +01:00
|
|
|
end;
|
|
|
|
websocket_payload(State=#state{utf8_state=Incomplete},
|
2013-07-02 11:09:27 +02:00
|
|
|
Req, HandlerState, Opcode, Len, MaskKey, Unmasked, UnmaskedLen,
|
|
|
|
Data, Rsv)
|
2013-01-14 16:20:33 +01:00
|
|
|
when Opcode =:= 1; (Opcode =:= 8) and (Unmasked =/= <<>>) ->
|
2013-01-13 00:10:32 +01:00
|
|
|
<< End:Len/binary, Rest/bits >> = Data,
|
|
|
|
Unmasked2 = websocket_unmask(End,
|
2013-07-02 11:09:27 +02:00
|
|
|
rotate_mask_key(MaskKey, UnmaskedLen), <<>>),
|
2013-04-15 18:36:33 +02:00
|
|
|
{Unmasked3, State2} = websocket_inflate_frame(Unmasked2, Rsv, true, State),
|
|
|
|
case is_utf8(<< Incomplete/binary, Unmasked3/binary >>) of
|
2013-01-13 00:10:32 +01:00
|
|
|
<<>> ->
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_dispatch(State2#state{utf8_state= <<>>},
|
2013-01-13 00:10:32 +01:00
|
|
|
Req, HandlerState, Rest, Opcode,
|
2013-04-15 18:36:33 +02:00
|
|
|
<< Unmasked/binary, Unmasked3/binary >>);
|
2013-01-13 00:10:32 +01:00
|
|
|
_ ->
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_close(State2, Req, HandlerState, {error, badencoding})
|
2013-01-13 00:10:32 +01:00
|
|
|
end;
|
|
|
|
%% Fragmented text frames may cut payload in the middle of UTF-8 codepoints.
|
|
|
|
websocket_payload(State=#state{frag_state={_, 1, _}, utf8_state=Incomplete},
|
2013-07-02 11:09:27 +02:00
|
|
|
Req, HandlerState, Opcode=0, Len, MaskKey, Unmasked, UnmaskedLen,
|
|
|
|
Data, Rsv)
|
2013-01-13 00:10:32 +01:00
|
|
|
when byte_size(Data) < Len ->
|
|
|
|
Unmasked2 = websocket_unmask(Data,
|
2013-07-02 11:09:27 +02:00
|
|
|
rotate_mask_key(MaskKey, UnmaskedLen), <<>>),
|
2013-04-15 18:36:33 +02:00
|
|
|
{Unmasked3, State2} = websocket_inflate_frame(Unmasked2, Rsv, false, State),
|
|
|
|
case is_utf8(<< Incomplete/binary, Unmasked3/binary >>) of
|
2013-01-13 00:10:32 +01:00
|
|
|
false ->
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_close(State2, Req, HandlerState, {error, badencoding});
|
2013-01-13 00:10:32 +01:00
|
|
|
Utf8State ->
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_payload_loop(State2#state{utf8_state=Utf8State},
|
2013-01-13 00:10:32 +01:00
|
|
|
Req, HandlerState, Opcode, Len - byte_size(Data), MaskKey,
|
2013-07-02 11:09:27 +02:00
|
|
|
<< Unmasked/binary, Unmasked3/binary >>,
|
|
|
|
UnmaskedLen + byte_size(Data), Rsv)
|
2013-01-13 00:10:32 +01:00
|
|
|
end;
|
|
|
|
websocket_payload(State=#state{frag_state={Fin, 1, _}, utf8_state=Incomplete},
|
2013-07-02 11:09:27 +02:00
|
|
|
Req, HandlerState, Opcode=0, Len, MaskKey, Unmasked, UnmaskedLen,
|
|
|
|
Data, Rsv) ->
|
2013-01-13 00:10:32 +01:00
|
|
|
<< End:Len/binary, Rest/bits >> = Data,
|
|
|
|
Unmasked2 = websocket_unmask(End,
|
2013-07-02 11:09:27 +02:00
|
|
|
rotate_mask_key(MaskKey, UnmaskedLen), <<>>),
|
2013-07-02 12:36:26 +02:00
|
|
|
{Unmasked3, State2} = websocket_inflate_frame(Unmasked2, Rsv, Fin =:= fin, State),
|
2013-04-15 18:36:33 +02:00
|
|
|
case is_utf8(<< Incomplete/binary, Unmasked3/binary >>) of
|
2013-01-13 00:10:32 +01:00
|
|
|
<<>> ->
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_dispatch(State2#state{utf8_state= <<>>},
|
2013-01-13 00:10:32 +01:00
|
|
|
Req, HandlerState, Rest, Opcode,
|
2013-04-15 18:36:33 +02:00
|
|
|
<< Unmasked/binary, Unmasked3/binary >>);
|
2013-01-13 00:10:32 +01:00
|
|
|
Utf8State when is_binary(Utf8State), Fin =:= nofin ->
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_dispatch(State2#state{utf8_state=Utf8State},
|
2013-01-13 00:10:32 +01:00
|
|
|
Req, HandlerState, Rest, Opcode,
|
2013-04-15 18:36:33 +02:00
|
|
|
<< Unmasked/binary, Unmasked3/binary >>);
|
2013-01-13 00:10:32 +01:00
|
|
|
_ ->
|
2013-01-14 16:20:33 +01:00
|
|
|
websocket_close(State, Req, HandlerState, {error, badencoding})
|
2013-01-13 00:10:32 +01:00
|
|
|
end;
|
|
|
|
%% Other frames have a binary payload.
|
2013-01-12 22:31:23 +01:00
|
|
|
websocket_payload(State, Req, HandlerState,
|
2013-07-02 11:09:27 +02:00
|
|
|
Opcode, Len, MaskKey, Unmasked, UnmaskedLen, Data, Rsv)
|
2013-01-12 22:31:23 +01:00
|
|
|
when byte_size(Data) < Len ->
|
|
|
|
Unmasked2 = websocket_unmask(Data,
|
2013-07-02 11:09:27 +02:00
|
|
|
rotate_mask_key(MaskKey, UnmaskedLen), <<>>),
|
2013-04-15 18:36:33 +02:00
|
|
|
{Unmasked3, State2} = websocket_inflate_frame(Unmasked2, Rsv, false, State),
|
|
|
|
websocket_payload_loop(State2, Req, HandlerState,
|
2013-07-02 11:09:27 +02:00
|
|
|
Opcode, Len - byte_size(Data), MaskKey,
|
|
|
|
<< Unmasked/binary, Unmasked3/binary >>, UnmaskedLen + byte_size(Data),
|
|
|
|
Rsv);
|
2013-01-12 22:31:23 +01:00
|
|
|
websocket_payload(State, Req, HandlerState,
|
2013-07-02 11:09:27 +02:00
|
|
|
Opcode, Len, MaskKey, Unmasked, UnmaskedLen, Data, Rsv) ->
|
2013-01-12 22:31:23 +01:00
|
|
|
<< End:Len/binary, Rest/bits >> = Data,
|
|
|
|
Unmasked2 = websocket_unmask(End,
|
2013-07-02 11:09:27 +02:00
|
|
|
rotate_mask_key(MaskKey, UnmaskedLen), <<>>),
|
2013-04-15 18:36:33 +02:00
|
|
|
{Unmasked3, State2} = websocket_inflate_frame(Unmasked2, Rsv, true, State),
|
2013-07-02 11:09:27 +02:00
|
|
|
websocket_dispatch(State2, Req, HandlerState, Rest, Opcode,
|
|
|
|
<< Unmasked/binary, Unmasked3/binary >>).
|
2013-04-15 18:36:33 +02:00
|
|
|
|
|
|
|
-spec websocket_inflate_frame(binary(), rsv(), boolean(), #state{}) ->
|
|
|
|
{binary(), #state{}}.
|
|
|
|
websocket_inflate_frame(Data, << Rsv1:1, _:2 >>, _,
|
|
|
|
#state{deflate_frame = DeflateFrame} = State)
|
|
|
|
when DeflateFrame =:= false orelse Rsv1 =:= 0 ->
|
|
|
|
{Data, State};
|
2013-07-02 10:58:12 +02:00
|
|
|
websocket_inflate_frame(Data, << 1:1, _:2 >>, false, State) ->
|
|
|
|
Result = zlib:inflate(State#state.inflate_state, Data),
|
|
|
|
{iolist_to_binary(Result), State};
|
|
|
|
websocket_inflate_frame(Data, << 1:1, _:2 >>, true, State) ->
|
|
|
|
Result = zlib:inflate(State#state.inflate_state,
|
|
|
|
<< Data/binary, 0:8, 0:8, 255:8, 255:8 >>),
|
|
|
|
{iolist_to_binary(Result), State}.
|
2011-08-23 16:24:02 +02:00
|
|
|
|
2013-01-12 22:31:23 +01:00
|
|
|
-spec websocket_unmask(B, mask_key(), B) -> B when B::binary().
|
|
|
|
websocket_unmask(<<>>, _, Unmasked) ->
|
|
|
|
Unmasked;
|
|
|
|
websocket_unmask(<< O:32, Rest/bits >>, MaskKey, Acc) ->
|
2011-08-23 16:24:02 +02:00
|
|
|
T = O bxor MaskKey,
|
2013-01-12 22:31:23 +01:00
|
|
|
websocket_unmask(Rest, MaskKey, << Acc/binary, T:32 >>);
|
|
|
|
websocket_unmask(<< O:24 >>, MaskKey, Acc) ->
|
2011-08-23 16:24:02 +02:00
|
|
|
<< MaskKey2:24, _:8 >> = << MaskKey:32 >>,
|
|
|
|
T = O bxor MaskKey2,
|
2013-01-12 22:31:23 +01:00
|
|
|
<< Acc/binary, T:24 >>;
|
|
|
|
websocket_unmask(<< O:16 >>, MaskKey, Acc) ->
|
2011-08-23 16:24:02 +02:00
|
|
|
<< MaskKey2:16, _:16 >> = << MaskKey:32 >>,
|
|
|
|
T = O bxor MaskKey2,
|
2013-01-12 22:31:23 +01:00
|
|
|
<< Acc/binary, T:16 >>;
|
|
|
|
websocket_unmask(<< O:8 >>, MaskKey, Acc) ->
|
2011-08-23 16:24:02 +02:00
|
|
|
<< MaskKey2:8, _:24 >> = << MaskKey:32 >>,
|
|
|
|
T = O bxor MaskKey2,
|
2013-01-12 22:31:23 +01:00
|
|
|
<< Acc/binary, T:8 >>.
|
|
|
|
|
|
|
|
%% Because we unmask on the fly we need to continue from the right mask byte.
|
|
|
|
-spec rotate_mask_key(mask_key(), non_neg_integer()) -> mask_key().
|
|
|
|
rotate_mask_key(MaskKey, UnmaskedLen) ->
|
|
|
|
Left = UnmaskedLen rem 4,
|
|
|
|
Right = 4 - Left,
|
|
|
|
(MaskKey bsl (Left * 8)) + (MaskKey bsr (Right * 8)).
|
|
|
|
|
2013-01-13 00:10:32 +01:00
|
|
|
%% Returns <<>> if the argument is valid UTF-8, false if not,
|
|
|
|
%% or the incomplete part of the argument if we need more data.
|
|
|
|
-spec is_utf8(binary()) -> false | binary().
|
|
|
|
is_utf8(Valid = <<>>) ->
|
|
|
|
Valid;
|
|
|
|
is_utf8(<< _/utf8, Rest/binary >>) ->
|
|
|
|
is_utf8(Rest);
|
|
|
|
%% 2 bytes. Codepages C0 and C1 are invalid; fail early.
|
|
|
|
is_utf8(<< 2#1100000:7, _/bits >>) ->
|
|
|
|
false;
|
|
|
|
is_utf8(Incomplete = << 2#110:3, _:5 >>) ->
|
|
|
|
Incomplete;
|
|
|
|
%% 3 bytes.
|
|
|
|
is_utf8(Incomplete = << 2#1110:4, _:4 >>) ->
|
|
|
|
Incomplete;
|
|
|
|
is_utf8(Incomplete = << 2#1110:4, _:4, 2#10:2, _:6 >>) ->
|
|
|
|
Incomplete;
|
|
|
|
%% 4 bytes. Codepage F4 may have invalid values greater than 0x10FFFF.
|
|
|
|
is_utf8(<< 2#11110100:8, 2#10:2, High:6, _/bits >>) when High >= 2#10000 ->
|
|
|
|
false;
|
|
|
|
is_utf8(Incomplete = << 2#11110:5, _:3 >>) ->
|
|
|
|
Incomplete;
|
|
|
|
is_utf8(Incomplete = << 2#11110:5, _:3, 2#10:2, _:6 >>) ->
|
|
|
|
Incomplete;
|
|
|
|
is_utf8(Incomplete = << 2#11110:5, _:3, 2#10:2, _:6, 2#10:2, _:6 >>) ->
|
|
|
|
Incomplete;
|
|
|
|
%% Invalid.
|
|
|
|
is_utf8(_) ->
|
|
|
|
false.
|
|
|
|
|
2013-01-12 22:31:23 +01:00
|
|
|
-spec websocket_payload_loop(#state{}, Req, any(),
|
2013-07-02 11:09:27 +02:00
|
|
|
opcode(), non_neg_integer(), mask_key(), binary(),
|
|
|
|
non_neg_integer(), rsv())
|
2013-01-12 22:31:23 +01:00
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
| {suspend, module(), atom(), [any()]}
|
|
|
|
when Req::cowboy_req:req().
|
|
|
|
websocket_payload_loop(State=#state{socket=Socket, transport=Transport,
|
|
|
|
messages={OK, Closed, Error}, timeout_ref=TRef},
|
2013-07-02 11:09:27 +02:00
|
|
|
Req, HandlerState, Opcode, Len, MaskKey, Unmasked, UnmaskedLen, Rsv) ->
|
2013-01-12 22:31:23 +01:00
|
|
|
Transport:setopts(Socket, [{active, once}]),
|
|
|
|
receive
|
|
|
|
{OK, Socket, Data} ->
|
|
|
|
State2 = handler_loop_timeout(State),
|
|
|
|
websocket_payload(State2, Req, HandlerState,
|
2013-07-02 11:09:27 +02:00
|
|
|
Opcode, Len, MaskKey, Unmasked, UnmaskedLen, Data, Rsv);
|
2013-01-12 22:31:23 +01:00
|
|
|
{Closed, Socket} ->
|
|
|
|
handler_terminate(State, Req, HandlerState, {error, closed});
|
|
|
|
{Error, Socket, Reason} ->
|
|
|
|
handler_terminate(State, Req, HandlerState, {error, Reason});
|
|
|
|
{timeout, TRef, ?MODULE} ->
|
2014-09-30 20:12:13 +03:00
|
|
|
websocket_close(State, Req, HandlerState, timeout);
|
2013-01-12 22:31:23 +01:00
|
|
|
{timeout, OlderTRef, ?MODULE} when is_reference(OlderTRef) ->
|
|
|
|
websocket_payload_loop(State, Req, HandlerState,
|
2013-07-02 11:09:27 +02:00
|
|
|
Opcode, Len, MaskKey, Unmasked, UnmaskedLen, Rsv);
|
2013-01-12 22:31:23 +01:00
|
|
|
Message ->
|
|
|
|
handler_call(State, Req, HandlerState,
|
|
|
|
<<>>, websocket_info, Message,
|
|
|
|
fun (State2, Req2, HandlerState2, _) ->
|
|
|
|
websocket_payload_loop(State2, Req2, HandlerState2,
|
2013-07-02 11:09:27 +02:00
|
|
|
Opcode, Len, MaskKey, Unmasked, UnmaskedLen, Rsv)
|
2013-01-12 22:31:23 +01:00
|
|
|
end)
|
|
|
|
end.
|
2011-08-23 16:24:02 +02:00
|
|
|
|
2013-01-03 22:47:51 +01:00
|
|
|
-spec websocket_dispatch(#state{}, Req, any(), binary(), opcode(), binary())
|
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
| {suspend, module(), atom(), [any()]}
|
|
|
|
when Req::cowboy_req:req().
|
2013-01-12 22:31:23 +01:00
|
|
|
%% Continuation frame.
|
|
|
|
websocket_dispatch(State=#state{frag_state={nofin, Opcode, SoFar}},
|
|
|
|
Req, HandlerState, RemainingData, 0, Payload) ->
|
2012-04-01 18:58:28 -07:00
|
|
|
websocket_data(State#state{frag_state={nofin, Opcode,
|
2013-01-12 22:31:23 +01:00
|
|
|
<< SoFar/binary, Payload/binary >>}}, Req, HandlerState, RemainingData);
|
|
|
|
%% Last continuation frame.
|
|
|
|
websocket_dispatch(State=#state{frag_state={fin, Opcode, SoFar}},
|
|
|
|
Req, HandlerState, RemainingData, 0, Payload) ->
|
2012-04-01 18:58:28 -07:00
|
|
|
websocket_dispatch(State#state{frag_state=undefined}, Req, HandlerState,
|
2013-01-12 22:31:23 +01:00
|
|
|
RemainingData, Opcode, << SoFar/binary, Payload/binary >>);
|
2011-08-23 16:24:02 +02:00
|
|
|
%% Text frame.
|
|
|
|
websocket_dispatch(State, Req, HandlerState, RemainingData, 1, Payload) ->
|
|
|
|
handler_call(State, Req, HandlerState, RemainingData,
|
|
|
|
websocket_handle, {text, Payload}, fun websocket_data/4);
|
|
|
|
%% Binary frame.
|
|
|
|
websocket_dispatch(State, Req, HandlerState, RemainingData, 2, Payload) ->
|
|
|
|
handler_call(State, Req, HandlerState, RemainingData,
|
|
|
|
websocket_handle, {binary, Payload}, fun websocket_data/4);
|
|
|
|
%% Close control frame.
|
2013-01-14 16:20:33 +01:00
|
|
|
websocket_dispatch(State, Req, HandlerState, _RemainingData, 8, <<>>) ->
|
2014-09-30 20:12:13 +03:00
|
|
|
websocket_close(State, Req, HandlerState, remote);
|
2013-01-14 16:20:33 +01:00
|
|
|
websocket_dispatch(State, Req, HandlerState, _RemainingData, 8,
|
|
|
|
<< Code:16, Payload/bits >>) ->
|
|
|
|
websocket_close(State, Req, HandlerState, {remote, Code, Payload});
|
2011-08-23 16:24:02 +02:00
|
|
|
%% Ping control frame. Send a pong back and forward the ping to the handler.
|
2012-09-15 21:09:12 +02:00
|
|
|
websocket_dispatch(State=#state{socket=Socket, transport=Transport},
|
|
|
|
Req, HandlerState, RemainingData, 9, Payload) ->
|
2013-01-10 21:58:38 +01:00
|
|
|
Len = payload_length_to_binary(byte_size(Payload)),
|
2011-08-23 16:24:02 +02:00
|
|
|
Transport:send(Socket, << 1:1, 0:3, 10:4, 0:1, Len/bits, Payload/binary >>),
|
|
|
|
handler_call(State, Req, HandlerState, RemainingData,
|
|
|
|
websocket_handle, {ping, Payload}, fun websocket_data/4);
|
|
|
|
%% Pong control frame.
|
|
|
|
websocket_dispatch(State, Req, HandlerState, RemainingData, 10, Payload) ->
|
|
|
|
handler_call(State, Req, HandlerState, RemainingData,
|
|
|
|
websocket_handle, {pong, Payload}, fun websocket_data/4).
|
|
|
|
|
2013-01-03 22:47:51 +01:00
|
|
|
-spec handler_call(#state{}, Req, any(), binary(), atom(), any(), fun())
|
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
| {suspend, module(), atom(), [any()]}
|
|
|
|
when Req::cowboy_req:req().
|
2013-08-24 20:36:23 +02:00
|
|
|
handler_call(State=#state{handler=Handler}, Req, HandlerState,
|
|
|
|
RemainingData, Callback, Message, NextState) ->
|
2011-07-19 12:12:25 +02:00
|
|
|
try Handler:Callback(Message, Req, HandlerState) of
|
2011-04-14 21:21:17 +02:00
|
|
|
{ok, Req2, HandlerState2} ->
|
|
|
|
NextState(State, Req2, HandlerState2, RemainingData);
|
2011-06-06 18:15:36 +02:00
|
|
|
{ok, Req2, HandlerState2, hibernate} ->
|
|
|
|
NextState(State#state{hibernate=true},
|
|
|
|
Req2, HandlerState2, RemainingData);
|
2012-10-11 21:46:43 +02:00
|
|
|
{reply, Payload, Req2, HandlerState2}
|
2013-08-13 23:29:16 +04:00
|
|
|
when is_list(Payload) ->
|
|
|
|
case websocket_send_many(Payload, State) of
|
2013-04-15 18:36:33 +02:00
|
|
|
{ok, State2} ->
|
|
|
|
NextState(State2, Req2, HandlerState2, RemainingData);
|
|
|
|
{shutdown, State2} ->
|
2014-09-30 20:12:13 +03:00
|
|
|
handler_terminate(State2, Req2, HandlerState2, shutdown);
|
2013-04-15 18:36:33 +02:00
|
|
|
{{error, _} = Error, State2} ->
|
|
|
|
handler_terminate(State2, Req2, HandlerState2, Error)
|
2012-04-08 11:57:30 +02:00
|
|
|
end;
|
2012-10-11 21:46:43 +02:00
|
|
|
{reply, Payload, Req2, HandlerState2, hibernate}
|
2013-08-13 23:29:16 +04:00
|
|
|
when is_list(Payload) ->
|
|
|
|
case websocket_send_many(Payload, State) of
|
2013-04-15 18:36:33 +02:00
|
|
|
{ok, State2} ->
|
|
|
|
NextState(State2#state{hibernate=true},
|
2012-04-08 11:57:30 +02:00
|
|
|
Req2, HandlerState2, RemainingData);
|
2013-04-15 18:36:33 +02:00
|
|
|
{shutdown, State2} ->
|
2014-09-30 20:12:13 +03:00
|
|
|
handler_terminate(State2, Req2, HandlerState2, shutdown);
|
2013-04-15 18:36:33 +02:00
|
|
|
{{error, _} = Error, State2} ->
|
|
|
|
handler_terminate(State2, Req2, HandlerState2, Error)
|
2012-04-08 11:57:30 +02:00
|
|
|
end;
|
2013-08-13 23:29:16 +04:00
|
|
|
{reply, Payload, Req2, HandlerState2} ->
|
|
|
|
case websocket_send(Payload, State) of
|
2013-04-15 18:36:33 +02:00
|
|
|
{ok, State2} ->
|
|
|
|
NextState(State2, Req2, HandlerState2, RemainingData);
|
|
|
|
{shutdown, State2} ->
|
2014-09-30 20:12:13 +03:00
|
|
|
handler_terminate(State2, Req2, HandlerState2, shutdown);
|
2013-04-15 18:36:33 +02:00
|
|
|
{{error, _} = Error, State2} ->
|
|
|
|
handler_terminate(State2, Req2, HandlerState2, Error)
|
2012-04-08 11:57:30 +02:00
|
|
|
end;
|
2013-08-13 23:29:16 +04:00
|
|
|
{reply, Payload, Req2, HandlerState2, hibernate} ->
|
|
|
|
case websocket_send(Payload, State) of
|
2013-04-15 18:36:33 +02:00
|
|
|
{ok, State2} ->
|
|
|
|
NextState(State2#state{hibernate=true},
|
2012-04-08 11:57:30 +02:00
|
|
|
Req2, HandlerState2, RemainingData);
|
2013-04-15 18:36:33 +02:00
|
|
|
{shutdown, State2} ->
|
2014-09-30 20:12:13 +03:00
|
|
|
handler_terminate(State2, Req2, HandlerState2, shutdown);
|
2013-04-15 18:36:33 +02:00
|
|
|
{{error, _} = Error, State2} ->
|
|
|
|
handler_terminate(State2, Req2, HandlerState2, Error)
|
2012-04-08 11:57:30 +02:00
|
|
|
end;
|
2011-04-14 21:21:17 +02:00
|
|
|
{shutdown, Req2, HandlerState2} ->
|
2014-09-30 20:12:13 +03:00
|
|
|
websocket_close(State, Req2, HandlerState2, shutdown)
|
2011-05-27 12:32:02 +02:00
|
|
|
catch Class:Reason ->
|
2014-09-30 20:12:13 +03:00
|
|
|
_ = websocket_close(State, Req, HandlerState, {crash, Class, Reason}),
|
2013-08-24 20:36:23 +02:00
|
|
|
erlang:Class([
|
|
|
|
{reason, Reason},
|
|
|
|
{mfa, {Handler, Callback, 3}},
|
|
|
|
{stacktrace, erlang:get_stacktrace()},
|
|
|
|
{msg, Message},
|
|
|
|
{req, cowboy_req:to_list(Req)},
|
|
|
|
{state, HandlerState}
|
|
|
|
])
|
2011-04-14 21:21:17 +02:00
|
|
|
end.
|
|
|
|
|
2012-12-02 21:37:24 +01:00
|
|
|
websocket_opcode(text) -> 1;
|
|
|
|
websocket_opcode(binary) -> 2;
|
|
|
|
websocket_opcode(close) -> 8;
|
|
|
|
websocket_opcode(ping) -> 9;
|
|
|
|
websocket_opcode(pong) -> 10.
|
|
|
|
|
2013-07-02 11:02:32 +02:00
|
|
|
-spec websocket_deflate_frame(opcode(), binary(), #state{}) ->
|
2013-07-08 09:49:35 +02:00
|
|
|
{binary(), rsv(), #state{}}.
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_deflate_frame(Opcode, Payload,
|
|
|
|
State=#state{deflate_frame = DeflateFrame})
|
|
|
|
when DeflateFrame =:= false orelse Opcode >= 8 ->
|
2013-07-02 11:02:32 +02:00
|
|
|
{Payload, << 0:3 >>, State};
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_deflate_frame(_, Payload, State=#state{deflate_state = Deflate}) ->
|
|
|
|
Deflated = iolist_to_binary(zlib:deflate(Deflate, Payload, sync)),
|
|
|
|
DeflatedBodyLength = erlang:size(Deflated) - 4,
|
|
|
|
Deflated1 = case Deflated of
|
2013-07-02 11:02:32 +02:00
|
|
|
<< Body:DeflatedBodyLength/binary, 0:8, 0:8, 255:8, 255:8 >> -> Body;
|
2013-04-15 18:36:33 +02:00
|
|
|
_ -> Deflated
|
|
|
|
end,
|
2013-07-02 11:02:32 +02:00
|
|
|
{Deflated1, << 1:1, 0:2 >>, State}.
|
2013-04-15 18:36:33 +02:00
|
|
|
|
2012-12-02 21:37:24 +01:00
|
|
|
-spec websocket_send(frame(), #state{})
|
2013-04-15 18:36:33 +02:00
|
|
|
-> {ok, #state{}} | {shutdown, #state{}} | {{error, atom()}, #state{}}.
|
|
|
|
websocket_send(Type, State=#state{socket=Socket, transport=Transport})
|
2012-12-02 21:37:24 +01:00
|
|
|
when Type =:= close ->
|
|
|
|
Opcode = websocket_opcode(Type),
|
|
|
|
case Transport:send(Socket, << 1:1, 0:3, Opcode:4, 0:8 >>) of
|
2013-04-15 18:36:33 +02:00
|
|
|
ok -> {shutdown, State};
|
|
|
|
Error -> {Error, State}
|
2012-12-02 21:37:24 +01:00
|
|
|
end;
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_send(Type, State=#state{socket=Socket, transport=Transport})
|
2012-12-02 21:37:24 +01:00
|
|
|
when Type =:= ping; Type =:= pong ->
|
|
|
|
Opcode = websocket_opcode(Type),
|
2013-04-15 18:36:33 +02:00
|
|
|
{Transport:send(Socket, << 1:1, 0:3, Opcode:4, 0:8 >>), State};
|
2012-12-08 19:11:56 +01:00
|
|
|
websocket_send({close, Payload}, State) ->
|
|
|
|
websocket_send({close, 1000, Payload}, State);
|
2013-04-15 18:36:33 +02:00
|
|
|
websocket_send({Type = close, StatusCode, Payload}, State=#state{
|
2012-12-08 19:11:56 +01:00
|
|
|
socket=Socket, transport=Transport}) ->
|
|
|
|
Opcode = websocket_opcode(Type),
|
|
|
|
Len = 2 + iolist_size(Payload),
|
|
|
|
%% Control packets must not be > 125 in length.
|
|
|
|
true = Len =< 125,
|
2013-01-10 21:58:38 +01:00
|
|
|
BinLen = payload_length_to_binary(Len),
|
2012-12-08 19:11:56 +01:00
|
|
|
Transport:send(Socket,
|
|
|
|
[<< 1:1, 0:3, Opcode:4, 0:1, BinLen/bits, StatusCode:16 >>, Payload]),
|
2013-04-15 18:36:33 +02:00
|
|
|
{shutdown, State};
|
|
|
|
websocket_send({Type, Payload0}, State=#state{socket=Socket, transport=Transport}) ->
|
2012-12-02 21:37:24 +01:00
|
|
|
Opcode = websocket_opcode(Type),
|
2013-04-15 18:36:33 +02:00
|
|
|
{Payload, Rsv, State2} = websocket_deflate_frame(Opcode, iolist_to_binary(Payload0), State),
|
2012-12-02 21:37:24 +01:00
|
|
|
Len = iolist_size(Payload),
|
|
|
|
%% Control packets must not be > 125 in length.
|
2012-12-08 19:11:56 +01:00
|
|
|
true = if Type =:= ping; Type =:= pong ->
|
2012-12-02 21:37:24 +01:00
|
|
|
Len =< 125;
|
|
|
|
true ->
|
|
|
|
true
|
2011-08-23 16:24:02 +02:00
|
|
|
end,
|
2013-01-10 21:58:38 +01:00
|
|
|
BinLen = payload_length_to_binary(Len),
|
2013-04-15 18:36:33 +02:00
|
|
|
{Transport:send(Socket,
|
|
|
|
[<< 1:1, Rsv/bits, Opcode:4, 0:1, BinLen/bits >>, Payload]), State2}.
|
2011-04-14 21:21:17 +02:00
|
|
|
|
2014-09-26 15:58:44 +03:00
|
|
|
-spec payload_length_to_binary(0..16#7fffffffffffffff)
|
|
|
|
-> << _:7 >> | << _:23 >> | << _:71 >>.
|
|
|
|
payload_length_to_binary(N) ->
|
|
|
|
case N of
|
|
|
|
N when N =< 125 -> << N:7 >>;
|
|
|
|
N when N =< 16#ffff -> << 126:7, N:16 >>;
|
|
|
|
N when N =< 16#7fffffffffffffff -> << 127:7, N:64 >>
|
|
|
|
end.
|
|
|
|
|
2012-12-02 21:37:24 +01:00
|
|
|
-spec websocket_send_many([frame()], #state{})
|
2013-04-15 18:36:33 +02:00
|
|
|
-> {ok, #state{}} | {shutdown, #state{}} | {{error, atom()}, #state{}}.
|
|
|
|
websocket_send_many([], State) ->
|
|
|
|
{ok, State};
|
2012-10-11 21:46:43 +02:00
|
|
|
websocket_send_many([Frame|Tail], State) ->
|
2012-04-08 11:57:30 +02:00
|
|
|
case websocket_send(Frame, State) of
|
2013-04-15 18:36:33 +02:00
|
|
|
{ok, State2} -> websocket_send_many(Tail, State2);
|
|
|
|
{shutdown, State2} -> {shutdown, State2};
|
|
|
|
{Error, State2} -> {Error, State2}
|
2012-04-08 11:57:30 +02:00
|
|
|
end.
|
2012-10-11 21:46:43 +02:00
|
|
|
|
2014-03-08 19:51:39 +01:00
|
|
|
-spec websocket_close(#state{}, Req, any(), terminate_reason())
|
2013-01-03 22:47:51 +01:00
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
when Req::cowboy_req:req().
|
2012-09-15 21:09:12 +02:00
|
|
|
websocket_close(State=#state{socket=Socket, transport=Transport},
|
|
|
|
Req, HandlerState, Reason) ->
|
2013-01-14 16:20:33 +01:00
|
|
|
case Reason of
|
2014-09-30 20:12:13 +03:00
|
|
|
Normal when Normal =:= shutdown; Normal =:= timeout ->
|
2013-01-14 16:20:33 +01:00
|
|
|
Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>);
|
|
|
|
{error, badframe} ->
|
|
|
|
Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1002:16 >>);
|
|
|
|
{error, badencoding} ->
|
|
|
|
Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1007:16 >>);
|
2014-09-30 20:12:13 +03:00
|
|
|
{crash, _, _} ->
|
2013-01-14 16:20:33 +01:00
|
|
|
Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, 1011:16 >>);
|
2014-09-30 20:12:13 +03:00
|
|
|
remote ->
|
2013-01-14 16:20:33 +01:00
|
|
|
Transport:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>);
|
|
|
|
{remote, Code, _} ->
|
|
|
|
Transport:send(Socket, << 1:1, 0:3, 8:4, 0:1, 2:7, Code:16 >>)
|
|
|
|
end,
|
2011-04-14 21:21:17 +02:00
|
|
|
handler_terminate(State, Req, HandlerState, Reason).
|
|
|
|
|
2014-03-08 19:51:39 +01:00
|
|
|
-spec handler_terminate(#state{}, Req, any(), terminate_reason())
|
2013-01-03 22:47:51 +01:00
|
|
|
-> {ok, Req, cowboy_middleware:env()}
|
|
|
|
when Req::cowboy_req:req().
|
2013-08-24 20:36:23 +02:00
|
|
|
handler_terminate(#state{env=Env, handler=Handler},
|
2014-09-26 15:58:44 +03:00
|
|
|
Req, HandlerState, Reason) ->
|
2014-09-30 20:12:13 +03:00
|
|
|
cowboy_handler:terminate(Reason, Req, HandlerState, Handler),
|
2013-01-03 22:47:51 +01:00
|
|
|
{ok, Req, [{result, closed}|Env]}.
|