Adding support for 'no_proxy' environment variable to suppress proxy in http downloader

This commit is contained in:
Darrin Wortlehock 2011-09-16 01:12:56 +01:00 committed by Mitchell Hashimoto
parent c2ef1329aa
commit 4c01a87ab3
2 changed files with 26 additions and 1 deletions

View File

@ -16,8 +16,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)
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) 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"
@ -71,6 +73,19 @@ module Vagrant
rescue SocketError rescue SocketError
raise Errors::DownloaderHTTPSocketError raise Errors::DownloaderHTTPSocketError
end 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 end
end end

View File

@ -33,6 +33,16 @@ class HttpDownloaderTest < Test::Unit::TestCase
@downloader.download!(@uri, @tempfile) @downloader.download!(@uri, @tempfile)
end 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 should "enable SSL if scheme is https" do
@uri = "https://google.com/" @uri = "https://google.com/"
@http.expects(:use_ssl=).with(true).once @http.expects(:use_ssl=).with(true).once