From 9776b3735f86577e68418b3da40d7c3fe46eb662 Mon Sep 17 00:00:00 2001 From: ARAYN7011 <88572149+ARAYN7011@users.noreply.github.com> Date: Sun, 6 Dec 2026 10:08:22 +0530 Subject: [PATCH] fix: keep the connection after a clean 204/304 response A 204 or 304 always ended with the socket dropped, so every such response cost a new TCP/TLS connection. Only drop it when the response declares a body anyway (Transfer-Encoding, or a non-zero Content-Length on a 204). Fixes #1048 Co-Authored-By: Claude Sonnet 5.5 --- src/hackney_conn.erl | 40 ++++++++-- test/hackney_no_body_reuse_tests.erl | 111 +++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 6 deletions(-) create mode 100644 test/hackney_no_body_reuse_tests.erl diff --git a/src/hackney_conn.erl b/src/hackney_conn.erl index d705a5e5..11d23420 100644 --- a/src/hackney_conn.erl +++ b/src/hackney_conn.erl @@ -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)). @@ -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} -> diff --git a/test/hackney_no_body_reuse_tests.erl b/test/hackney_no_body_reuse_tests.erl new file mode 100644 index 00000000..fd681839 --- /dev/null +++ b/test/hackney_no_body_reuse_tests.erl @@ -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, <>); + _ -> closed + end; + _ -> ok + end.