couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From beno...@apache.org
Subject [41/50] mochiweb commit: updated refs/heads/import-upstream to 8eb1f22
Date Thu, 13 Feb 2014 16:06:36 GMT
clean up whitespace and long lines


Project: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/commit/74d6cdc7
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/tree/74d6cdc7
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/diff/74d6cdc7

Branch: refs/heads/import-upstream
Commit: 74d6cdc7efc70e3024458ad0d7da3104233348b5
Parents: ef24a57
Author: Bob Ippolito <bob@redivi.com>
Authored: Thu Dec 26 10:03:07 2013 -0800
Committer: Bob Ippolito <bob@redivi.com>
Committed: Thu Dec 26 10:07:27 2013 -0800

----------------------------------------------------------------------
 src/mochiweb_websocket.erl        | 141 ++++++++++++++-------------------
 test/mochiweb_websocket_tests.erl |  71 ++++++++++-------
 2 files changed, 103 insertions(+), 109 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/blob/74d6cdc7/src/mochiweb_websocket.erl
----------------------------------------------------------------------
diff --git a/src/mochiweb_websocket.erl b/src/mochiweb_websocket.erl
index 3b87629..e5aff05 100644
--- a/src/mochiweb_websocket.erl
+++ b/src/mochiweb_websocket.erl
@@ -30,7 +30,8 @@
 
 loop(Socket, Body, State, WsVersion, ReplyChannel) ->
     ok = mochiweb_socket:setopts(Socket, [{packet, 0}, {active, once}]),
-    proc_lib:hibernate(?MODULE, request, [Socket, Body, State, WsVersion, ReplyChannel]).
+    proc_lib:hibernate(?MODULE, request,
+                       [Socket, Body, State, WsVersion, ReplyChannel]).
 
 request(Socket, Body, State, WsVersion, ReplyChannel) ->
     receive
@@ -43,22 +44,18 @@ request(Socket, Body, State, WsVersion, ReplyChannel) ->
         {tcp_error, _, _} ->
             mochiweb_socket:close(Socket),
             exit(normal);
-
         {tcp, _, WsFrames} ->
             case parse_frames(WsVersion, WsFrames, Socket) of
                 close ->
                     mochiweb_socket:close(Socket),
                     exit(normal);
-
                 error ->
                     mochiweb_socket:close(Socket),
                     exit(normal);
-
                 Payload ->
                     NewState = call_body(Body, Payload, State, ReplyChannel),
                     loop(Socket, Body, NewState, WsVersion, ReplyChannel)
             end;
-
         _ ->
             mochiweb_socket:close(Socket),
             exit(normal)
@@ -75,7 +72,6 @@ send(Socket, Payload, hybi) ->
     Len = payload_length(iolist_size(Payload)),
     Data = <<1:1, 0:3, 1:4, 0:1, Len/bits, Payload/binary>>,
     mochiweb_socket:send(Socket, Data);
-
 send(Socket, Payload, hixie) ->
     Data = <<0, Payload/binary, 255>>,
     mochiweb_socket:send(Socket, Data).
@@ -120,7 +116,6 @@ hybi_handshake(SecKey) ->
     BinKey = list_to_binary(SecKey),
     Bin = <<BinKey/binary, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11">>,
     Challenge = base64:encode(crypto:hash(sha, Bin)),
-
     Response = {101, [{"Connection", "Upgrade"},
                       {"Upgrade", "websocket"},
                       {"Sec-Websocket-Accept", Challenge}], ""},
@@ -135,23 +130,23 @@ scheme(Req) ->
     end.
 
 hixie_handshake(Scheme, Host, Path, Key1, Key2, Body, Origin) ->
