couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From beno...@apache.org
Subject [31/50] mochiweb commit: updated refs/heads/import-upstream to 8eb1f22
Date Thu, 13 Feb 2014 16:06:26 GMT
refactor of request/5 function


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

Branch: refs/heads/import-upstream
Commit: 3961edcb51caed422abb6c6ce42c00bca52ab785
Parents: de79c0a
Author: Łukasz Lalik <lukasz.lalik@gmail.com>
Authored: Wed Dec 25 13:13:46 2013 +0100
Committer: Łukasz Lalik <lukasz.lalik@gmail.com>
Committed: Wed Dec 25 13:13:46 2013 +0100

----------------------------------------------------------------------
 examples/websocket/websocket.erl |  4 +--
 src/mochiweb_websocket.erl       | 66 ++++++++++++++++-------------------
 2 files changed, 33 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/blob/3961edcb/examples/websocket/websocket.erl
----------------------------------------------------------------------
diff --git a/examples/websocket/websocket.erl b/examples/websocket/websocket.erl
index 8d4eeb0..b70a0de 100644
--- a/examples/websocket/websocket.erl
+++ b/examples/websocket/websocket.erl
@@ -26,8 +26,8 @@
 
 start_link() ->
     Loop = fun (Req) ->
-                       ?MODULE:loop(Req)
-           end,
+            ?MODULE:loop(Req)
+        end,
     
     mochiweb_http:start_link([
                               {name,  client_access}, 

http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/blob/3961edcb/src/mochiweb_websocket.erl
----------------------------------------------------------------------
diff --git a/src/mochiweb_websocket.erl b/src/mochiweb_websocket.erl
index 677091b..3ef5c5e 100644
--- a/src/mochiweb_websocket.erl
+++ b/src/mochiweb_websocket.erl
@@ -46,33 +46,18 @@ request(Socket, Body, State, WsVersion, ReplyChannel) ->
 
         {tcp, _, WsFrames} ->
             {M, F} = Body,
-            case WsVersion of
-              hybi ->
-                Reply = fun(close) ->
-                                mochiweb_socket:close(Socket),
-                                exit(normal);
-                            (Payload) ->
-                                NewState = M:F(Payload, State, ReplyChannel),
-                                loop(Socket, Body, NewState, WsVersion, ReplyChannel)
-                        end,
-
-                try parse_hybi_frames(Socket, WsFrames, []) of
-                    Parsed -> process_frames(Parsed, Reply, [])
-                catch
-                    _:_ -> Reply(close)
-                end;
-
-              hixie ->
-                try parse_hixie_frames(WsFrames, []) of
-                    Payload -> 
-                        NewState = M:F(Payload, State),
-                        loop(Socket, Body, NewState, WsVersion, ReplyChannel)
-                catch
-                    _:_ ->
-                      mochiweb_socket:close(Socket),
-                      exit(normal)
-                end
-                
+            case parse_frames(WsVersion, WsFrames, Socket) of
+                close -> 
+                    mochiweb_socket:close(Socket),
+                    exit(normal);
+
+                error ->
+                    mochiweb_socket:close(Socket),
+                    exit(normal);
+
+                Payload ->
+                    NewState = M:F(Payload, State, ReplyChannel),
+                    loop(Socket, Body, NewState, WsVersion, ReplyChannel)
             end;
 
         _ ->
@@ -155,20 +140,31 @@ hixie_handshake(Host, Path, Key1, Key2, Body, Origin) ->
                     Challenge},
   {hixie, Response}.
 
+parse_frames(hybi, Frames, Socket) ->
+    try parse_hybi_frames(Socket, Frames, []) of
+        Parsed -> process_frames(Parsed, [])
+    catch
+        _:_ -> error
+    end;
+
+parse_frames(hixie, Frames, Socket) ->
+    try parse_hixie_frames(Frames, []) of
+        Payload -> Payload
+    catch
+        _:_ -> error
+    end.
+
 %%
 %% Websockets internal functions for RFC6455 and hybi draft
 %%
-process_frames([], Reply, Acc) ->
-    Reply(lists:reverse(Acc));
+process_frames([], Acc) ->
+    lists:reverse(Acc);
 
-process_frames([{Opcode, Payload} | Rest], Reply, Acc) ->
+process_frames([{Opcode, Payload} | Rest], Acc) ->
     case Opcode of
-        8 ->
-            Reply(lists:reverse(Acc)),
-            Reply(close);
-
+        8 -> close;
         _ ->
-            process_frames(Rest, Reply, [Payload | Acc])
+            process_frames(Rest, [Payload | Acc])
     end.
 
 parse_hybi_frames(_, <<>>, Acc) ->


Mime
View raw message