mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Support ad-hoc keep-alive for HTTP/1.0 Clients
Only go for keep-alive if they submit a 'connection: keep-alive' header in the request, keep behaviour the same otherwise. The new RFC 7230 (http://tools.ietf.org/html/rfc7230#section-6.3) states: If the received protocol is HTTP/1.0, the "keep-alive" connection option is present, the recipient is not a proxy, and the recipient wishes to honor the HTTP/1.0 "keep-alive" mechanism, the connection will persist after the current response; Even though clients are discouraged from doing so in Appendix A.1.2 (http://tools.ietf.org/html/rfc7230#appendix-A.1.2)
This commit is contained in:
parent
9980348a1c
commit
b10b34a8f2
2 changed files with 33 additions and 2 deletions
|
@ -174,13 +174,16 @@ new(Socket, Transport, Peer, Method, Path, Query,
|
||||||
method=Method, path=Path, qs=Query, version=Version,
|
method=Method, path=Path, qs=Query, version=Version,
|
||||||
headers=Headers, host=Host, port=Port, buffer=Buffer,
|
headers=Headers, host=Host, port=Port, buffer=Buffer,
|
||||||
resp_compress=Compress, onresponse=OnResponse},
|
resp_compress=Compress, onresponse=OnResponse},
|
||||||
case CanKeepalive and (Version =:= 'HTTP/1.1') of
|
case CanKeepalive of
|
||||||
false ->
|
false ->
|
||||||
Req#http_req{connection=close};
|
Req#http_req{connection=close};
|
||||||
true ->
|
true ->
|
||||||
case lists:keyfind(<<"connection">>, 1, Headers) of
|
case lists:keyfind(<<"connection">>, 1, Headers) of
|
||||||
false ->
|
false ->
|
||||||
Req; %% keepalive
|
case Version of
|
||||||
|
'HTTP/1.1' -> Req; %% keepalive
|
||||||
|
'HTTP/1.0' -> Req#http_req{connection=close}
|
||||||
|
end;
|
||||||
{_, ConnectionHeader} ->
|
{_, ConnectionHeader} ->
|
||||||
Tokens = cow_http_hd:parse_connection(ConnectionHeader),
|
Tokens = cow_http_hd:parse_connection(ConnectionHeader),
|
||||||
Connection = connection_to_atom(Tokens),
|
Connection = connection_to_atom(Tokens),
|
||||||
|
|
|
@ -426,6 +426,34 @@ http10_hostless(Config) ->
|
||||||
[{port, Port10}|Config]),
|
[{port, Port10}|Config]),
|
||||||
cowboy:stop_listener(http10_hostless).
|
cowboy:stop_listener(http10_hostless).
|
||||||
|
|
||||||
|
http10_keepalive_default(Config) ->
|
||||||
|
Normal = "GET / HTTP/1.0\r\nhost: localhost\r\n\r\n",
|
||||||
|
Client = raw_open(Config),
|
||||||
|
ok = raw_send(Client, Normal),
|
||||||
|
case catch raw_recv_head(Client) of
|
||||||
|
{'EXIT', _} -> error(closed);
|
||||||
|
_ -> ok
|
||||||
|
end,
|
||||||
|
ok = raw_send(Client, Normal),
|
||||||
|
case catch raw_recv_head(Client) of
|
||||||
|
{'EXIT', _} -> closed;
|
||||||
|
_ -> error(not_closed)
|
||||||
|
end.
|
||||||
|
|
||||||
|
http10_keepalive_forced(Config) ->
|
||||||
|
Keepalive = "GET / HTTP/1.0\r\nhost: localhost\r\nConnection: keep-alive\r\n\r\n",
|
||||||
|
Client = raw_open(Config),
|
||||||
|
ok = raw_send(Client, Keepalive),
|
||||||
|
case catch raw_recv_head(Client) of
|
||||||
|
{'EXIT', _} -> error(closed);
|
||||||
|
_ -> ok
|
||||||
|
end,
|
||||||
|
ok = raw_send(Client, Keepalive),
|
||||||
|
case catch raw_recv_head(Client) of
|
||||||
|
{'EXIT', Err} -> error({closed, Err});
|
||||||
|
_ -> ok
|
||||||
|
end.
|
||||||
|
|
||||||
keepalive_max(Config) ->
|
keepalive_max(Config) ->
|
||||||
{ConnPid, MRef} = gun_monitor_open(Config),
|
{ConnPid, MRef} = gun_monitor_open(Config),
|
||||||
Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}])
|
Refs = [gun:get(ConnPid, "/", [{<<"connection">>, <<"keep-alive">>}])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue