diff options
-rw-r--r-- | src/cowboy_protocol.erl | 29 | ||||
-rw-r--r-- | src/cowboy_req.erl | 14 | ||||
-rw-r--r-- | src/cowboy_spdy.erl | 18 |
3 files changed, 26 insertions, 35 deletions
diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl index b42f524..40be2c0 100644 --- a/src/cowboy_protocol.erl +++ b/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. diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl index 66abcf8..32ff7b0 100644 --- a/src/cowboy_req.erl +++ b/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. diff --git a/src/cowboy_spdy.erl b/src/cowboy_spdy.erl index 182e6da..cc4d867 100644 --- a/src/cowboy_spdy.erl +++ b/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. |