From 650ff4b734ea74b49f95950028eef3e5d4b66e92 Mon Sep 17 00:00:00 2001 From: Anko painting Date: Wed, 8 Sep 2010 20:59:22 -0700 Subject: [PATCH] Allow downloading via a proxy if http_proxy environment variable is set. [closes GH-157] --- CHANGELOG.md | 2 ++ lib/vagrant/downloaders/http.rb | 4 +++- test/vagrant/downloaders/http_test.rb | 12 +++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3c51085e..2f019a1f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## 0.6.0 (unreleased) + - If `http_proxy` environmental variable is set, it will be used as the proxy + box adding via http. - Remove `config.ssh.password`. It hasn't been used for a few versions now and was only kept around to avoid exceptions in Vagrantfiles. - Configuration is now validated so improper input can be found in diff --git a/lib/vagrant/downloaders/http.rb b/lib/vagrant/downloaders/http.rb index 212b8d881..e525bd370 100644 --- a/lib/vagrant/downloaders/http.rb +++ b/lib/vagrant/downloaders/http.rb @@ -15,8 +15,10 @@ module Vagrant end def download!(source_url, destination_file) + proxy_uri = URI.parse(ENV["http_proxy"] || "") uri = URI.parse(source_url) - http = Net::HTTP.new(uri.host, uri.port) + http = Net::HTTP.new(uri.host, uri.port, proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password) + if uri.scheme == "https" http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE diff --git a/test/vagrant/downloaders/http_test.rb b/test/vagrant/downloaders/http_test.rb index e2fab87f8..6bf6c8f63 100644 --- a/test/vagrant/downloaders/http_test.rb +++ b/test/vagrant/downloaders/http_test.rb @@ -10,6 +10,8 @@ class HttpDownloaderTest < Test::Unit::TestCase context "downloading" do setup do + ENV["http_proxy"] = nil + @parsed_uri = URI.parse(@uri) @http = Net::HTTP.new(@parsed_uri.host, @parsed_uri.port) Net::HTTP.stubs(:new).returns(@http) @@ -17,7 +19,15 @@ class HttpDownloaderTest < Test::Unit::TestCase end should "create a proper net/http object" do - Net::HTTP.expects(:new).with(@parsed_uri.host, @parsed_uri.port).once.returns(@http) + 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 "create a proper net/http object with a proxy" do + ENV["http_proxy"] = "http://user:foo@google.com" + @proxy = URI.parse(ENV["http_proxy"]) + Net::HTTP.expects(:new).with(@parsed_uri.host, @parsed_uri.port, @proxy.host, @proxy.port, @proxy.user, @proxy.password).once.returns(@http) @http.expects(:start) @downloader.download!(@uri, @tempfile) end