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

Fix 408 not sending connection: close for HTTP/1.1

Also make sure the header is sent for all types of early_error
that result in the closing of the connection.
This commit is contained in:
Loïc Hoguin 2017-12-06 16:30:30 +01:00
parent 1f4c1e2c67
commit 5269bf580b
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
2 changed files with 47 additions and 4 deletions

View file

@ -1157,16 +1157,22 @@ error_terminate(StatusCode, State=#state{ref=Ref, peer=Peer, in_state=StreamStat
end
}
end,
early_error(StatusCode, State, Reason, PartialReq),
early_error(StatusCode, State, Reason, PartialReq, #{<<"connection">> => <<"close">>}),
terminate(State, Reason).
early_error(StatusCode, State, Reason, PartialReq) ->
early_error(StatusCode, State, Reason, PartialReq, #{}).
early_error(StatusCode0, #state{socket=Socket, transport=Transport,
opts=Opts, in_streamid=StreamID}, Reason, PartialReq) ->
Resp = {response, StatusCode0, RespHeaders0=#{<<"content-length">> => <<"0">>}, <<>>},
opts=Opts, in_streamid=StreamID}, Reason, PartialReq, RespHeaders0) ->
RespHeaders1 = RespHeaders0#{<<"content-length">> => <<"0">>},
Resp = {response, StatusCode0, RespHeaders1, <<>>},
try cowboy_stream:early_error(StreamID, Reason, PartialReq, Resp, Opts) of
{response, StatusCode, RespHeaders, RespBody} ->
Transport:send(Socket, [
cow_http:response(StatusCode, 'HTTP/1.1', maps:to_list(RespHeaders)),
%% @todo We shouldn't send the body when the method is HEAD.
%% @todo Technically we allow the sendfile tuple.
RespBody
])
catch Class:Exception ->
@ -1176,7 +1182,7 @@ early_error(StatusCode0, #state{socket=Socket, transport=Transport,
%% We still need to send an error response, so send what we initially
%% wanted to send. It's better than nothing.
Transport:send(Socket, cow_http:response(StatusCode0,
'HTTP/1.1', maps:to_list(RespHeaders0)))
'HTTP/1.1', maps:to_list(RespHeaders1)))
end,
ok.