From 360051e032dd1604c50adf1c684b0ceea6c56b2d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 5 Mar 2013 00:33:56 -0800 Subject: [PATCH] Greatly improve http_proxy/https_proxy detection for box downloads --- lib/vagrant/downloaders/http.rb | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/vagrant/downloaders/http.rb b/lib/vagrant/downloaders/http.rb index a83bef967..387f263d9 100644 --- a/lib/vagrant/downloaders/http.rb +++ b/lib/vagrant/downloaders/http.rb @@ -82,19 +82,34 @@ module Vagrant # This method respects the "http_proxy" and "no_proxy" environmental # variables so that HTTP proxies can properly be used with Vagrant. def resolve_proxy(source_uri) - proxy_string = ENV["http_proxy"] || "" - if !proxy_string.empty? && ENV.has_key?("no_proxy") - # Respect the "no_proxy" environmental variable which contains a list - # of hosts that a proxy should not be used for. - ENV["no_proxy"].split(",").each do |host| - if source_uri.host =~ /#{Regexp.quote(host.strip)}$/ - proxy_string = "" - break + # Get the proper proxy key depending on the scheme of the box URL + proxy_key = "#{source_uri.scheme}_proxy".downcase + proxy_string = ENV[proxy_key] || ENV[proxy_key.upcase] || "" + + if !proxy_string.empty? + # Make sure the proxy string starts with a protocol so that + # URI.parse works properly below. + proxy_string = "http://#{proxy_string}" if !proxy_string.include?("://") + + if ENV.has_key?("no_proxy") + # Respect the "no_proxy" environmental variable which contains a list + # of hosts that a proxy should not be used for. + ENV["no_proxy"].split(",").each do |host| + if source_uri.host =~ /#{Regexp.quote(host.strip)}$/ + proxy_string = "" + break + end end end end - URI.parse(proxy_string) + begin + URI.parse(proxy_string) + rescue URI::InvalidURIError + # If we have an invalid URI, we assume the proxy is invalid, + # so we don't use a proxy. + URI.parse("") + end end end end