From c4df3e60ecd75dcb9fd2bc4caaa772ddc0b742e0 Mon Sep 17 00:00:00 2001 From: Jianbo He Date: Wed, 21 Oct 2026 15:58:43 +0800 Subject: [PATCH 1/3] fix: complete HTTP responses ending with trailers Treat response trailers as the end of a stream so synchronous and asynchronous requests complete and pooled connections remain reusable. --- changelog.md | 5 +++++ src/ehttpc.erl | 5 +++++ test/ehttpc_server.erl | 6 ++++-- test/ehttpc_tests.erl | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 047f01c..9e9502a 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,10 @@ # ehttpc changes +## Unreleased + +- Complete chunked responses that end with trailers, preserving the response status, + headers, and body for synchronous and asynchronous requests. + ## 0.7.8 - Made it possible to call `ehttpc:health_check` with a timeout of `infinity`. diff --git a/src/ehttpc.erl b/src/ehttpc.erl index 948ac6d..1c5534e 100644 --- a/src/ehttpc.erl +++ b/src/ehttpc.erl @@ -380,6 +380,11 @@ do_handle_info( #state{client = Client} = State ) -> handle_gun_reply(State, Client, StreamRef, IsFin, ?undef, ?undef, Data); +do_handle_info( + {gun_trailers, Client, StreamRef, _Trailers}, + #state{client = Client} = State +) -> + handle_gun_reply(State, Client, StreamRef, fin, ?undef, ?undef, <<>>); do_handle_info( {gun_error, Client, StreamRef, Reason}, State = #state{client = Client, requests = Requests} diff --git a/test/ehttpc_server.erl b/test/ehttpc_server.erl index f46b56e..993f81f 100644 --- a/test/ehttpc_server.erl +++ b/test/ehttpc_server.erl @@ -277,8 +277,10 @@ make_response(Opts, IsHead) -> }, Resp. -make_chunks(#{chunked := #{chunk_size := Size, chunks := Count}}) -> +make_chunks(#{chunked := #{chunk_size := Size, chunks := Count} = Chunked}) -> %% we use the counts to generat bytes, (to verify the final number of chunks received) Count > 255 andalso error({bad_chunk_count, Count}), RawL = [iolist_to_binary(lists:duplicate(Size, I)) || I <- lists:seq(1, Count)], - [cow_http_te:chunk(I) || I <- RawL] ++ [cow_http_te:last_chunk()]. + Trailers = maps:to_list(maps:get(trailers, Chunked, #{})), + Last = ["0\r\n", [[K, ": ", V, "\r\n"] || {K, V} <- Trailers], "\r\n"], + [cow_http_te:chunk(I) || I <- RawL] ++ [Last]. diff --git a/test/ehttpc_tests.erl b/test/ehttpc_tests.erl index 141594e..84099cb 100644 --- a/test/ehttpc_tests.erl +++ b/test/ehttpc_tests.erl @@ -770,6 +770,39 @@ head_request_test() -> {ok, _, _} = ehttpc:request(?POOL, head, <<"/">>) ). +chunked_trailers_test_() -> + [ + {integer_to_list(Count), fun() -> chunked_trailers(Count) end} + || Count <- [0, 2] + ]. + +chunked_trailers(Count) -> + Port = ?PORT, + ServerOpts = #{ + port => Port, + name => chunked_trailers, + chunked => #{chunk_size => 3, chunks => Count, trailers => #{<<"x-check">> => <<"ok">>}} + }, + ?WITH( + ServerOpts, + pool_opts(Port, false), + begin + Worker = ehttpc_pool:pick_worker(?POOL), + Expected = iolist_to_binary([binary:copy(<>, 3) || I <- lists:seq(1, Count)]), + ?assertMatch({ok, 200, _, Expected}, ehttpc:request(Worker, get, req(), 500, 0)), + #{client := Client} = ehttpc:get_state(Worker, normal), + Tester = self(), + Callback = {fun(Result) -> Tester ! {trailers_result, Result} end, []}, + ok = ehttpc:request_async(Worker, get, req(), 500, Callback), + receive + {trailers_result, Result} -> ?assertMatch({ok, 200, _, Expected}, Result) + after 1_000 -> + error(trailers_callback_timeout) + end, + ?assertMatch(#{client := Client}, ehttpc:get_state(Worker, normal)) + end + ). + data_chunked_test() -> Port = ?PORT, Host = "127.0.0.1", From f8ae076e3ca5cac62b7f15993c43633bab0fc4d1 Mon Sep 17 00:00:00 2001 From: Jianbo He Date: Thu, 22 Oct 2026 13:42:59 +0800 Subject: [PATCH 2/3] test: cover trailers lifecycle and update app version Exercise pipelined, expired, and cancelled requests against a controlled HTTP server. Align the application version and changelog with the next patch release. --- changelog.md | 3 +- src/ehttpc.app.src | 2 +- test/ehttpc_server.erl | 15 +++--- test/ehttpc_tests.erl | 101 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 9 deletions(-) diff --git a/changelog.md b/changelog.md index 9e9502a..35a9911 100644 --- a/changelog.md +++ b/changelog.md @@ -1,9 +1,10 @@ # ehttpc changes -## Unreleased +## 0.7.9 - Complete chunked responses that end with trailers, preserving the response status, headers, and body for synchronous and asynchronous requests. + Trailer fields are not merged into the response headers. ## 0.7.8 diff --git a/src/ehttpc.app.src b/src/ehttpc.app.src index baf43e0..4996fd3 100644 --- a/src/ehttpc.app.src +++ b/src/ehttpc.app.src @@ -1,6 +1,6 @@ {application, ehttpc, [ {description, "HTTP Client for Erlang/OTP"}, - {vsn, "0.7.7"}, + {vsn, "0.7.9"}, {registered, []}, {applications, [ kernel, diff --git a/test/ehttpc_server.erl b/test/ehttpc_server.erl index 993f81f..f27ec6d 100644 --- a/test/ehttpc_server.erl +++ b/test/ehttpc_server.erl @@ -214,9 +214,10 @@ socket_send( _ -> 0 end, + BeforeFin = maps:get(before_fin, maps:get(chunked, Opts, #{}), fun() -> ok end), case gen_tcp:send(Socket, Headers) of ok -> - case socket_send_body_chunks(Socket, BodyChunks, ChunkedDelay, FinDelay) of + case socket_send_body_chunks(Socket, BodyChunks, ChunkedDelay, FinDelay, BeforeFin) of ok -> ok; {error, Reason} -> @@ -226,21 +227,21 @@ socket_send( {error, Reason} end. -socket_send_body_chunks(_Socket, [], _, _) -> +socket_send_body_chunks(_Socket, [], _, _, _) -> ok; -socket_send_body_chunks(Socket, [H | T], Delay, FinDelay) -> +socket_send_body_chunks(Socket, [H | T], Delay, FinDelay, BeforeFin) -> case gen_tcp:send(Socket, H) of ok -> %% maybe delya each chunk (when Delay > 0) timer:sleep(Delay), case T of - [_] when FinDelay > 0 -> - %% delay the last chunk - timer:sleep(FinDelay); + [_] -> + BeforeFin(), + FinDelay > 0 andalso timer:sleep(FinDelay); _ -> ok end, - socket_send_body_chunks(Socket, T, Delay, FinDelay); + socket_send_body_chunks(Socket, T, Delay, FinDelay, BeforeFin); {error, Reason} -> {error, Reason} end. diff --git a/test/ehttpc_tests.erl b/test/ehttpc_tests.erl index 84099cb..662d41a 100644 --- a/test/ehttpc_tests.erl +++ b/test/ehttpc_tests.erl @@ -803,6 +803,107 @@ chunked_trailers(Count) -> end ). +chunked_trailers_pipeline_test() -> + with_controlled_trailers(fun(Worker) -> + ok = request_trailers_async(Worker, first), + Server = await_trailers(), + ok = request_trailers_async(Worker, second), + ?assertMatch(#{requests := #{sent := 2}}, ehttpc:get_state(Worker)), + #{client := Client} = ehttpc:get_state(Worker), + Server ! send_trailers, + assert_trailers_result(first), + Server = await_trailers(), + Server ! send_trailers, + assert_trailers_result(second), + ?assertMatch(#{client := Client, requests := #{sent := 0}}, ehttpc:get_state(Worker)) + end). + +chunked_trailers_after_timeout_test() -> + with_controlled_trailers(fun(Worker) -> + ?assertEqual({error, timeout}, ehttpc:request(Worker, get, req(), 200, 0)), + Server = await_trailers(), + #{client := Client} = ehttpc:get_state(Worker), + ok = request_trailers_async(Worker, next), + ?assertMatch(#{requests := #{sent := 2}}, ehttpc:get_state(Worker)), + Server ! send_trailers, + Server = await_trailers(), + Server ! send_trailers, + assert_trailers_result(next), + ?assertMatch(#{client := Client, requests := #{sent := 0}}, ehttpc:get_state(Worker)) + end). + +chunked_trailers_after_cancel_test() -> + with_controlled_trailers(fun(Worker) -> + Tester = self(), + ok = ehttpc:request_async(Worker, get, req(), 1_000, #{ + final_reply => {fun(Result) -> Tester ! {cancelled_result, Result} end, []}, + stream_ref => {fun(Ref, W) -> Tester ! {trailers_stream, Ref, W} end, []} + }), + Ref = + receive + {trailers_stream, R, Worker} -> R + after 1_000 -> error(no_stream_ref) + end, + Server = await_trailers(), + ok = ehttpc:cancel(Worker, Ref), + ?assertMatch(#{requests := #{sent := 0}}, ehttpc:get_state(Worker)), + Server ! send_trailers, + ok = request_trailers_async(Worker, next), + Server = await_trailers(), + Server ! send_trailers, + assert_trailers_result(next), + receive + {cancelled_result, _} -> error(callback_after_cancel) + after 100 -> ok + end + end). + +with_controlled_trailers(Fun) -> + Tester = self(), + BeforeFin = fun() -> + Tester ! {trailers_ready, self()}, + receive + send_trailers -> ok + after 2_000 -> error(trailers_not_released) + end + end, + Port = ?PORT, + ServerOpts = #{ + port => Port, + name => controlled_trailers, + chunked => #{ + chunk_size => 1, + chunks => 1, + trailers => #{<<"x-check">> => <<"ok">>}, + before_fin => BeforeFin + } + }, + ?WITH(ServerOpts, pool_opts(Port, 2), Fun(ehttpc_pool:pick_worker(?POOL))). + +await_trailers() -> + receive + {trailers_ready, Server} -> Server + after 1_000 -> error(no_trailers) + end. + +request_trailers_async(Worker, Tag) -> + Tester = self(), + ehttpc:request_async( + Worker, + get, + req(), + 1_000, + { + fun(Result) -> Tester ! {Tag, Result} end, [] + } + ). + +assert_trailers_result(Tag) -> + receive + {Tag, Result} -> ?assertMatch({ok, 200, _, <<1>>}, Result) + after 1_000 -> error(trailers_callback_timeout) + end. + data_chunked_test() -> Port = ?PORT, Host = "127.0.0.1", From 91f47c60b10811fc220627f83c6c3338b72f1c15 Mon Sep 17 00:00:00 2001 From: Jianbo He Date: Thu, 22 Oct 2026 13:51:44 +0800 Subject: [PATCH 3/3] ci: stabilize HTTP test listeners and pin Ubuntu 24.04 Keep OTP 25 available after the ubuntu-latest image migration. Move test listeners outside the default ephemeral port range and report bind failures without a startup timeout. --- .github/workflows/erlang.yml | 2 +- test/ehttpc_pool_tests.erl | 2 +- test/ehttpc_server.erl | 4 +++- test/ehttpc_tests.erl | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/erlang.yml b/.github/workflows/erlang.yml index c01c144..b4500be 100644 --- a/.github/workflows/erlang.yml +++ b/.github/workflows/erlang.yml @@ -10,7 +10,7 @@ jobs: build: - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 strategy: fail-fast: false diff --git a/test/ehttpc_pool_tests.erl b/test/ehttpc_pool_tests.erl index 654d84e..dcc32ca 100644 --- a/test/ehttpc_pool_tests.erl +++ b/test/ehttpc_pool_tests.erl @@ -19,7 +19,7 @@ -include_lib("eunit/include/eunit.hrl"). -include_lib("snabbkaffe/include/snabbkaffe.hrl"). --define(PORT, (60000 + ?LINE)). +-define(PORT, (62000 + ?LINE)). kill_recover_test_() -> { diff --git a/test/ehttpc_server.erl b/test/ehttpc_server.erl index f27ec6d..706c954 100644 --- a/test/ehttpc_server.erl +++ b/test/ehttpc_server.erl @@ -81,7 +81,9 @@ listen(Port) -> end), receive {ok, LSocket} -> - {ok, LSocket} + {ok, LSocket}; + {error, Reason} -> + {error, Reason} end. loop(Socket, Opts) -> diff --git a/test/ehttpc_tests.erl b/test/ehttpc_tests.erl index 662d41a..e6967ab 100644 --- a/test/ehttpc_tests.erl +++ b/test/ehttpc_tests.erl @@ -20,7 +20,7 @@ -include_lib("snabbkaffe/include/snabbkaffe.hrl"). -define(POOL, <<"testpool">>). --define(PORT, (60000 + ?LINE)). +-define(PORT, (62000 + ?LINE)). -define(WITH_SERVER(Opts, Expr), with_server(Opts, fun() -> Expr end)