From ed9bb449d1bfc837d46b2572e9ffc2a70b32f9de Mon Sep 17 00:00:00 2001 From: Xu Cheng Date: Wed, 13 Jul 2016 20:56:12 +0800 Subject: [PATCH] CurlDownloadStrategy#_fetch: do not alter variable `@url` (#504) In case of download failure and retry, altering `@url` can cause side effect like repeatedly applying `HOMEBREW_ARTIFACT_DOMAIN`. --- Library/Homebrew/download_strategy.rb | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index ec060cef02b31..4fa0b3803e937 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -330,23 +330,25 @@ def clear_cache # Private method, can be overridden if needed. def _fetch + url = @url + if ENV["HOMEBREW_ARTIFACT_DOMAIN"] - @url.sub!(%r{^((ht|f)tps?://)?}, ENV["HOMEBREW_ARTIFACT_DOMAIN"].chomp("/") + "/") - ohai "Downloading from #{@url}" + url.sub!(%r{^((ht|f)tps?://)?}, ENV["HOMEBREW_ARTIFACT_DOMAIN"].chomp("/") + "/") + ohai "Downloading from #{url}" end - urls = actual_urls + urls = actual_urls(url) unless urls.empty? ohai "Downloading from #{urls.last}" - if !ENV["HOMEBREW_NO_INSECURE_REDIRECT"].nil? && @url.start_with?("https://") && + if !ENV["HOMEBREW_NO_INSECURE_REDIRECT"].nil? && url.start_with?("https://") && urls.any? { |u| !u.start_with? "https://" } puts "HTTPS to HTTP redirect detected & HOMEBREW_NO_INSECURE_REDIRECT is set." - raise CurlDownloadStrategyError.new(@url) + raise CurlDownloadStrategyError.new(url) end - @url = urls.last + url = urls.last end - curl @url, "-C", downloaded_size, "-o", temporary_path + curl url, "-C", downloaded_size, "-o", temporary_path end # Curl options to be always passed to curl, @@ -357,11 +359,11 @@ def _curl_opts copts end - def actual_urls + def actual_urls(url) urls = [] - curl_args = _curl_opts << "-I" << "-L" << @url + curl_args = _curl_opts << "-I" << "-L" << url Utils.popen_read("curl", *curl_args).scan(/^Location: (.+)$/).map do |m| - urls << URI.join(urls.last || @url, m.first.chomp).to_s + urls << URI.join(urls.last || url, m.first.chomp).to_s end urls end