0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-16 05:00:24 +00:00

Reply with 400 on header parsing crash

This is a first step to improve the HTTP status codes returned
by Cowboy on crashes. We will tweak it over time.

Also fixes a small bug where two replies may have been sent
when using loop handlers under rare conditions.
This commit is contained in:
Loïc Hoguin 2014-07-12 12:09:43 +02:00
parent 20f598f373
commit 97a3108576
8 changed files with 55 additions and 26 deletions

View file

@ -1036,15 +1036,22 @@ continue(#http_req{socket=Socket, transport=Transport,
<< HTTPVer/binary, " ", (status(100))/binary, "\r\n\r\n" >>).
%% Meant to be used internally for sending errors after crashes.
-spec maybe_reply(cowboy:http_status(), req()) -> ok.
maybe_reply(Status, Req) ->
-spec maybe_reply([{module(), atom(), arity() | [term()], _}], req()) -> ok.
maybe_reply(Stacktrace, Req) ->
receive
{cowboy_req, resp_sent} -> ok
after 0 ->
_ = cowboy_req:reply(Status, Req),
_ = do_maybe_reply(Stacktrace, Req),
ok
end.
do_maybe_reply([
{cow_http_hd, _, _, _},
{cowboy_req, parse_header, _, _}|_], Req) ->
cowboy_req:reply(400, Req);
do_maybe_reply(_, Req) ->
cowboy_req:reply(500, Req).
-spec ensure_response(req(), cowboy:http_status()) -> ok.
%% The response has already been fully sent to the client.
ensure_response(#http_req{resp_state=done}, _) ->