-  Ikey1 = [D || D <- Key1, $0 =< D, D =< $9],
-  Ikey2 = [D || D <- Key2, $0 =< D, D =< $9],
-  Blank1 = length([D || D <- Key1, D =:= 32]),
-  Blank2 = length([D || D <- Key2, D =:= 32]),
-  Part1 = erlang:list_to_integer(Ikey1) div Blank1,
-  Part2 = erlang:list_to_integer(Ikey2) div Blank2,
-  Ckey = <<Part1:4/big-unsigned-integer-unit:8, Part2:4/big-unsigned-integer-unit:8,
Body/binary>>,
-  Challenge = erlang:md5(Ckey),
-
-  Location = lists:concat([Scheme, Host, Path]),
-
-  Response = {101, [{"Upgrade", "WebSocket"},
-                     {"Connection", "Upgrade"},
-                     {"Sec-WebSocket-Origin", Origin},
-                     {"Sec-WebSocket-Location", Location}],
-                    Challenge},
-  {hixie, Response}.
+    Ikey1 = [D || D <- Key1, $0 =< D, D =< $9],
+    Ikey2 = [D || D <- Key2, $0 =< D, D =< $9],
+    Blank1 = length([D || D <- Key1, D =:= 32]),
+    Blank2 = length([D || D <- Key2, D =:= 32]),
+    Part1 = erlang:list_to_integer(Ikey1) div Blank1,
+    Part2 = erlang:list_to_integer(Ikey2) div Blank2,
+    Ckey = <<Part1:4/big-unsigned-integer-unit:8,
+            Part2:4/big-unsigned-integer-unit:8,
+            Body/binary>>,
+    Challenge = erlang:md5(Ckey),
+    Location = lists:concat([Scheme, Host, Path]),
+    Response = {101, [{"Upgrade", "WebSocket"},
+                      {"Connection", "Upgrade"},
+                      {"Sec-WebSocket-Origin", Origin},
+                      {"Sec-WebSocket-Location", Location}],
+                Challenge},
+    {hixie, Response}.
 
 parse_frames(hybi, Frames, Socket) ->
     try parse_hybi_frames(Socket, Frames, []) of
@@ -159,7 +154,6 @@ parse_frames(hybi, Frames, Socket) ->
     catch
         _:_ -> error
     end;
-
 parse_frames(hixie, Frames, _Socket) ->
     try parse_hixie_frames(Frames, []) of
         Payload -> Payload
@@ -172,7 +166,6 @@ parse_frames(hixie, Frames, _Socket) ->
 %%
 process_frames([], Acc) ->
     lists:reverse(Acc);
-
 process_frames([{Opcode, Payload} | Rest], Acc) ->
     case Opcode of
         8 -> close;
@@ -182,43 +175,38 @@ process_frames([{Opcode, Payload} | Rest], Acc) ->
 
 parse_hybi_frames(_, <<>>, Acc) ->
     lists:reverse(Acc);
-
 parse_hybi_frames(S, <<_Fin:1,
-               _Rsv:3,
-               Opcode:4,
-               _Mask:1,
-               PayloadLen:7,
-               MaskKey:4/binary,
-               Payload:PayloadLen/binary-unit:8,
-               Rest/binary>>,
-             Acc) when PayloadLen < 126 ->
-
+                      _Rsv:3,
+                      Opcode:4,
+                      _Mask:1,
+                      PayloadLen:7,
+                      MaskKey:4/binary,
+                      Payload:PayloadLen/binary-unit:8,
+                      Rest/binary>>,
+                  Acc) when PayloadLen < 126 ->
     Payload2 = hybi_unmask(Payload, MaskKey, <<>>),
     parse_hybi_frames(S, Rest, [{Opcode, Payload2} | Acc]);
-
 parse_hybi_frames(S, <<_Fin:1,
-               _Rsv:3,
-               Opcode:4,
-               _Mask:1,
-               126:7,
-               PayloadLen:16,
-               MaskKey:4/binary,
-               Payload:PayloadLen/binary-unit:8,
-               Rest/binary>>,
-             Acc) ->
-
+                      _Rsv:3,
+                      Opcode:4,
+                      _Mask:1,
+                      126:7,
+                      PayloadLen:16,
+                      MaskKey:4/binary,
+                      Payload:PayloadLen/binary-unit:8,
+                      Rest/binary>>,
+                  Acc) ->
     Payload2 = hybi_unmask(Payload, MaskKey, <<>>),
     parse_hybi_frames(S, Rest, [{Opcode, Payload2} | Acc]);
-
 parse_hybi_frames(Socket, <<_Fin:1,
-               _Rsv:3,
-               _Opcode:4,
-               _Mask:1,
-               126:7,
-               _PayloadLen:16,
-               _MaskKey:4/binary,
-               _/binary-unit:8>> = PartFrame,
-             Acc) ->
+                           _Rsv:3,
+                           _Opcode:4,
+                           _Mask:1,
+                           126:7,
+                           _PayloadLen:16,
+                           _MaskKey:4/binary,
+                           _/binary-unit:8>> = PartFrame,
+                  Acc) ->
 
     ok = mochiweb_socket:setopts(Socket, [{packet, 0}, {active, once}]),
     receive
