Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get proxy info from environnment #725

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading