mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Simpler code for sending errors following crashes
This commit is contained in:
parent
ac6c460169
commit
bfb6db1eab
3 changed files with 26 additions and 35 deletions
|
@ -573,29 +573,16 @@ next_request(Req, State=#state{req_keepalive=Keepalive, timeout=Timeout},
|
||||||
end
|
end
|
||||||
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.
|
-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}) ->
|
compress=Compress, onresponse=OnResponse}) ->
|
||||||
receive
|
error_terminate(Status, cowboy_req:new(Socket, Transport,
|
||||||
{cowboy_req, resp_sent} -> ok
|
undefined, <<"GET">>, <<>>, <<>>, 'HTTP/1.1', [], <<>>,
|
||||||
after 0 ->
|
undefined, <<>>, false, Compress, OnResponse), State).
|
||||||
_ = cowboy_req:reply(Code, cowboy_req:new(Socket, Transport,
|
|
||||||
undefined, <<"GET">>, <<>>, <<>>, 'HTTP/1.1', [], <<>>,
|
-spec error_terminate(cowboy:http_status(), cowboy_req:req(), #state{}) -> ok.
|
||||||
undefined, <<>>, false, Compress, OnResponse)),
|
error_terminate(Status, Req, State) ->
|
||||||
ok
|
cowboy_req:maybe_reply(Status, Req),
|
||||||
end,
|
|
||||||
terminate(State).
|
terminate(State).
|
||||||
|
|
||||||
-spec terminate(#state{}) -> ok.
|
-spec terminate(#state{}) -> ok.
|
||||||
|
|
|
@ -101,6 +101,7 @@
|
||||||
-export([chunked_reply/3]).
|
-export([chunked_reply/3]).
|
||||||
-export([chunk/2]).
|
-export([chunk/2]).
|
||||||
-export([upgrade_reply/3]).
|
-export([upgrade_reply/3]).
|
||||||
|
-export([maybe_reply/2]).
|
||||||
-export([ensure_response/2]).
|
-export([ensure_response/2]).
|
||||||
|
|
||||||
%% Private setter/getter API.
|
%% Private setter/getter API.
|
||||||
|
@ -1120,6 +1121,19 @@ upgrade_reply(Status, Headers, Req=#http_req{transport=Transport,
|
||||||
], <<>>, Req),
|
], <<>>, Req),
|
||||||
{ok, Req2#http_req{resp_state=done, resp_headers=[], resp_body= <<>>}}.
|
{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.
|
%% @doc Ensure the response has been sent fully.
|
||||||
%% @private
|
%% @private
|
||||||
-spec ensure_response(req(), cowboy:http_status()) -> ok.
|
-spec ensure_response(req(), cowboy:http_status()) -> ok.
|
||||||
|
|
|
@ -520,8 +520,8 @@ execute(Req, Env, [Middleware|Tail]) ->
|
||||||
[Env, Tail, Module, Function, Args]);
|
[Env, Tail, Module, Function, Args]);
|
||||||
{halt, Req2} ->
|
{halt, Req2} ->
|
||||||
cowboy_req:ensure_response(Req2, 204);
|
cowboy_req:ensure_response(Req2, 204);
|
||||||
{error, Code, Req2} ->
|
{error, Status, Req2} ->
|
||||||
error_terminate(Code, Req2)
|
cowboy_req:maybe_reply(Status, Req2)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
|
@ -536,18 +536,8 @@ resume(Env, Tail, Module, Function, Args) ->
|
||||||
[Env, Tail, Module2, Function2, Args2]);
|
[Env, Tail, Module2, Function2, Args2]);
|
||||||
{halt, Req2} ->
|
{halt, Req2} ->
|
||||||
cowboy_req:ensure_response(Req2, 204);
|
cowboy_req:ensure_response(Req2, 204);
|
||||||
{error, Code, Req2} ->
|
{error, Status, Req2} ->
|
||||||
error_terminate(Code, Req2)
|
cowboy_req:maybe_reply(Status, 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
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% Reply functions used by cowboy_req.
|
%% Reply functions used by cowboy_req.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue