Skip to content

Commit 91b7cd1

Browse files
committed
Update netloc on redirect
1 parent 5b51fb8 commit 91b7cd1

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

THANKS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ Richard Jones <[email protected]>
2828
Eduardo Gurgel <[email protected]>
2929
Mayorov Andrey <[email protected]>
3030
31-
Pavel Abalihin <[email protected]>
31+
Pavel Abalikhin <[email protected]>
3232
Ilya Khaprov <[email protected]>

src/hackney.erl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,9 +801,14 @@ redirect(Client0, {Method, NewLocation, Headers, Body}) ->
801801
Client1 = hackney_connect:check_or_close(Client),
802802

803803
%% update the state with the redirect info
804+
RedirectNetloc = hackney_url:netloc(
805+
hackney_url:transport_scheme(RedirectTransport),
806+
RedirectHost,
807+
RedirectPort),
804808
Client2 = Client1#client{transport=RedirectTransport,
805809
host=RedirectHost,
806810
port=RedirectPort,
811+
netloc=RedirectNetloc,
807812
options=Opts},
808813

809814
%% send a request to the new location

src/hackney_connect.erl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,8 @@ create_connection(Transport, Host, Port, Options) ->
4949

5050
create_connection(Transport, Host, Port, Options, Dynamic)
5151
when is_list(Options) ->
52-
Netloc = case {Transport, Port} of
53-
{hackney_tcp, 80} -> list_to_binary(Host);
54-
{hackney_ssl, 443} -> list_to_binary(Host);
55-
_ ->
56-
iolist_to_binary([Host, ":", integer_to_list(Port)])
57-
end,
52+
Netloc = hackney_url:netloc(hackney_url:transport_scheme(Transport),
53+
Host, Port),
5854
%% default timeout
5955
Timeout = proplists:get_value(recv_timeout, Options, ?RECV_TIMEOUT),
6056
FollowRedirect = proplists:get_value(follow_redirect, Options, false),

src/hackney_url.erl

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
-export([parse_url/1,
1515
transport_scheme/1,
16+
netloc/3,
1617
unparse_url/1,
1718
urldecode/1, urldecode/2,
1819
urlencode/1, urlencode/2,
@@ -94,14 +95,7 @@ normalize(#hackney_url{}=Url, Fun) when is_function(Fun, 1) ->
9495

9596
%% encode domain if needed
9697
Host2 = idna:to_ascii(Host1),
97-
Netloc1 = case {Scheme, Port} of
98-
{http, 80} -> list_to_binary(Host2);
99-
{https, 443} -> list_to_binary(Host2);
100-
{http_unix, _} -> list_to_binary(Host2);
101-
_ ->
102-
iolist_to_binary([Host2, ":", integer_to_list(Port)])
103-
end,
104-
{Host2, Netloc1}
98+
{Host2, netloc(Scheme, Host2, Port)}
10599
end,
106100
Path1 = Fun(Path),
107101
Url#hackney_url{host=Host, netloc=Netloc, path=Path1}.
@@ -113,6 +107,15 @@ transport_scheme(hackney_ssl) ->
113107
transport_scheme(hackney_local_tcp) ->
114108
http_unix.
115109

110+
netloc(http, Host, 80) ->
111+
list_to_binary(Host);
112+
netloc(https, Host, 443) ->
113+
list_to_binary(Host);
114+
netloc(http_unix, Host, _Port) ->
115+
list_to_binary(Host);
116+
netloc(_, Host, Port) ->
117+
iolist_to_binary([Host, ":", integer_to_list(Port)]).
118+
116119
unparse_url(#hackney_url{}=Url) ->
117120
#hackney_url{scheme = Scheme,
118121
netloc = Netloc,

0 commit comments

Comments
 (0)