Skip to content

Commit

Permalink
Update netloc on redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
tnt-dev committed Oct 26, 2017
1 parent 5b51fb8 commit 765f675
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/hackney.erl
Original file line number Diff line number Diff line change
Expand Up @@ -801,9 +801,14 @@ redirect(Client0, {Method, NewLocation, Headers, Body}) ->
Client1 = hackney_connect:check_or_close(Client),

%% update the state with the redirect info
RedirectNetloc = hackney_url:netloc(
hackney_url:transport_scheme(RedirectTransport),
RedirectHost,
RedirectPort),
Client2 = Client1#client{transport=RedirectTransport,
host=RedirectHost,
port=RedirectPort,
netloc=RedirectNetloc,
options=Opts},

%% send a request to the new location
Expand Down
8 changes: 2 additions & 6 deletions src/hackney_connect.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ create_connection(Transport, Host, Port, Options) ->

create_connection(Transport, Host, Port, Options, Dynamic)
when is_list(Options) ->
Netloc = case {Transport, Port} of
{hackney_tcp, 80} -> list_to_binary(Host);
{hackney_ssl, 443} -> list_to_binary(Host);
_ ->
iolist_to_binary([Host, ":", integer_to_list(Port)])
end,
Netloc = hackney_url:netloc(hackney_url:transport_scheme(Transport),
Host, Port),
%% default timeout
Timeout = proplists:get_value(recv_timeout, Options, ?RECV_TIMEOUT),
FollowRedirect = proplists:get_value(follow_redirect, Options, false),
Expand Down
19 changes: 11 additions & 8 deletions src/hackney_url.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

-export([parse_url/1,
transport_scheme/1,
netloc/3,
unparse_url/1,
urldecode/1, urldecode/2,
urlencode/1, urlencode/2,
Expand Down Expand Up @@ -94,14 +95,7 @@ normalize(#hackney_url{}=Url, Fun) when is_function(Fun, 1) ->

%% encode domain if needed
Host2 = idna:to_ascii(Host1),
Netloc1 = case {Scheme, Port} of
{http, 80} -> list_to_binary(Host2);
{https, 443} -> list_to_binary(Host2);
{http_unix, _} -> list_to_binary(Host2);
_ ->
iolist_to_binary([Host2, ":", integer_to_list(Port)])
end,
{Host2, Netloc1}
{Host2, netloc(Scheme, Host2, Port)}
end,
Path1 = Fun(Path),
Url#hackney_url{host=Host, netloc=Netloc, path=Path1}.
Expand All @@ -113,6 +107,15 @@ transport_scheme(hackney_ssl) ->
transport_scheme(hackney_local_tcp) ->
http_unix.

netloc(http, Host, 80) ->
list_to_binary(Host);
netloc(https, Host, 443) ->
list_to_binary(Host);
netloc(http_unix, Host, _Port) ->
list_to_binary(Host);
netloc(_, Host, Port) ->
iolist_to_binary([Host, ":", integer_to_list(Port)]).

unparse_url(#hackney_url{}=Url) ->
#hackney_url{scheme = Scheme,
netloc = Netloc,
Expand Down

0 comments on commit 765f675

Please sign in to comment.