Skip to content

Instantly share code, notes, and snippets.

@rnewson
Created August 25, 2015 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rnewson/99ad0accff39b2cdc01e to your computer and use it in GitHub Desktop.
Save rnewson/99ad0accff39b2cdc01e to your computer and use it in GitHub Desktop.
Don't use the message queue
commit 944d2d81226c7d442ee8d6fa67b2e153e29b7d84
Author: Robert Newson <rnewson@apache.org>
Date: Tue Aug 25 12:57:39 2015 +0100
Don't use the message queue
diff --git a/src/mochiweb_http.erl b/src/mochiweb_http.erl
index 1ea1f15..9e32d7d 100644
--- a/src/mochiweb_http.erl
+++ b/src/mochiweb_http.erl
@@ -57,26 +57,22 @@ loop(Socket, Opts, Body) ->
request(Socket, Opts, Body).
request(Socket, Opts, Body) ->
- ok = mochiweb_socket:setopts(Socket, [{active, once}]),
- receive
- {Protocol, _, {http_request, Method, Path, Version}} when Protocol == http orelse Protocol == ssl ->
+ case mochiweb_socket:recv(Socket, 0, ?REQUEST_RECV_TIMEOUT) of
+ {ok, {http_request, Method, Path, Version}} ->
ok = mochiweb_socket:setopts(Socket, [{packet, httph}]),
headers(Socket, Opts, {Method, Path, Version}, [], Body, 0);
- {Protocol, _, {http_error, "\r\n"}} when Protocol == http orelse Protocol == ssl ->
+ {error, {http_error, "\r\n"}} ->
request(Socket, Opts, Body);
- {Protocol, _, {http_error, "\n"}} when Protocol == http orelse Protocol == ssl ->
+ {error, {http_error, "\n"}} ->
request(Socket, Opts, Body);
- {tcp_closed, _} ->
+ {error, closed} ->
mochiweb_socket:close(Socket),
exit(normal);
- {ssl_closed, _} ->
+ {error, timeout} ->
mochiweb_socket:close(Socket),
exit(normal);
Other ->
handle_invalid_msg_request(Other, Socket, Opts)
- after ?REQUEST_RECV_TIMEOUT ->
- mochiweb_socket:close(Socket),
- exit(normal)
end.
reentry(Body) ->
@@ -89,23 +85,22 @@ headers(Socket, Opts, Request, Headers, _Body, ?MAX_HEADERS) ->
ok = mochiweb_socket:setopts(Socket, [{packet, raw}]),
handle_invalid_request(Socket, Opts, Request, Headers);
headers(Socket, Opts, Request, Headers, Body, HeaderCount) ->
- ok = mochiweb_socket:setopts(Socket, [{active, once}]),
- receive
- {Protocol, _, http_eoh} when Protocol == http orelse Protocol == ssl ->
+ case mochiweb_socket:recv(Socket, 0, ?REQUEST_RECV_TIMEOUT) of
+ {ok, http_eoh} ->
Req = new_request(Socket, Opts, Request, Headers),
call_body(Body, Req),
?MODULE:after_response(Body, Req);
- {Protocol, _, {http_header, _, Name, _, Value}} when Protocol == http orelse Protocol == ssl ->
+ {ok, {http_header, _, Name, _, Value}} ->
headers(Socket, Opts, Request, [{Name, Value} | Headers], Body,
1 + HeaderCount);
- {tcp_closed, _} ->
+ {error, closed} ->
+ mochiweb_socket:close(Socket),
+ exit(normal);
+ {error, timeout} ->
mochiweb_socket:close(Socket),
exit(normal);
Other ->
handle_invalid_msg_request(Other, Socket, Opts, Request, Headers)
- after ?HEADERS_RECV_TIMEOUT ->
- mochiweb_socket:close(Socket),
- exit(normal)
end.
call_body({M, F, A}, Req) ->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment