Skip to content

Commit

Permalink
simplify sanity_check
Browse files Browse the repository at this point in the history
  • Loading branch information
RJ committed Nov 22, 2011
1 parent 0da38b4 commit 059a19e
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions src/misultin_websocket_draft-hybi-10_17.erl
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ i_handle_data(#state{buffer=ToParse} = State, {Socket, SocketMode, WsHandleLoopP
?LOG_DEBUG("parsed frame ~p, remaining buffer is: ~p", [Frame,Rest]),
%% sanity check, in case client is broken
case sanity_check(Frame) of
ok ->
true ->
?LOG_DEBUG("sanity checks successfully performed",[]),
case handle_frame(Frame,
State#state{buffer = Rest},
Expand All @@ -267,30 +267,22 @@ i_handle_data(#state{buffer=ToParse} = State, {Socket, SocketMode, WsHandleLoopP
Other ->
Other
end;
protocol_error ->
false -> % protocol error
?LOG_DEBUG("sanity checks errors encountered, closing websocket",[]),
{websocket_close, websocket_close_data()}
end
end.

% format sanity checks
-spec sanity_check(#frame{}) -> ok | protocol_error.
-spec sanity_check(#frame{}) -> true | false.
sanity_check(Frame) ->
Checks = [
{1, Frame#frame.maskbit},
{0, Frame#frame.rsv1},
{0, Frame#frame.rsv2},
{0, Frame#frame.rsv3}
],
ChecksVerified = lists:dropwhile(
fun ({A, A}) -> true;
({_A, _}) -> false
end, Checks
),
case ChecksVerified of
[] -> ok;
_ -> protocol_error
end.
lists:foldl(fun({A,B}, Acc) -> Acc andalso (A =:= B) end, true, Checks).

% ---------------------------- /\ frame parsing ------------------------------------------------------------

Expand Down

0 comments on commit 059a19e

Please sign in to comment.