@@ -394,23 +394,45 @@ defmodule Mix.Utils do
394
394
:ssl . start
395
395
:inets . start
396
396
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
+
397
401
headers = [ { 'user-agent' , 'Mix/#{ System . version } ' } ]
398
402
request = { :binary . bin_to_list ( path ) , headers }
399
403
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
+
400
408
# We are using relaxed: true because some clients (namely Github pages
401
409
# which we are using to download rebar) is returning a Location header
402
410
# with relative paths, which does not follow the spec. This would cause
403
411
# 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
405
413
{ :ok , { { _ , status , _ } , _ , body } } when status in 200 .. 299 ->
406
414
body
407
415
{ :ok , { { _ , status , _ } , _ , _ } } ->
408
416
Mix . raise "Could not access url #{ path } , got status: #{ status } "
409
417
{ :error , reason } ->
410
418
Mix . raise "Could not access url #{ path } , error: #{ inspect reason } "
411
419
end
420
+ :inets . stop ( :httpc , :mix )
412
421
end
413
422
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
+
414
436
defp file? ( path ) do
415
437
File . regular? ( path )
416
438
end
0 commit comments