diff --git a/lib/vagrant/downloaders/http.rb b/lib/vagrant/downloaders/http.rb index 4cbb949e8..f568fe2f9 100644 --- a/lib/vagrant/downloaders/http.rb +++ b/lib/vagrant/downloaders/http.rb @@ -16,8 +16,10 @@ module Vagrant end def download!(source_url, destination_file) - proxy_uri = URI.parse(ENV["http_proxy"] || "") + uri = URI.parse(source_url) + proxy_uri = resolve_proxy(uri) + http = Net::HTTP.new(uri.host, uri.port, proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password) if uri.scheme == "https" @@ -71,6 +73,19 @@ module Vagrant rescue SocketError raise Errors::DownloaderHTTPSocketError end + + private + + def resolve_proxy(source_uri) + proxy_string = nil + if ENV['no_proxy'] && ENV['no_proxy'].split(',').any? { |h| source_uri.host =~ /#{Regexp.quote(h.strip)}$/ } + proxy_string = '' + else + proxy_string = ENV["http_proxy"] || '' + end + URI.parse(proxy_string) + end + end end end diff --git a/test/unit/vagrant/downloaders/http_test.rb b/test/unit/vagrant/downloaders/http_test.rb index 6befb6acb..946a2214f 100644 --- a/test/unit/vagrant/downloaders/http_test.rb +++ b/test/unit/vagrant/downloaders/http_test.rb @@ -33,6 +33,16 @@ class HttpDownloaderTest < Test::Unit::TestCase @downloader.download!(@uri, @tempfile) end + should "create a proper net/http object without a proxy if no_proxy defined" do + @uri = "http://somewhere.direct.com/some_file" + @parsed_uri = URI.parse(@uri) + ENV["http_proxy"] = "http://user:foo@google.com" + ENV["no_proxy"] = "direct.com" + Net::HTTP.expects(:new).with(@parsed_uri.host, @parsed_uri.port, nil, nil, nil, nil).once.returns(@http) + @http.expects(:start) + @downloader.download!(@uri, @tempfile) + end + should "enable SSL if scheme is https" do @uri = "https://google.com/" @http.expects(:use_ssl=).with(true).once