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
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]), ?LOG_DEBUG("parsed frame ~p, remaining buffer is: ~p", [Frame,Rest]),
%% sanity check, in case client is broken %% sanity check, in case client is broken
case sanity_check(Frame) of case sanity_check(Frame) of
ok -> true ->
?LOG_DEBUG("sanity checks successfully performed",[]), ?LOG_DEBUG("sanity checks successfully performed",[]),
case handle_frame(Frame, case handle_frame(Frame,
State#state{buffer = Rest}, State#state{buffer = Rest},
Expand All @@ -267,30 +267,22 @@ i_handle_data(#state{buffer=ToParse} = State, {Socket, SocketMode, WsHandleLoopP
Other -> Other ->
Other Other
end; end;
protocol_error -> false -> % protocol error
?LOG_DEBUG("sanity checks errors encountered, closing websocket",[]), ?LOG_DEBUG("sanity checks errors encountered, closing websocket",[]),
{websocket_close, websocket_close_data()} {websocket_close, websocket_close_data()}
end end
end. end.


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


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


Expand Down

0 comments on commit 059a19e

Please sign in to comment.