Skip to content

Commit

Permalink
Merge pull request #725 from benoitc/feature/use-proxy-env
Browse files Browse the repository at this point in the history
get proxy info from environnment
  • Loading branch information
benoitc committed Sep 20, 2023
2 parents 745e795 + d98e376 commit cb913e6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
3 changes: 2 additions & 1 deletion include/hackney.hrl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-define(RECV_TIMEOUT, 5000).


-record(connection, {transport,
host,
port,
Expand Down Expand Up @@ -57,3 +56,5 @@
-define(CONFIG, hackney_config).

-define(CONNECTIONS, hackney_connections).

-define(PROXY_ENV_VARS, ["http_proxy", "https_proxy", "HTTP_PROXY", "HTTPS_PROXY"]).
52 changes: 37 additions & 15 deletions src/hackney.erl
Original file line number Diff line number Diff line change
Expand Up @@ -620,21 +620,7 @@ maybe_proxy(Transport, Host, Port, Options)
case proplists:get_value(proxy, Options) of
Url when is_binary(Url) orelse is_list(Url) ->
?report_debug("HTTP proxy request", [{url, Url}]),
Url1 = hackney_url:parse_url(Url),
#hackney_url{transport = PTransport,
host = ProxyHost,
port = ProxyPort} = hackney_url:normalize(Url1),
ProxyAuth = proplists:get_value(proxy_auth, Options),
case {Transport, PTransport} of
{hackney_ssl, hackney_ssl} -> {error, invalid_proxy_transport};
{hackney_ssl, _} ->
do_connect(ProxyHost, ProxyPort, ProxyAuth,Transport, Host, Port, Options);
_ ->
case hackney_connect:connect(Transport, ProxyHost,ProxyPort, Options, true) of
{ok, Ref} -> {ok, Ref, true};
Error -> Error
end
end;
proxy_from_url(Url, Transport, Host, Port, Options);
{ProxyHost, ProxyPort} ->
?report_debug("HTTP proxy request", [{proxy_host, ProxyHost}, {proxy_port, ProxyPort}]),
case Transport of
Expand Down Expand Up @@ -682,10 +668,46 @@ maybe_proxy(Transport, Host, Port, Options)
%% connect using a socks5 proxy
hackney_connect:connect(hackney_socks5, Host, Port, Options1, true);
_ ->
maybe_proxy_from_env(Transport, Host, Port, Options)
end.

maybe_proxy_from_env(Transport, Host, Port, Options) ->
case get_proxy_env() of
{ok, Url} ->
proxy_from_url(Url, Transport, Host, Port, Options);
false ->
?report_debug("request without proxy", []),
hackney_connect:connect(Transport, Host, Port, Options, true)
end.

proxy_from_url(Url, Transport, Host, Port, Options) ->
?report_debug("HTTP proxy request", [{url, Url}]),
Url1 = hackney_url:parse_url(Url),
#hackney_url{transport = PTransport,
host = ProxyHost,
port = ProxyPort} = hackney_url:normalize(Url1),
ProxyAuth = proplists:get_value(proxy_auth, Options),
case {Transport, PTransport} of
{hackney_ssl, hackney_ssl} -> {error, invalid_proxy_transport};
{hackney_ssl, _} ->
do_connect(ProxyHost, ProxyPort, ProxyAuth,Transport, Host, Port, Options);
_ ->
case hackney_connect:connect(Transport, ProxyHost,ProxyPort, Options, true) of
{ok, Ref} -> {ok, Ref, true};
Error -> Error
end
end.

get_proxy_env() ->
get_proxy_env(?PROXY_ENV_VARS).

get_proxy_env([Var | Rest]) ->
case os:getenv(Var) of
false -> get_proxy_env(Rest);
Url -> {ok, Url}
end;
get_proxy_env([]) ->
false.

do_connect(ProxyHost, ProxyPort, undefined, Transport, Host, Port, Options) ->
do_connect(ProxyHost, ProxyPort, {undefined, <<>>}, Transport, Host, Port, Options);
Expand Down

0 comments on commit cb913e6

Please sign in to comment.