Permalink
Browse files

Fix bug #128 and add a new test case for it

  • Loading branch information...
1 parent 954ccb1 commit 02817364cdb70099b9c7c68e3cc10f9fe71b66ca @leg0 leg0 committed with vinoski Oct 22, 2012
Showing with 18 additions and 5 deletions.
  1. +13 −5 src/yaws_websockets.erl
  2. +5 −0 test/t2/app_test.erl
View
@@ -466,7 +466,7 @@ ws_frame_info(#ws_state{sock=Socket},
case check_control_frame(Len1, Opcode, Fin) of
ok ->
{ws_frame_info_secondary, Length, MaskingKey, Payload, Excess}
- = ws_frame_info_secondary(Socket, Len1, Rest),
+ = ws_frame_info_secondary(Socket, Masked, Len1, Rest),
FrameInfo = #ws_frame_info{fin=Fin,
rsv=Rsv,
opcode=opcode_to_atom(Opcode),
@@ -482,16 +482,21 @@ ws_frame_info(#ws_state{sock=Socket},
ws_frame_info(State = #ws_state{sock=Socket}, FirstPacket) ->
ws_frame_info(State, buffer(Socket, 2,FirstPacket)).
-ws_frame_info_secondary(Socket, Len1, Rest) ->
+
+ws_frame_info_secondary(Socket, Masked, Len1, Rest) ->
+ MaskLength = case Masked of
+ 0 -> 0;
+ 1 -> 4
+ end,
case Len1 of
126 ->
- <<Len:16, MaskingKey:4/binary, Rest2/binary>> =
+ <<Len:16, MaskingKey:MaskLength/binary, Rest2/binary>> =
buffer(Socket, 6, Rest);
127 ->
- <<Len:64, MaskingKey:4/binary, Rest2/binary>> =
+ <<Len:64, MaskingKey:MaskLength/binary, Rest2/binary>> =
buffer(Socket, 12, Rest);
Len ->
- <<MaskingKey:4/binary, Rest2/binary>> = buffer(Socket, 4, Rest)
+ <<MaskingKey:MaskLength/binary, Rest2/binary>> = buffer(Socket, 4, Rest)
end,
if
Len > ?MAX_PAYLOAD ->
@@ -659,6 +664,9 @@ mask(MaskBin, Data) ->
rmask(_,<<>>) ->
[<<>>];
+rmask(<<>>, Data) ->
+ [Data];
+
rmask(MaskBin = <<Mask:4/integer-unit:8>>,
<<Data:4/integer-unit:8, Rest/binary>>) ->
Masked = Mask bxor Data,
View
@@ -903,6 +903,11 @@ test_websocket() ->
<<129, 133, "aaaa", 38722669838:5/integer-unit:8>>,
ok = gen_tcp:send(Sock, Frame),
ExpectFrame = <<129, 5, "hello">>,
+ {ok, ExpectFrame} = gen_tcp:recv(Sock, 0, 2000),
+
+ %% Text frame "hello" without masking
+ UnmaskedFrame = <<129, 5, "hello">>,
+ ok = gen_tcp:send(Sock, UnmaskedFrame),
{ok, ExpectFrame} = gen_tcp:recv(Sock, 0, 2000).
test_embedded_id_dir() ->

0 comments on commit 0281736

Please sign in to comment.