From 5269bf580bc1bc75bb1afe59d52ef2c4772ff12d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 6 Dec 2017 16:30:30 +0100 Subject: 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. --- src/cowboy_http.erl | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/cowboy_http.erl') diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl index 71b7099..bc8fbb9 100644 --- a/src/cowboy_http.erl +++ b/src/cowboy_http.erl @@ -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. -- cgit v1.2.3