Skip to content

Commit 515745f

Browse files
committed
fix: store final redirect location in connection process
After following redirects, store the final URL in the connection state so it can be retrieved via hackney:location/1.
1 parent bee5ae0 commit 515745f

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/hackney.erl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ sync_request_with_redirect_body(ConnPid, Method, Path, HeadersList, FinalBody,
923923
{error, Reason}
924924
end.
925925

926-
follow_redirect(_ConnPid, Method, Body, WithBody, Options, CurrentURL, RespHeaders, Status,
926+
follow_redirect(ConnPid, Method, Body, WithBody, Options, CurrentURL, RespHeaders, Status,
927927
MaxRedirect, RedirectCount) ->
928928
%% Get the Location header
929929
Location = redirect_location(RespHeaders),
@@ -933,6 +933,8 @@ follow_redirect(_ConnPid, Method, Body, WithBody, Options, CurrentURL, RespHeade
933933
LocationBin ->
934934
%% Parse the new URL (could be relative or absolute)
935935
NewURL = resolve_redirect_url(CurrentURL, LocationBin),
936+
%% Get the full URL as a binary for storing
937+
FinalLocation = hackney_url:unparse_url(NewURL),
936938
%% Determine method for redirect (301, 302, 303 -> GET, 307, 308 -> same method)
937939
NewMethod = case Status of
938940
S when S =:= 301; S =:= 302; S =:= 303 ->
@@ -954,8 +956,12 @@ follow_redirect(_ConnPid, Method, Body, WithBody, Options, CurrentURL, RespHeade
954956
[{follow_redirect, true}, {max_redirect, MaxRedirect},
955957
{redirect_count, RedirectCount + 1}, {with_body, WithBody} | Options2]) of
956958
{ok, Status2, Headers2, Body2} ->
959+
%% Store the final location in the connection
960+
hackney_conn:set_location(ConnPid, FinalLocation),
957961
{ok, Status2, Headers2, Body2};
958962
{ok, Status2, Headers2} ->
963+
%% Store the final location in the connection
964+
hackney_conn:set_location(ConnPid, FinalLocation),
959965
{ok, Status2, Headers2};
960966
Error ->
961967
Error

0 commit comments

Comments
 (0)