You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2014/01/17 23:53:05 UTC

[28/50] git commit: {active, false} -> {active, once}

{active, false} -> {active, once}

Small refactoring


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

Branch: refs/heads/import
Commit: f11da939a8ddaa87244f9c1f53dd67a2ef70699c
Parents: ff2cdc9
Author: Dmitry Demeshchuk <de...@gmail.com>
Authored: Tue Nov 2 15:08:09 2010 +0300
Committer: Dmitry Demeshchuk <de...@gmail.com>
Committed: Tue Nov 2 15:08:09 2010 +0300

----------------------------------------------------------------------
 src/mochiweb_http.erl | 41 +++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/blob/f11da939/src/mochiweb_http.erl
----------------------------------------------------------------------
diff --git a/src/mochiweb_http.erl b/src/mochiweb_http.erl
index 2414099..012be5d 100644
--- a/src/mochiweb_http.erl
+++ b/src/mochiweb_http.erl
@@ -90,22 +90,23 @@ loop(Socket, Body) ->
     request(Socket, Body).
 
 request(Socket, Body) ->
-    case mochiweb_socket:recv(Socket, 0, ?REQUEST_RECV_TIMEOUT) of
-        {ok, {http_request, Method, Path, Version}} ->
+    mochiweb_socket:setopts(Socket, [{active, once}]),
+    receive
+        {Protocol, _, {http_request, Method, Path, Version}} when Protocol == http orelse Protocol == ssl ->
             mochiweb_socket:setopts(Socket, [{packet, httph}]),
             headers(Socket, {Method, Path, Version}, [], Body, 0);
-        {error, {http_error, "\r\n"}} ->
+        {Protocol, _, {http_error, "\r\n"}} when Protocol == http orelse Protocol == ssl ->
             request(Socket, Body);
-        {error, {http_error, "\n"}} ->
+        {Protocol, _, {http_error, "\n"}} when Protocol == http orelse Protocol == ssl ->
             request(Socket, Body);
-        {error, closed} ->
-            mochiweb_socket:close(Socket),
-            exit(normal);
-        {error, timeout} ->
+        {tcp_closed, _} ->
             mochiweb_socket:close(Socket),
             exit(normal);
         _Other ->
             handle_invalid_request(Socket)
+    after ?REQUEST_RECV_TIMEOUT ->
+        mochiweb_socket:close(Socket),
+        exit(normal)
     end.
 
 reentry(Body) ->
@@ -118,21 +119,23 @@ headers(Socket, Request, Headers, _Body, ?MAX_HEADERS) ->
     mochiweb_socket:setopts(Socket, [{packet, raw}]),
     handle_invalid_request(Socket, Request, Headers);
 headers(Socket, Request, Headers, Body, HeaderCount) ->
-    case mochiweb_socket:recv(Socket, 0, ?HEADERS_RECV_TIMEOUT) of
-        {ok, http_eoh} ->
-            mochiweb_socket:setopts(Socket, [{packet, raw}]),
-            Req = mochiweb:new_request({Socket, Request,
-                                        lists:reverse(Headers)}),
+    mochiweb_socket:setopts(Socket, [{active, once}]),
+    receive
+        {Protocol, _, http_eoh} when Protocol == http orelse Protocol == ssl ->
+            Req = new_request(Socket, Request, Headers),
             call_body(Body, Req),
             ?MODULE:after_response(Body, Req);
-        {ok, {http_header, _, Name, _, Value}} ->
+        {Protocol, _, {http_header, _, Name, _, Value}} when Protocol == http orelse Protocol == ssl ->
             headers(Socket, Request, [{Name, Value} | Headers], Body,
                     1 + HeaderCount);
-        {error, closed} ->
+        {tcp_closed, _} ->
             mochiweb_socket:close(Socket),
             exit(normal);
         _Other ->
             handle_invalid_request(Socket, Request, Headers)
+    after ?HEADERS_RECV_TIMEOUT ->
+        mochiweb_socket:close(Socket),
+        exit(normal)
     end.
 
 call_body({M, F}, Req) ->
@@ -144,13 +147,15 @@ handle_invalid_request(Socket) ->
     handle_invalid_request(Socket, {'GET', {abs_path, "/"}, {0,9}}, []).
 
 handle_invalid_request(Socket, Request, RevHeaders) ->
-    mochiweb_socket:setopts(Socket, [{packet, raw}]),
-    Req = mochiweb:new_request({Socket, Request,
-                                lists:reverse(RevHeaders)}),
+    Req = new_request(Socket, Request, RevHeaders),
     Req:respond({400, [], []}),
     mochiweb_socket:close(Socket),
     exit(normal).
 
+new_request(Socket, Request, RevHeaders) ->
+    mochiweb_socket:setopts(Socket, [{packet, raw}]),
+    mochiweb:new_request({Socket, Request, lists:reverse(RevHeaders)}).
+
 after_response(Body, Req) ->
     Socket = Req:get(socket),
     case Req:should_close() of