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