Skip to content

Commit 892f211

Browse files
author
José Valim
committed
Merge pull request #2368 from bkochendorfer/add_proxy_support
Add http and https proxy options
2 parents 1aeb7ae + 2caa16a commit 892f211

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

lib/mix/lib/mix/utils.ex

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,23 +394,45 @@ defmodule Mix.Utils do
394394
:ssl.start
395395
:inets.start
396396

397+
# Starting a http client profile allows us to scope
398+
# the effects of using a http proxy to this function
399+
{:ok, pid} = :inets.start(:httpc, [{:profile, :mix}])
400+
397401
headers = [{'user-agent', 'Mix/#{System.version}'}]
398402
request = {:binary.bin_to_list(path), headers}
399403

404+
# If a proxy environment variable was supplied add a proxy to httpc
405+
if http_proxy = System.get_env("HTTP_PROXY"), do: proxy(http_proxy)
406+
if https_proxy = System.get_env("HTTPS_PROXY"), do: proxy(https_proxy)
407+
400408
# We are using relaxed: true because some clients (namely Github pages
401409
# which we are using to download rebar) is returning a Location header
402410
# with relative paths, which does not follow the spec. This would cause
403411
# the request to fail with {:error, :no_scheme} unless :relaxed is given.
404-
case :httpc.request(:get, request, [relaxed: true], [body_format: :binary]) do
412+
case :httpc.request(:get, request, [relaxed: true], [body_format: :binary], :mix) do
405413
{:ok, {{_, status, _}, _, body}} when status in 200..299 ->
406414
body
407415
{:ok, {{_, status, _}, _, _}} ->
408416
Mix.raise "Could not access url #{path}, got status: #{status}"
409417
{:error, reason} ->
410418
Mix.raise "Could not access url #{path}, error: #{inspect reason}"
411419
end
420+
:inets.stop(:httpc, :mix)
412421
end
413422

423+
defp proxy(proxy) do
424+
uri = URI.parse(proxy)
425+
:httpc.set_options([{ proxy_scheme(uri.scheme),
426+
{ { uri.host |> String.to_char_list, uri.port }, [] } }], :mix)
427+
end
428+
429+
defp proxy_scheme(scheme) do
430+
case scheme do
431+
"http" -> :proxy
432+
"https" -> :https_proxy
433+
end
434+
end
435+
414436
defp file?(path) do
415437
File.regular?(path)
416438
end

0 commit comments

Comments
 (0)