@@ -231,10 +219,9 @@ parse_hybi_frames(Socket, <<_Fin:1,
         {tcp_error, _, _} ->
             mochiweb_socket:close(Socket),
             exit(normal);
-
         {tcp, _, Continuation} ->
-          parse_hybi_frames(Socket, <<PartFrame/binary, Continuation/binary>>,
Acc);
-
+            parse_hybi_frames(Socket, <<PartFrame/binary, Continuation/binary>>,
+                              Acc);
         _ ->
             mochiweb_socket:close(Socket),
             exit(normal)
@@ -243,39 +230,33 @@ parse_hybi_frames(Socket, <<_Fin:1,
         mochiweb_socket:close(Socket),
         exit(normal)
     end;
-
 parse_hybi_frames(S, <<_Fin:1,
-               _Rsv:3,
-               Opcode:4,
-               _Mask:1,
-               127:7,
-               0:1,
-               PayloadLen:63,
-               MaskKey:4/binary,
-               Payload:PayloadLen/binary-unit:8,
-               Rest/binary>>,
-             Acc) ->
-
+                      _Rsv:3,
+                      Opcode:4,
+                      _Mask:1,
+                      127:7,
+                      0:1,
+                      PayloadLen:63,
+                      MaskKey:4/binary,
+                      Payload:PayloadLen/binary-unit:8,
+                      Rest/binary>>,
+                  Acc) ->
     Payload2 = hybi_unmask(Payload, MaskKey, <<>>),
     parse_hybi_frames(S, Rest, [{Opcode, Payload2} | Acc]).
 
-% Unmasks RFC 6455 message
+%% Unmasks RFC 6455 message
 hybi_unmask(<<O:32, Rest/bits>>, MaskKey, Acc) ->
     <<MaskKey2:32>> = MaskKey,
-    T = O bxor MaskKey2,
-    hybi_unmask(Rest, MaskKey, <<Acc/binary, T:32>>);
+    hybi_unmask(Rest, MaskKey, <<Acc/binary, (O bxor MaskKey2):32>>);
 hybi_unmask(<<O:24>>, MaskKey, Acc) ->
     <<MaskKey2:24, _:8>> = MaskKey,
-    T = O bxor MaskKey2,
-    <<Acc/binary, T:24>>;
+    <<Acc/binary, (O bxor MaskKey2):24>>;
 hybi_unmask(<<O:16>>, MaskKey, Acc) ->
     <<MaskKey2:16, _:16>> = MaskKey,
-    T = O bxor MaskKey2,
-    <<Acc/binary, T:16>>;
+    <<Acc/binary, (O bxor MaskKey2):16>>;
 hybi_unmask(<<O:8>>, MaskKey, Acc) ->
     <<MaskKey2:8, _:24>> = MaskKey,
-    T = O bxor MaskKey2,
-    <<Acc/binary, T:8>>;
+    <<Acc/binary, (O bxor MaskKey2):8>>;
 hybi_unmask(<<>>, _MaskKey, Acc) ->
     Acc.
 

http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/blob/74d6cdc7/test/mochiweb_websocket_tests.erl
----------------------------------------------------------------------
diff --git a/test/mochiweb_websocket_tests.erl b/test/mochiweb_websocket_tests.erl
index 98cb860..890aa17 100644
--- a/test/mochiweb_websocket_tests.erl
+++ b/test/mochiweb_websocket_tests.erl
@@ -23,49 +23,62 @@
 %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 %% THE SOFTWARE.
 
--ifdef(TEST).
 -include_lib("eunit/include/eunit.hrl").
 
 make_handshake_for_correct_client_test() ->
     %% Hybi handshake
-    Req1 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
-        mochiweb_headers:make([{"Sec-WebSocket-Key", "Xn3fdKyc3qEXPuj2A3O+ZA=="}])),
+    Req1 = mochiweb_request:new(
+             nil, 'GET', "/foo", {1, 1},
+             mochiweb_headers:make([{"Sec-WebSocket-Key",
+                                     "Xn3fdKyc3qEXPuj2A3O+ZA=="}])),
 
