aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexey Lebedeff <[email protected]>2015-07-28 23:20:51 +0300
committerLoïc Hoguin <[email protected]>2015-08-07 15:30:09 +0200
commit8c60dd6c1b56aeef4493142adcbd8fb0ab6917ef (patch)
tree4181c522ac1f91491c92c13dfcc768c5fbc6b666
parent19a279dfdc923091616bcdd700e1a59e4c2404f4 (diff)
downloadcowboy-8c60dd6c1b56aeef4493142adcbd8fb0ab6917ef.tar.gz
cowboy-8c60dd6c1b56aeef4493142adcbd8fb0ab6917ef.tar.bz2
cowboy-8c60dd6c1b56aeef4493142adcbd8fb0ab6917ef.zip
Use 'Connection' header only when necessary
Fixes #839 when 'Connection: Keep-Alive' wasn't sent in a HTTP/1.0 response. Now the usage of 'Connection' header is consistent with current protocol version: when this header is not specified explicitly in the response, HTTP/1.0 implies 'Connection: close' and HTTP/1.1 implies 'Connection: Keep-Alive'. So if current 'Connection' value matches the default value of current protocol, we won't state obvious fact in the response; and vice versa. Amended to fix and improve tests, and revert the variable name change from HTTP11Headers to StdHeaders. I think it's still good to leave it as is because it's not really a standard header for HTTP/1.0, and it's gone from HTTP/2 entirely.
-rw-r--r--src/cowboy_req.erl13
-rw-r--r--test/http_SUITE.erl20
2 files changed, 24 insertions, 9 deletions
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl
index 27c59cf..fcc9744 100644
--- a/src/cowboy_req.erl
+++ b/src/cowboy_req.erl
@@ -820,7 +820,9 @@ reply(Status, Headers, Body, Req=#http_req{
resp_state=RespState, resp_headers=RespHeaders})
when RespState =:= waiting; RespState =:= waiting_stream ->
HTTP11Headers = if
- Transport =/= cowboy_spdy, Version =:= 'HTTP/1.1' ->
+ Transport =/= cowboy_spdy, Version =:= 'HTTP/1.0', Connection =:= keepalive ->
+ [{<<"connection">>, atom_to_connection(Connection)}];
+ Transport =/= cowboy_spdy, Version =:= 'HTTP/1.1', Connection =:= close ->
[{<<"connection">>, atom_to_connection(Connection)}];
true ->
[]
@@ -1134,13 +1136,20 @@ chunked_response(Status, Headers, Req=#http_req{
when RespState =:= waiting; RespState =:= waiting_stream ->
RespConn = response_connection(Headers, Connection),
HTTP11Headers = if
+ Version =:= 'HTTP/1.0', Connection =:= keepalive ->
+ [{<<"connection">>, atom_to_connection(Connection)}];
Version =:= 'HTTP/1.0' -> [];
true ->
MaybeTE = if
RespState =:= waiting_stream -> [];
true -> [{<<"transfer-encoding">>, <<"chunked">>}]
end,
- [{<<"connection">>, atom_to_connection(Connection)}|MaybeTE]
+ if
+ Connection =:= close ->
+ [{<<"connection">>, atom_to_connection(Connection)}|MaybeTE];
+ true ->
+ MaybeTE
+ end
end,
RespState2 = if
Version =:= 'HTTP/1.1', RespState =:= 'waiting' -> chunks;
diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl
index 0417ad0..af3a453 100644
--- a/test/http_SUITE.erl
+++ b/test/http_SUITE.erl
@@ -432,7 +432,10 @@ http10_keepalive_default(Config) ->
ok = raw_send(Client, Normal),
case catch raw_recv_head(Client) of
{'EXIT', _} -> error(closed);
- _ -> ok
+ Data ->
+ {'HTTP/1.0', 200, _, Rest} = cow_http:parse_status_line(Data),
+ {Headers, _} = cow_http:parse_headers(Rest),
+ false = lists:keymember(<<"connection">>, 1, Headers)
end,
ok = raw_send(Client, Normal),
case catch raw_recv_head(Client) of
@@ -446,7 +449,10 @@ http10_keepalive_forced(Config) ->
ok = raw_send(Client, Keepalive),
case catch raw_recv_head(Client) of
{'EXIT', _} -> error(closed);
- _ -> ok
+ Data ->
+ {'HTTP/1.0', 200, _, Rest} = cow_http:parse_status_line(Data),
+ {Headers, _} = cow_http:parse_headers(Rest),
+ {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
end,
ok = raw_send(Client, Keepalive),
case catch raw_recv_head(Client) of
@@ -461,7 +467,7 @@ keepalive_max(Config) ->
CloseRef = gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}]),
_ = [begin
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref, MRef),
- {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
+ false = lists:keymember(<<"connection">>, 1, Headers)
end || Ref <- Refs],
{response, nofin, 200, Headers} = gun:await(ConnPid, CloseRef, MRef),
{_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
@@ -476,7 +482,7 @@ keepalive_nl(Config) ->
end || _ <- lists:seq(1, 10)],
_ = [begin
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
- {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
+ false = lists:keymember(<<"connection">>, 1, Headers)
end || Ref <- Refs],
ok.
@@ -730,7 +736,7 @@ rest_keepalive(Config) ->
Refs = [gun:get(ConnPid, "/simple") || _ <- lists:seq(1, 10)],
_ = [begin
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref),
- {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers)
+ false = lists:keymember(<<"connection">>, 1, Headers)
end || Ref <- Refs],
ok.
@@ -744,9 +750,9 @@ rest_keepalive_post(Config) ->
} || _ <- lists:seq(1, 5)],
_ = [begin
{response, fin, 403, Headers1} = gun:await(ConnPid, Ref1),
- {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers1),
+ false = lists:keymember(<<"connection">>, 1, Headers1),
{response, fin, 303, Headers2} = gun:await(ConnPid, Ref2),
- {_, <<"keep-alive">>} = lists:keyfind(<<"connection">>, 1, Headers2)
+ false = lists:keymember(<<"connection">>, 1, Headers2)
end || {Ref1, Ref2} <- Refs],
ok.