0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-16 05:00:24 +00:00

Fix byte-by-byte Websocket handling

If the websocket frame handling code in cowboy_http_websocket receives
only 1 byte at a time, it fails with a badmatch in
cowboy_http_websocket:websocket_data/4. This commit fixes the problem
and introduces a test of the correct behaviour.
This commit is contained in:
Hunter Morris 2011-09-30 18:05:02 +01:00
parent eff9477201
commit 6250b22384
2 changed files with 75 additions and 7 deletions

View file

@ -232,20 +232,26 @@ websocket_data(State=#state{version=0, eop=EOP}, Req, HandlerState,
%% @todo We probably should allow limiting frame length.
handler_before_loop(State, Req, HandlerState, Data)
end;
%% incomplete hybi data frame.
websocket_data(State=#state{version=Version}, Req, HandlerState, Data)
when Version =/= 0, byte_size(Data) =:= 1 ->
handler_before_loop(State, Req, HandlerState, Data);
%% hybi data frame.
%% @todo Handle Fin.
websocket_data(State=#state{version=Version}, Req, HandlerState, Data)
when Version =/= 0 ->
<< 1:1, 0:3, Opcode:4, Mask:1, PayloadLen:7, Rest/bits >> = Data,
{PayloadLen2, Rest2} = case PayloadLen of
126 -> << L:16, R/bits >> = Rest, {L, R};
127 -> << 0:1, L:63, R/bits >> = Rest, {L, R};
PayloadLen -> {PayloadLen, Rest}
{PayloadLen2, Rest2} = case {PayloadLen, Rest} of
{126, << L:16, R/bits >>} -> {L, R};
{126, Rest} -> {undefined, Rest};
{127, << 0:1, L:63, R/bits >>} -> {L, R};
{127, Rest} -> {undefined, Rest};
{PayloadLen, Rest} -> {PayloadLen, Rest}
end,
case {Mask, PayloadLen2} of
{0, 0} ->
websocket_dispatch(State, Req, HandlerState, Rest2, Opcode, <<>>);
{1, N} when N + 4 < byte_size(Rest2) ->
{1, N} when N + 4 > byte_size(Rest2); N =:= undefined ->
%% @todo We probably should allow limiting frame length.
handler_before_loop(State, Req, HandlerState, Data);
{1, _N} ->