diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 79623ff2bf..58a79b181f 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -470,25 +470,29 @@ class CurlDownloadStrategy < AbstractFileDownloadStrategy ohai "Downloading #{url}" - use_cached_location = cached_location.exist? + cached_location_valid = cached_location.exist? v = version - use_cached_location = false if v.is_a?(Cask::DSL::Version) && v.latest? + cached_location_valid = false if v.is_a?(Cask::DSL::Version) && v.latest? - resolved_url, _, last_modified, _, is_redirection = begin + resolved_url, _, last_modified, file_size, is_redirection = begin resolve_url_basename_time_file_size(url, timeout: Utils::Timer.remaining!(end_time)) rescue ErrorDuringExecution - raise unless use_cached_location + raise unless cached_location_valid end # Authorization is no longer valid after redirects meta[:headers]&.delete_if { |header| header.start_with?("Authorization") } if is_redirection - # The cached location is no longer fresh if Last-Modified is after the file's timestamp - if cached_location.exist? && last_modified && last_modified > cached_location.mtime - use_cached_location = false + # The cached location is no longer fresh if either: + # - Last-Modified value is newer than the file's timestamp + # - Content-Length value is different than the file's size + cached_location_valid = if cached_location_valid + newer_last_modified = last_modified && last_modified > cached_location.mtime + different_file_size = file_size && file_size != cached_location.size + !(newer_last_modified || different_file_size) end - if use_cached_location + if cached_location_valid puts "Already downloaded: #{cached_location}" else begin