From fa2f17c7aee5ab11f8f1f55b4d58ef7bdd7d55b6 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Sat, 6 Jan 2018 12:12:43 -0800 Subject: [PATCH 1/5] Notify user when download host changes on redirect --- lib/vagrant/util/downloader.rb | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/vagrant/util/downloader.rb b/lib/vagrant/util/downloader.rb index 9e29469b2..b22145d9e 100644 --- a/lib/vagrant/util/downloader.rb +++ b/lib/vagrant/util/downloader.rb @@ -83,7 +83,7 @@ module Vagrant extra_subprocess_opts[:notify] = :stderr progress_data = "" - progress_regexp = /(\r(.+?))\r/ + progress_regexp = /^\r\s*(\d.+)\r$/m # Setup the proc that'll receive the real-time data from # the downloader. @@ -99,9 +99,28 @@ module Vagrant # we report new progress reports. Otherwise, just keep # accumulating. match = progress_regexp.match(progress_data) + break if !match - data = match[2] - progress_data.gsub!(match[1], "") + + # If the download has been redirected and we are no longer downloading + # from the original host, notify the user that the target host has + # changed from the source. + if progress_data.include?("Location") + location = progress_data.scan(/Location: (.+?)$/m).flatten.compact.last.to_s.strip + if !location.empty? + @logger.info("download redirected to #{location}") + location_uri = URI.parse(location) + source_uri = URI.parse(source) + if location_uri.host != source_uri.host + @ui.clear_line + @ui.detail "Download redirected to host: #{location_uri.host}" + end + end + end + + data = match[1].to_s + stop = progress_data.index(data) + data.length + progress_data.slice!(0, stop) # Ignore the first \r and split by whitespace to grab the columns columns = data.strip.split(/\s+/) @@ -273,7 +292,7 @@ module Vagrant "-q", "--fail", "--location", - "--max-redirs", "10", + "--max-redirs", "10", "--verbose", "--user-agent", USER_AGENT, ] From 5c9c10558ad9388d185a60d89dca49764209f0b7 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Sat, 6 Jan 2018 12:18:18 -0800 Subject: [PATCH 2/5] Update curl options in test --- test/unit/vagrant/util/downloader_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/vagrant/util/downloader_test.rb b/test/unit/vagrant/util/downloader_test.rb index 42af3ca53..772412fbc 100644 --- a/test/unit/vagrant/util/downloader_test.rb +++ b/test/unit/vagrant/util/downloader_test.rb @@ -23,7 +23,7 @@ describe Vagrant::Util::Downloader do describe "#download!" do let(:curl_options) { ["-q", "--fail", "--location", "--max-redirs", "10", - "--user-agent", described_class::USER_AGENT, + "--verbose", "--user-agent", described_class::USER_AGENT, "--output", destination, source, {}] } @@ -195,7 +195,7 @@ describe Vagrant::Util::Downloader do describe "#head" do let(:curl_options) { - ["-q", "--fail", "--location", "--max-redirs", "10", "--user-agent", described_class::USER_AGENT, source, {}] + ["-q", "--fail", "--location", "--max-redirs", "10", "--verbose", "--user-agent", described_class::USER_AGENT, source, {}] } it "returns the output" do From 5e7e56f5730e0430eba371b7cbbdb5bd4bec5bc4 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 12 Jan 2018 12:42:59 -0800 Subject: [PATCH 3/5] Fix redirect display and chosen location --- lib/vagrant/util/downloader.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/vagrant/util/downloader.rb b/lib/vagrant/util/downloader.rb index b22145d9e..eda284db0 100644 --- a/lib/vagrant/util/downloader.rb +++ b/lib/vagrant/util/downloader.rb @@ -25,6 +25,12 @@ module Vagrant :sha1 => Digest::SHA1 }.freeze + # Hosts that do not require notification on redirect + SILENCED_HOSTS = [ + "vagrantcloud.com".freeze, + "vagrantup.com".freeze + ].freeze + attr_reader :source attr_reader :destination @@ -106,16 +112,21 @@ module Vagrant # from the original host, notify the user that the target host has # changed from the source. if progress_data.include?("Location") - location = progress_data.scan(/Location: (.+?)$/m).flatten.compact.last.to_s.strip + location = progress_data.scan(/Location: (.+?)$/m).flatten.compact.first.to_s.strip if !location.empty? @logger.info("download redirected to #{location}") location_uri = URI.parse(location) source_uri = URI.parse(source) - if location_uri.host != source_uri.host + source_host = source_uri.host.split(".", 2).last + location_host = location_uri.host.split(".", 2).last + if !@redirect_notify && location_host != source_host && !SILENCED_HOSTS.include?(location_host) @ui.clear_line @ui.detail "Download redirected to host: #{location_uri.host}" end + @redirect_notify = true end + progress_data.replace("") + break end data = match[1].to_s From 04e05088ff73f1579a9012046ba98cfb89dc538e Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Wed, 21 Feb 2018 19:46:12 -0800 Subject: [PATCH 4/5] Check location first. Grab final progress when multiple entries listed. --- lib/vagrant/util/downloader.rb | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/vagrant/util/downloader.rb b/lib/vagrant/util/downloader.rb index eda284db0..b61103d6f 100644 --- a/lib/vagrant/util/downloader.rb +++ b/lib/vagrant/util/downloader.rb @@ -101,13 +101,6 @@ module Vagrant progress_data << data while true - # If we have a full amount of column data (two "\r") then - # we report new progress reports. Otherwise, just keep - # accumulating. - match = progress_regexp.match(progress_data) - - break if !match - # If the download has been redirected and we are no longer downloading # from the original host, notify the user that the target host has # changed from the source. @@ -129,9 +122,24 @@ module Vagrant break end - data = match[1].to_s - stop = progress_data.index(data) + data.length - progress_data.slice!(0, stop) + # If we have a full amount of column data (two "\r") then + # we report new progress reports. Otherwise, just keep + # accumulating. + match = nil + check_match = true + + while check_match + check_match = progress_regexp.match(progress_data) + if check_match + data = check_match[1].to_s + stop = progress_data.index(data) + data.length + progress_data.slice!(0, stop) + + match = check_match + end + end + + break if !match # Ignore the first \r and split by whitespace to grab the columns columns = data.strip.split(/\s+/) From ffa6d2810dc86da6f076222678bbbe381f82fe84 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Thu, 22 Feb 2018 09:46:24 -0800 Subject: [PATCH 5/5] Remove end of line matcher from progress regexp --- lib/vagrant/util/downloader.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vagrant/util/downloader.rb b/lib/vagrant/util/downloader.rb index b61103d6f..2812dc3ab 100644 --- a/lib/vagrant/util/downloader.rb +++ b/lib/vagrant/util/downloader.rb @@ -89,7 +89,7 @@ module Vagrant extra_subprocess_opts[:notify] = :stderr progress_data = "" - progress_regexp = /^\r\s*(\d.+)\r$/m + progress_regexp = /^\r\s*(\d.+?)\r/m # Setup the proc that'll receive the real-time data from # the downloader.