Browse Source

Don't include a content-length in 204 responses

Loïc Hoguin 7 years ago
parent
commit
b0519af42a
2 changed files with 6 additions and 1 deletions
  1. 5 0
      src/cowboy_req.erl
  2. 1 1
      test/rfc7230_SUITE.erl

+ 5 - 0
src/cowboy_req.erl

@@ -725,6 +725,11 @@ 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) ->
+	do_reply(Status, Headers, Body, Req);
+reply(Status= <<"204",_/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#{

+ 1 - 1
test/rfc7230_SUITE.erl

@@ -1764,7 +1764,7 @@ no_content_length_in_204_response(Config) ->
 		"(RFC7230 3.3.1, RFC7230 3.3.2)"),
 	Client = raw_open(Config),
 	ok = raw_send(Client, [
-		"GET /resp/reply4/204 HTTP/1.1\r\n"
+		"GET /resp/reply3/204 HTTP/1.1\r\n"
 		"Host: localhost\r\n"
 		"\r\n"]),
 	{_, 204, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),