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

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.
This commit is contained in:
Loïc Hoguin 2018-11-14 19:24:39 +01:00
parent 4fedb33631
commit 292039362a
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
4 changed files with 74 additions and 5 deletions

View file

@ -1745,9 +1745,55 @@ no_body_in_head_response(Config) ->
%no_body_in_100_response(Config) ->
%no_body_in_101_response(Config) ->
%no_body_in_102_response(Config) ->
%no_body_in_204_response(Config) ->
%no_body_in_304_response(Config) ->
%1xx, 204 and 304 responses never include a message body. (RFC7230 3.3)
%1xx responses never include a message body. (RFC7230 3.3)
no_body_in_204_response(Config) ->
doc("204 responses never include a message body. (RFC7230 3.3)"),
Client = raw_open(Config),
ok = raw_send(Client, [
"GET /resp/reply2/204 HTTP/1.1\r\n"
"Host: localhost\r\n"
"\r\n"]),
{_, 204, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
{_, <<>>} = cow_http:parse_headers(Rest),
{error, timeout} = raw_recv(Client, 1, 1000),
ok.
no_body_in_204_response_stream(Config) ->
doc("204 responses never include a message body. (RFC7230 3.3)"),
Client = raw_open(Config),
ok = raw_send(Client, [
"GET /resp/stream_reply2/204 HTTP/1.1\r\n"
"Host: localhost\r\n"
"\r\n"]),
{_, 204, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
{_, <<>>} = cow_http:parse_headers(Rest),
{error, timeout} = raw_recv(Client, 1, 1000),
ok.
no_body_in_304_response(Config) ->
doc("304 responses never include a message body. (RFC7230 3.3)"),
Client = raw_open(Config),
ok = raw_send(Client, [
"GET /resp/reply2/304 HTTP/1.1\r\n"
"Host: localhost\r\n"
"\r\n"]),
{_, 304, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
{_, <<>>} = cow_http:parse_headers(Rest),
{error, timeout} = raw_recv(Client, 1, 1000),
ok.
no_body_in_304_response_stream(Config) ->
doc("304 responses never include a message body. (RFC7230 3.3)"),
Client = raw_open(Config),
ok = raw_send(Client, [
"GET /resp/stream_reply2/304 HTTP/1.1\r\n"
"Host: localhost\r\n"
"\r\n"]),
{_, 304, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
{_, <<>>} = cow_http:parse_headers(Rest),
{error, timeout} = raw_recv(Client, 1, 1000),
ok.
same_content_length_as_get_in_head_response(Config) ->
doc("Responses to HEAD requests can include a content-length header. "
@ -1802,6 +1848,20 @@ no_content_length_in_204_response(Config) ->
false = lists:keyfind(<<"content-length">>, 1, Headers),
ok.
no_content_length_in_empty_304_response(Config) ->
doc("304 responses should not include a content-length header, "
"unless it matches the resource's and was therefore set "
"explicitly by the user. (RFC7230 3.3.1, RFC7230 3.3.2)"),
Client = raw_open(Config),
ok = raw_send(Client, [
"GET /resp/reply3/304 HTTP/1.1\r\n"
"Host: localhost\r\n"
"\r\n"]),
{_, 304, _, Rest} = cow_http:parse_status_line(raw_recv_head(Client)),
{Headers, <<>>} = cow_http:parse_headers(Rest),
false = lists:keyfind(<<"content-length">>, 1, Headers),
ok.
%%% @todo CONNECT no_content_length_in_2xx_response_to_connect_request(Config) ->
%no_transfer_encoding_in_100_response(Config) ->
%no_transfer_encoding_in_101_response(Config) ->