Skip to content
This repository
Browse code

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

  • Loading branch information...
commit 02817364cdb70099b9c7c68e3cc10f9fe71b66ca 1 parent 954ccb1
Ahti Legonkov authored October 22, 2012 vinoski committed October 23, 2012
18  src/yaws_websockets.erl
@@ -466,7 +466,7 @@ ws_frame_info(#ws_state{sock=Socket},
466 466
     case check_control_frame(Len1, Opcode, Fin) of
467 467
         ok ->
468 468
             {ws_frame_info_secondary, Length, MaskingKey, Payload, Excess}
469  
-                = ws_frame_info_secondary(Socket, Len1, Rest),
  469
+                = ws_frame_info_secondary(Socket, Masked, Len1, Rest),
470 470
             FrameInfo = #ws_frame_info{fin=Fin,
471 471
                                        rsv=Rsv,
472 472
                                        opcode=opcode_to_atom(Opcode),
@@ -482,16 +482,21 @@ ws_frame_info(#ws_state{sock=Socket},
482 482
 ws_frame_info(State = #ws_state{sock=Socket}, FirstPacket) ->
483 483
     ws_frame_info(State, buffer(Socket, 2,FirstPacket)).
484 484
 
485  
-ws_frame_info_secondary(Socket, Len1, Rest) ->
  485
+
  486
+ws_frame_info_secondary(Socket, Masked, Len1, Rest) ->
  487
+    MaskLength = case Masked of
  488
+        0 -> 0;
  489
+        1 -> 4
  490
+    end,
486 491
     case Len1 of
487 492
         126 ->
488  
-            <<Len:16, MaskingKey:4/binary, Rest2/binary>> =
  493
+            <<Len:16, MaskingKey:MaskLength/binary, Rest2/binary>> =
489 494
                 buffer(Socket, 6, Rest);
490 495
         127 ->
491  
-            <<Len:64, MaskingKey:4/binary, Rest2/binary>> =
  496
+            <<Len:64, MaskingKey:MaskLength/binary, Rest2/binary>> =
492 497
                 buffer(Socket, 12, Rest);
493 498
         Len ->
494  
-            <<MaskingKey:4/binary, Rest2/binary>> = buffer(Socket, 4, Rest)
  499
+            <<MaskingKey:MaskLength/binary, Rest2/binary>> = buffer(Socket, 4, Rest)
495 500
     end,
496 501
     if
497 502
 	Len > ?MAX_PAYLOAD ->
@@ -659,6 +664,9 @@ mask(MaskBin, Data) ->
659 664
 rmask(_,<<>>) ->
660 665
     [<<>>];
661 666
 
  667
+rmask(<<>>, Data) ->
  668
+    [Data];
  669
+
662 670
 rmask(MaskBin = <<Mask:4/integer-unit:8>>,
663 671
       <<Data:4/integer-unit:8, Rest/binary>>) ->
664 672
     Masked = Mask bxor Data,
5  test/t2/app_test.erl
@@ -903,6 +903,11 @@ test_websocket() ->
903 903
         <<129, 133, "aaaa", 38722669838:5/integer-unit:8>>,
904 904
     ok = gen_tcp:send(Sock, Frame),
905 905
     ExpectFrame = <<129, 5, "hello">>,
  906
+    {ok, ExpectFrame} = gen_tcp:recv(Sock, 0, 2000),
  907
+
  908
+    %% Text frame "hello" without masking
  909
+    UnmaskedFrame = <<129, 5, "hello">>,
  910
+    ok = gen_tcp:send(Sock, UnmaskedFrame),
906 911
     {ok, ExpectFrame} = gen_tcp:recv(Sock, 0, 2000).
907 912
 
908 913
 test_embedded_id_dir() ->

0 notes on commit 0281736

Please sign in to comment.
Something went wrong with that request. Please try again.