From 40d8bb0b6982cef3de597e3b61d8c392a8a3938a Mon Sep 17 00:00:00 2001 From: Iain Date: Thu, 3 Dec 2026 21:29:41 +0000 Subject: [PATCH] fix: keep the path on a query-only redirect Location A Location like "?page=2" was merged as if it were a relative path, so from /dir/start the redirect went to /dir/?page=2. RFC 3986 5.2.2 keeps the base path when the reference has none, giving /dir/start?page=2. --- src/hackney.erl | 3 +++ test/hackney_redirect_tests.erl | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/hackney.erl b/src/hackney.erl index 50293976..1891f464 100644 --- a/src/hackney.erl +++ b/src/hackney.erl @@ -1710,6 +1710,9 @@ parse_path_qs(PathLike) -> end. %% @private Merge a relative path with a base path per RFC 3986 Section 5.2.3 +merge_paths(BasePath, <<>>) -> + %% Empty reference path ("?q=1") keeps the base path (RFC 3986 Section 5.2.2) + BasePath; merge_paths(BasePath, RelPath) when is_binary(BasePath), is_binary(RelPath) -> case BasePath of <<>> -> diff --git a/test/hackney_redirect_tests.erl b/test/hackney_redirect_tests.erl index f0d769ef..73611ce1 100644 --- a/test/hackney_redirect_tests.erl +++ b/test/hackney_redirect_tests.erl @@ -200,6 +200,8 @@ redirect_integration_test_() -> %% Issue #711: relative redirect without slash {"relative redirect without slash", fun test_relative_redirect_without_slash/0}, + {"query-only redirect keeps the path", + fun test_query_only_redirect/0}, %% Issue #693: redirect to non-standard port preserves Host header {"redirect preserves port in Host header", fun test_redirect_port_in_host/0}, @@ -238,6 +240,11 @@ test_relative_redirect_without_slash() -> io:format("Relative redirect result: status=~p~n", [Status]), ?assertEqual(200, Status). +test_query_only_redirect() -> + URL = url(<<"/redirect-to?url=", (hackney_url:urlencode(<<"?url=/get">>))/binary>>), + {ok, Status, _Headers, _Body} = hackney:request(get, URL, [], <<>>, [{follow_redirect, true}]), + ?assertEqual(200, Status). + test_redirect_port_in_host() -> %% This tests issue #693 %% When redirecting to a URL with non-standard port, Host header should include port