Revert "Don't use message queue for request handling"
This reverts commit 15dc622fd8fd7c59890bc7d057f332e0aea46a77.
diff --git a/src/mochiweb_http.erl b/src/mochiweb_http.erl
index 16a9480..38d51d4 100644
--- a/src/mochiweb_http.erl
+++ b/src/mochiweb_http.erl
@@ -57,22 +57,26 @@
request(Socket, Body).
request(Socket, Body) ->
- case mochiweb_socket:recv(Socket, 0, ?REQUEST_RECV_TIMEOUT) of
- {ok, {http_request, Method, Path, Version}} ->
+ ok = mochiweb_socket:setopts(Socket, [{active, once}]),
+ receive
+ {Protocol, _, {http_request, Method, Path, Version}} when Protocol == http orelse Protocol == ssl ->
ok = 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} ->
+ {tcp_closed, _} ->
mochiweb_socket:close(Socket),
exit(normal);
- {error, timeout} ->
+ {ssl_closed, _} ->
mochiweb_socket:close(Socket),
exit(normal);
Other ->
handle_invalid_msg_request(Other, Socket)
+ after ?REQUEST_RECV_TIMEOUT ->
+ mochiweb_socket:close(Socket),
+ exit(normal)
end.
reentry(Body) ->
@@ -85,21 +89,23 @@
ok = 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)}),
+ ok = 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_msg_request(Other, Socket, Request, Headers)
+ after ?HEADERS_RECV_TIMEOUT ->
+ mochiweb_socket:close(Socket),
+ exit(normal)
end.
call_body({M, F, A}, Req) ->
@@ -126,13 +132,15 @@
-spec handle_invalid_request(term(), term(), term()) -> no_return().
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) ->
+ ok = 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