From 8c60dd6c1b56aeef4493142adcbd8fb0ab6917ef Mon Sep 17 00:00:00 2001 From: Alexey Lebedeff Date: Tue, 28 Jul 2015 23:20:51 +0300 Subject: 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. --- src/cowboy_req.erl | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src') 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; -- cgit v1.2.3