Skip to content
Open
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
33 changes: 30 additions & 3 deletions lib/elixir_make/downloader/httpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ defmodule ElixirMake.Downloader.Httpc do
{:ok, _} = Application.ensure_all_started(:public_key)

if proxy = proxy_env(:http) do
Mix.shell().info("Using HTTP_PROXY: #{proxy}")
Mix.shell().info("Using HTTP_PROXY: #{without_userinfo(proxy)}")
%{host: host, port: port} = URI.parse(proxy)

:httpc.set_options([{:proxy, {{String.to_charlist(host), port}, []}}])
end

if proxy = proxy_env(:https) do
Mix.shell().info("Using HTTPS_PROXY: #{proxy}")
Mix.shell().info("Using HTTPS_PROXY: #{without_userinfo(proxy)}")
%{host: host, port: port} = URI.parse(proxy)
:httpc.set_options([{:https_proxy, {{String.to_charlist(host), port}, []}}])
end
Expand All @@ -39,7 +39,7 @@ defmodule ElixirMake.Downloader.Httpc do

options = [body_format: :binary, max_header_size: 65_536]

case :httpc.request(:get, {url_charlist, []}, https_options, options) do
case :httpc.request(:get, {url_charlist, []}, https_options ++ proxy_auth(url), options) do
{:ok, {{_, 200, _}, _headers, body}} ->
{:ok, body}

Expand All @@ -48,6 +48,33 @@ defmodule ElixirMake.Downloader.Httpc do
end
end

# httpc doesn't read credentials from the proxy URL, so pass them as
# proxy_auth, as Mix.Utils.proxy_config/1 does.
defp proxy_auth(url) do
proxy =
case URI.parse(url) do
%URI{scheme: "https"} -> proxy_env(:https)
_ -> proxy_env(:http)
end

case proxy && URI.parse(proxy) do
%URI{userinfo: userinfo} when is_binary(userinfo) ->
destructure [user, pass], String.split(userinfo, ":", parts: 2)
[proxy_auth: {String.to_charlist(user), String.to_charlist(pass || "")}]

_ ->
[]
end
end

# Keeps proxy credentials out of build logs.
defp without_userinfo(proxy) do
case URI.parse(proxy) do
%URI{userinfo: nil} -> proxy
uri -> URI.to_string(%{uri | userinfo: nil})
end
end

defp cacerts_options do
cond do
path = System.get_env("HEX_CACERTS_PATH") ->
Expand Down