Skip to content

Commit

Permalink
Merge pull request #734 from Juliusan/bugfix_env
Browse files Browse the repository at this point in the history
HTTP and HTTPS proxies might differ
  • Loading branch information
benoitc authored Feb 21, 2024
2 parents e26739f + b7993e8 commit 7ae3831
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion include/hackney.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@

-define(CONNECTIONS, hackney_connections).

-define(PROXY_ENV_VARS, ["http_proxy", "https_proxy", "HTTP_PROXY", "HTTPS_PROXY"]).
-define(HTTP_PROXY_ENV_VARS, ["http_proxy", "HTTP_PROXY", "all_proxy", "ALL_PROXY"]).
-define(HTTPS_PROXY_ENV_VARS, ["https_proxy", "HTTPS_PROXY", "all_proxy", "ALL_PROXY"]).
21 changes: 12 additions & 9 deletions src/hackney.erl
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ request(Method, #hackney_url{}=URL0, Headers0, Body, Options0) ->
host = Host,
port = Port,
user = User,
password = Password} = URL,
password = Password,
scheme = Scheme} = URL,

Options = case User of
<<>> ->
Expand All @@ -332,7 +333,7 @@ request(Method, #hackney_url{}=URL0, Headers0, Body, Options0) ->

Headers1 = hackney_headers_new:new(Headers0),

case maybe_proxy(Transport, Host, Port, Options) of
case maybe_proxy(Transport, Scheme, Host, Port, Options) of
{ok, Ref, AbsolutePath} ->
Request = make_request(
Method, URL, Headers1, Body, Options, AbsolutePath
Expand Down Expand Up @@ -615,7 +616,7 @@ make_request(Method, #hackney_url{}=URL, Headers, Body, _, _) ->
{Method, FinalPath, Headers1, Body}.


maybe_proxy(Transport, Host, Port, Options)
maybe_proxy(Transport, Scheme, Host, Port, Options)
when is_list(Host), is_integer(Port), is_list(Options) ->
case proplists:get_value(proxy, Options) of
Url when is_binary(Url) orelse is_list(Url) ->
Expand Down Expand Up @@ -671,14 +672,14 @@ maybe_proxy(Transport, Host, Port, Options)
NoProxyEnv = proplists:get_value(
no_proxy_env, Options, application:get_env(hackney, no_proxy_env, false)
),
maybe_proxy_from_env(Transport, Host, Port, Options, NoProxyEnv)
maybe_proxy_from_env(Transport, Scheme, Host, Port, Options, NoProxyEnv)
end.

maybe_proxy_from_env(Transport, Host, Port, Options, true) ->
maybe_proxy_from_env(Transport, _Scheme, Host, Port, Options, true) ->
?report_debug("request without proxy", []),
hackney_connect:connect(Transport, Host, Port, Options, true);
maybe_proxy_from_env(Transport, Host, Port, Options, _) ->
case get_proxy_env() of
maybe_proxy_from_env(Transport, Scheme, Host, Port, Options, _) ->
case get_proxy_env(Scheme) of
{ok, Url} ->
proxy_from_url(Url, Transport, Host, Port, Options);
false ->
Expand All @@ -704,8 +705,10 @@ proxy_from_url(Url, Transport, Host, Port, Options) ->
end
end.

get_proxy_env() ->
get_proxy_env(?PROXY_ENV_VARS).
get_proxy_env(https) ->
get_proxy_env(?HTTPS_PROXY_ENV_VARS);
get_proxy_env(S) when S =:= http; S =:= http_unix ->
get_proxy_env(?HTTP_PROXY_ENV_VARS);

get_proxy_env([Var | Rest]) ->
case os:getenv(Var) of
Expand Down

0 comments on commit 7ae3831

Please sign in to comment.