From dab80649b8606a3352a51e40ce8c08500b7d3ebe Mon Sep 17 00:00:00 2001 From: Ticean Bennett Date: Wed, 21 Sep 2011 22:58:50 -0700 Subject: [PATCH] Added Basic Auth support to HTTP downloader. --- lib/vagrant/downloaders/http.rb | 8 +++++++- test/unit/vagrant/downloaders/http_test.rb | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/vagrant/downloaders/http.rb b/lib/vagrant/downloaders/http.rb index 5b502b2c6..4cbb949e8 100644 --- a/lib/vagrant/downloaders/http.rb +++ b/lib/vagrant/downloaders/http.rb @@ -2,6 +2,7 @@ require 'net/http' require 'net/https' require 'open-uri' require 'uri' +require 'base64' module Vagrant module Downloaders @@ -27,7 +28,12 @@ module Vagrant http.start do |h| env.ui.info I18n.t("vagrant.downloaders.http.download", :url => source_url) - h.request_get(uri.request_uri) do |response| + headers = nil + if uri.user && uri.password + headers = {'Authorization' => 'Basic ' + Base64.encode64(uri.user + ':' + uri.password)} + end + + h.request_get(uri.request_uri, headers) do |response| if response.is_a?(Net::HTTPRedirection) # Follow the HTTP redirect. # TODO: Error on some redirect limit diff --git a/test/unit/vagrant/downloaders/http_test.rb b/test/unit/vagrant/downloaders/http_test.rb index 6d4ba3a98..6befb6acb 100644 --- a/test/unit/vagrant/downloaders/http_test.rb +++ b/test/unit/vagrant/downloaders/http_test.rb @@ -6,6 +6,7 @@ class HttpDownloaderTest < Test::Unit::TestCase @downloader.stubs(:report_progress) @downloader.stubs(:complete_progress) @uri = "http://google.com/" + @headers = nil end context "downloading" do @@ -48,7 +49,7 @@ class HttpDownloaderTest < Test::Unit::TestCase segment.stubs(:length).returns(7) @http.stubs(:start).yields(h) - h.expects(:request_get).with(@parsed_uri.request_uri).once.yields(response) + h.expects(:request_get).with(@parsed_uri.request_uri, @headers).once.yields(response) response.expects(:read_body).once.yields(segment) @tempfile.expects(:write).with(segment).once