Allow downloading via a proxy if http_proxy environment variable is set. [closes GH-157]

This commit is contained in:
Anko painting 2010-09-08 20:59:22 -07:00 committed by Mitchell Hashimoto
parent 65eaca09bc
commit 650ff4b734
3 changed files with 16 additions and 2 deletions

View File

@ -1,5 +1,7 @@
## 0.6.0 (unreleased) ## 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 - Remove `config.ssh.password`. It hasn't been used for a few versions
now and was only kept around to avoid exceptions in Vagrantfiles. now and was only kept around to avoid exceptions in Vagrantfiles.
- Configuration is now validated so improper input can be found in - Configuration is now validated so improper input can be found in

View File

@ -15,8 +15,10 @@ module Vagrant
end end
def download!(source_url, destination_file) def download!(source_url, destination_file)
proxy_uri = URI.parse(ENV["http_proxy"] || "")
uri = URI.parse(source_url) 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" if uri.scheme == "https"
http.use_ssl = true http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.verify_mode = OpenSSL::SSL::VERIFY_NONE

View File

@ -10,6 +10,8 @@ class HttpDownloaderTest < Test::Unit::TestCase
context "downloading" do context "downloading" do
setup do setup do
ENV["http_proxy"] = nil
@parsed_uri = URI.parse(@uri) @parsed_uri = URI.parse(@uri)
@http = Net::HTTP.new(@parsed_uri.host, @parsed_uri.port) @http = Net::HTTP.new(@parsed_uri.host, @parsed_uri.port)
Net::HTTP.stubs(:new).returns(@http) Net::HTTP.stubs(:new).returns(@http)
@ -17,7 +19,15 @@ class HttpDownloaderTest < Test::Unit::TestCase
end end
should "create a proper net/http object" do 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) @http.expects(:start)
@downloader.download!(@uri, @tempfile) @downloader.download!(@uri, @tempfile)
end end