Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions src/hackney_conn.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3000,15 +3000,43 @@ recv_headers(#conn_data{parser = Parser} = Data, Headers) ->
{error, Reason}
end.

%% @private Finish a 204/304 response. These never have a body (RFC 7230
%% 3.3.3), so a clean response keeps its socket and the connection can be
%% reused. If the server declares a body anyway (Transfer-Encoding, or a
%% non-zero Content-Length on a 204; a 304 may carry the Content-Length of the
%% representation it stands for), the unread bytes would corrupt the next
%% response, so drop the socket.
no_body_status_done(#conn_data{status = Status, response_headers = Headers} = Data) ->
case declares_body(Status, Headers) of
true -> Data#conn_data{socket = undefined};
false -> Data
end.

declares_body(_Status, undefined) ->
true;
declares_body(Status, Headers) ->
case hackney_headers:get_value(<<"transfer-encoding">>, Headers, undefined) of
undefined when Status =:= 304 ->
false;
undefined ->
case hackney_headers:get_value(<<"content-length">>, Headers, undefined) of
undefined -> false;
<<"0">> -> false;
_ -> true
end;
_ ->
true
end.

%% @private Read full response body
read_full_body(#conn_data{method = <<"HEAD">>} = Data, Acc) ->
%% HEAD requests have no body
{ok, Acc, Data};
read_full_body(#conn_data{status = Status} = Data, Acc) when Status =:= 204; Status =:= 304 ->
%% 204 No Content and 304 Not Modified have no body per RFC 7230 3.3.3
%% Force connection close to avoid corrupting subsequent requests if
%% a misbehaving server sent Content-Length or body data
{ok, Acc, Data#conn_data{socket = undefined}};
%% Force connection close if the response declares a body anyway, to
%% avoid corrupting subsequent requests on a misbehaving server
{ok, Acc, no_body_status_done(Data)};
read_full_body(Data, Acc) ->
read_full_body(Data, [Acc], byte_size(Acc)).

Expand Down Expand Up @@ -3144,9 +3172,9 @@ stream_body_chunk(#conn_data{method = <<"HEAD">>} = Data) ->
{done, Data};
stream_body_chunk(#conn_data{status = Status} = Data) when Status =:= 204; Status =:= 304 ->
%% 204 No Content and 304 Not Modified have no body per RFC 7230 3.3.3
%% Force connection close to avoid corrupting subsequent requests if
%% a misbehaving server sent Content-Length or body data
{done, Data#conn_data{socket = undefined}};
%% Force connection close if the response declares a body anyway, to
%% avoid corrupting subsequent requests on a misbehaving server
{done, no_body_status_done(Data)};
stream_body_chunk(#conn_data{parser = Parser} = Data) ->
case hackney_http:execute(Parser) of
{more, NewParser, _Buffer} ->
Expand Down
111 changes: 111 additions & 0 deletions test/hackney_no_body_reuse_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
%%% 204 and 304 responses carry no body (RFC 7230 3.3.3). A clean one must leave
%%% the connection reusable; only a response that declares a body anyway forces
%%% a close, since the unread bytes would corrupt the next response.
-module(hackney_no_body_reuse_tests).

-include_lib("eunit/include/eunit.hrl").

no_body_reuse_test_() ->
{setup,
fun() -> _ = application:ensure_all_started(hackney), ok end,
fun(_) -> ok end,
[{"clean 204/304 responses reuse one connection",
{timeout, 30, fun clean_reuse/0}},
{"204 declaring a body closes the connection",
{timeout, 30, fun declared_body_closes/0}}]}.

clean_reuse() ->
Resp204 = <<"HTTP/1.1 204 No Content\r\n\r\n">>,
Resp304 = <<"HTTP/1.1 304 Not Modified\r\ncontent-length: 1234\r\n\r\n">>,
{Accepts, Server, Port} = start_server([Resp204, Resp304, Resp204]),
Pool = no_body_reuse_pool,
ok = hackney_pool:start_pool(Pool, [{pool_size, 1}]),
try
Url = url(Port),
lists:foreach(
fun(Status) ->
?assertMatch({ok, Status, _, <<>>},
hackney:request(get, Url, [], <<>>, [{pool, Pool}]))
end, [204, 304, 204]),
?assertEqual(1, count(Accepts))
after
_ = (try hackney_pool:stop_pool(Pool) catch _:_ -> ok end),
stop(Server)
end.

declared_body_closes() ->
Bad = <<"HTTP/1.1 204 No Content\r\ncontent-length: 5\r\n\r\nhello">>,
Ok = <<"HTTP/1.1 204 No Content\r\n\r\n">>,
{Accepts, Server, Port} = start_server([Bad, Ok]),
Pool = no_body_reuse_pool2,
ok = hackney_pool:start_pool(Pool, [{pool_size, 1}]),
try
Url = url(Port),
?assertMatch({ok, 204, _, <<>>}, hackney:request(get, Url, [], <<>>, [{pool, Pool}])),
?assertMatch({ok, 204, _, <<>>}, hackney:request(get, Url, [], <<>>, [{pool, Pool}])),
?assertEqual(2, count(Accepts))
after
_ = (try hackney_pool:stop_pool(Pool) catch _:_ -> ok end),
stop(Server)
end.

url(Port) ->
iolist_to_binary([<<"http://localhost:">>, integer_to_list(Port), <<"/">>]).

count(Agent) -> Agent ! {get, self()}, receive {count, N} -> N after 2000 -> error(timeout) end.

stop(Server) -> exit(Server, shutdown), ok.

%% Keep-alive plain TCP server: answers successive requests (across all
%% connections) with the scripted responses and counts accepted connections.
start_server(Responses) ->
{ok, L} = gen_tcp:listen(0, [binary, {active, false}, {reuseaddr, true}]),
{ok, Port} = inet:port(L),
Counter = spawn(fun() -> counter(0) end),
Script = spawn(fun() -> script(Responses) end),
Server = spawn(fun() -> accept(L, Counter, Script) end),
{Counter, Server, Port}.

counter(N) ->
receive
inc -> counter(N + 1);
{get, From} -> From ! {count, N}, counter(N)
end.

script(Rs) ->
receive
{next, From} ->
case Rs of
[R | T] -> From ! {resp, R}, script(T);
[] -> From ! {resp, <<"HTTP/1.1 500 X\r\ncontent-length: 0\r\n\r\n">>}, script([])
end
end.

accept(L, Counter, Script) ->
case gen_tcp:accept(L) of
{ok, S} ->
Counter ! inc,
Pid = spawn(fun() -> serve(S, Script) end),
_ = gen_tcp:controlling_process(S, Pid),
accept(L, Counter, Script);
_ -> ok
end.

serve(S, Script) ->
case recv_request(S, <<>>) of
ok ->
Script ! {next, self()},
receive {resp, R} -> gen_tcp:send(S, R) end,
serve(S, Script);
closed -> ok
end.

recv_request(S, Acc) ->
case binary:match(Acc, <<"\r\n\r\n">>) of
nomatch ->
case gen_tcp:recv(S, 0, 10000) of
{ok, D} -> recv_request(S, <<Acc/binary, D/binary>>);
_ -> closed
end;
_ -> ok
end.
Loading