-    {Version1, {HttpCode1, Headers1, _}} = mochiweb_websocket:make_handshake(Req1),
+    {Version1,
+     {HttpCode1, Headers1, _}} = mochiweb_websocket:make_handshake(Req1),
     ?assertEqual(hybi, Version1),
     ?assertEqual(101, HttpCode1),
     ?assertEqual("Upgrade", proplists:get_value("Connection", Headers1)),
-    ?assertEqual(<<"BIFTHkJk4r5t8kuud82tZJaQsCE=">>, proplists:get_value("Sec-Websocket-Accept",
Headers1)),
+    ?assertEqual(<<"BIFTHkJk4r5t8kuud82tZJaQsCE=">>,
+                 proplists:get_value("Sec-Websocket-Accept", Headers1)),
 
     %% Hixie handshake
-    {Version2, {HttpCode2, Headers2, Body2}} = mochiweb_websocket:hixie_handshake(
-                                            "ws://",
-                                            "localhost", "/",
-                                            "33j284    9  z63 e 9 7",
-                                            "TF'3|6D12659H 7 70",
-                                            <<175,181,191,215,128,195,144,120>>,
-                                            "null"),
+    {Version2, {HttpCode2, Headers2, Body2}} =
+        mochiweb_websocket:hixie_handshake(
+          "ws://",
+          "localhost", "/",
+          "33j284    9  z63 e 9 7",
+          "TF'3|6D12659H 7 70",
+          <<175,181,191,215,128,195,144,120>>,
+          "null"),
     ?assertEqual(hixie, Version2),
     ?assertEqual(101, HttpCode2),
     ?assertEqual("null", proplists:get_value("Sec-WebSocket-Origin", Headers2)),
-    ?assertEqual("ws://localhost/", proplists:get_value("Sec-WebSocket-Location", Headers2)),
-    ?assertEqual(<<230,144,237,94,84,214,41,69,244,150,134,167,221,103,239,246>>,
Body2).
+    ?assertEqual("ws://localhost/",
+                 proplists:get_value("Sec-WebSocket-Location", Headers2)),
+    ?assertEqual(
+       <<230,144,237,94,84,214,41,69,244,150,134,167,221,103,239,246>>,
+       Body2).
 
 hybi_frames_decode_test() ->
-    Resp1 = mochiweb_websocket:parse_hybi_frames(nil, <<129,131,118,21,153,58,16,122,246>>,
[]),
-    ?assertEqual([{1, <<"foo">>}], Resp1),
-
-    Resp2 = mochiweb_websocket:parse_hybi_frames(nil, <<129,131,1,225,201,42,103,142,166,129,131,93,222,214,66,63,191,164>>,
[]),
-    ?assertEqual([{1, <<"foo">>}, {1, <<"bar">>}], Resp2).
+    ?assertEqual(
+       [{1, <<"foo">>}],
+       mochiweb_websocket:parse_hybi_frames(
+         nil, <<129,131,118,21,153,58,16,122,246>>, [])),
+    ?assertEqual(
+       [{1, <<"foo">>}, {1, <<"bar">>}],
+       mochiweb_websocket:parse_hybi_frames(
+         nil,
+         <<129,131,1,225,201,42,103,142,166,129,131,93,222,214,66,63,191,164>>,
+         [])).
 
 hixie_frames_decode_test() ->
-    Resp1 = mochiweb_websocket:parse_hixie_frames(<<>>, []),
-    ?assertEqual([], Resp1),
-
-    Resp2 = mochiweb_websocket:parse_hixie_frames(<<0,102,111,111,255>>, []),
-    ?assertEqual([<<"foo">>], Resp2),
-
-    Resp3 = mochiweb_websocket:parse_hixie_frames(<<0,102,111,111,255,0,98,97,114,255>>,
[]),
-    ?assertEqual([<<"foo">>, <<"bar">>], Resp3).
-
--endif.
+    ?assertEqual(
+       [],
+       mochiweb_websocket:parse_hixie_frames(<<>>, [])),
+    ?assertEqual(
+       [<<"foo">>],
+       mochiweb_websocket:parse_hixie_frames(<<0,102,111,111,255>>, [])),
+    ?assertEqual(
+       [<<"foo">>, <<"bar">>],
+       mochiweb_websocket:parse_hixie_frames(
+         <<0,102,111,111,255,0,98,97,114,255>>,
+         [])).


Mime
View raw message