From 292039362a6125dfd0a163d5b0a49b800bf80b11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Wed, 14 Nov 2018 19:24:39 +0100 Subject: Don't send the content-length header in empty 304 responses It's OK to send it when set explicitly, as it can be set to what the representation's size would have been. --- src/cowboy_http.erl | 2 ++ src/cowboy_req.erl | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl index caa4932..56a4af8 100644 --- a/src/cowboy_http.erl +++ b/src/cowboy_http.erl @@ -963,6 +963,8 @@ commands(State0=#state{socket=Socket, transport=Transport, streams=Streams0, out {State1, Headers1} = case {Status, ContentLength, Version} of {204, _, 'HTTP/1.1'} -> {State0#state{out_state=done}, Headers0}; + {304, _, 'HTTP/1.1'} -> + {State0#state{out_state=done}, Headers0}; {_, undefined, 'HTTP/1.1'} -> {State0#state{out_state=chunked}, Headers0#{<<"transfer-encoding">> => <<"chunked">>}}; %% Close the connection after streaming without content-length to HTTP/1.0 client. diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl index 2c3de06..5633182 100644 --- a/src/cowboy_req.erl +++ b/src/cowboy_req.erl @@ -771,11 +771,15 @@ reply(Status, Headers, SendFile = {sendfile, _, Len, _}, Req) do_reply(Status, Headers#{ <<"content-length">> => integer_to_binary(Len) }, SendFile, Req); -%% 204 responses must not include content-length. (RFC7230 3.3.1, RFC7230 3.3.2) -reply(Status=204, Headers, Body, Req) -> +%% 204 responses must not include content-length. 304 responses may +%% but only when set explicitly. (RFC7230 3.3.1, RFC7230 3.3.2) +reply(Status, Headers, Body, Req) + when Status =:= 204; Status =:= 304 -> do_reply(Status, Headers, Body, Req); reply(Status= <<"204",_/bits>>, Headers, Body, Req) -> do_reply(Status, Headers, Body, Req); +reply(Status= <<"304",_/bits>>, Headers, Body, Req) -> + do_reply(Status, Headers, Body, Req); reply(Status, Headers, Body, Req) when is_integer(Status); is_binary(Status) -> do_reply(Status, Headers#{ -- cgit v1.2.3