Browse Source

Simpler code for sending errors following crashes

Loïc Hoguin 11 years ago
parent
commit
bfb6db1eab
3 changed files with 26 additions and 35 deletions
  1. 8 21
      src/cowboy_protocol.erl
  2. 14 0
      src/cowboy_req.erl
  3. 4 14
      src/cowboy_spdy.erl

+ 8 - 21
src/cowboy_protocol.erl

@@ -573,29 +573,16 @@ next_request(Req, State=#state{req_keepalive=Keepalive, timeout=Timeout},
 			end
 	end.
 
-%% Only send an error reply if there is no resp_sent message.
--spec error_terminate(cowboy:http_status(), cowboy_req:req(), #state{}) -> ok.
-error_terminate(Code, Req, State) ->
-	receive
-		{cowboy_req, resp_sent} -> ok
-	after 0 ->
-		_ = cowboy_req:reply(Code, Req),
-		ok
-	end,
-	terminate(State).
-
-%% Only send an error reply if there is no resp_sent message.
 -spec error_terminate(cowboy:http_status(), #state{}) -> ok.
-error_terminate(Code, State=#state{socket=Socket, transport=Transport,
+error_terminate(Status, State=#state{socket=Socket, transport=Transport,
 		compress=Compress, onresponse=OnResponse}) ->
-	receive
-		{cowboy_req, resp_sent} -> ok
-	after 0 ->
-		_ = cowboy_req:reply(Code, cowboy_req:new(Socket, Transport,
-			undefined, <<"GET">>, <<>>, <<>>, 'HTTP/1.1', [], <<>>,
-			undefined, <<>>, false, Compress, OnResponse)),
-		ok
-	end,
+	error_terminate(Status, cowboy_req:new(Socket, Transport,
+		undefined, <<"GET">>, <<>>, <<>>, 'HTTP/1.1', [], <<>>,
+		undefined, <<>>, false, Compress, OnResponse), State).
+
+-spec error_terminate(cowboy:http_status(), cowboy_req:req(), #state{}) -> ok.
+error_terminate(Status, Req, State) ->
+	cowboy_req:maybe_reply(Status, Req),
 	terminate(State).
 
 -spec terminate(#state{}) -> ok.

+ 14 - 0
src/cowboy_req.erl

@@ -101,6 +101,7 @@
 -export([chunked_reply/3]).
 -export([chunk/2]).
 -export([upgrade_reply/3]).
+-export([maybe_reply/2]).
 -export([ensure_response/2]).
 
 %% Private setter/getter API.
@@ -1120,6 +1121,19 @@ upgrade_reply(Status, Headers, Req=#http_req{transport=Transport,
 	], <<>>, Req),
 	{ok, Req2#http_req{resp_state=done, resp_headers=[], resp_body= <<>>}}.
 
+%% @doc Send a reply if one hasn't been sent already.
+%%
+%% Meant to be used internally for sending errors after crashes.
+%% @private
+-spec maybe_reply(cowboy:http_status(), req()) -> ok.
+maybe_reply(Status, Req) ->
+	receive
+		{cowboy_req, resp_sent} -> ok
+	after 0 ->
+		_ = cowboy_req:reply(Status, Req),
+		ok
+	end.
+
 %% @doc Ensure the response has been sent fully.
 %% @private
 -spec ensure_response(req(), cowboy:http_status()) -> ok.

+ 4 - 14
src/cowboy_spdy.erl

@@ -520,8 +520,8 @@ execute(Req, Env, [Middleware|Tail]) ->
 				[Env, Tail, Module, Function, Args]);
 		{halt, Req2} ->
 			cowboy_req:ensure_response(Req2, 204);
-		{error, Code, Req2} ->
-			error_terminate(Code, Req2)
+		{error, Status, Req2} ->
+			cowboy_req:maybe_reply(Status, Req2)
 	end.
 
 %% @private
@@ -536,18 +536,8 @@ resume(Env, Tail, Module, Function, Args) ->
 				[Env, Tail, Module2, Function2, Args2]);
 		{halt, Req2} ->
 			cowboy_req:ensure_response(Req2, 204);
-		{error, Code, Req2} ->
-			error_terminate(Code, Req2)
-	end.
-
-%% Only send an error reply if there is no resp_sent message.
--spec error_terminate(cowboy:http_status(), cowboy_req:req()) -> ok.
-error_terminate(Code, Req) ->
-	receive
-		{cowboy_req, resp_sent} -> ok
-	after 0 ->
-		_ = cowboy_req:reply(Code, Req),
-		ok
+		{error, Status, Req2} ->
+			cowboy_req:maybe_reply(Status, Req2)
 	end.
 
 %% Reply functions used by cowboy_req.