Merge pull request #19460 from Homebrew/cache-consider-file-size

download_strategy: compare cached file size to Content-Length
This commit is contained in:
Mike McQuaid 2025-03-15 09:12:07 +00:00 committed by GitHub
commit 4d55e48a16
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -470,25 +470,29 @@ class CurlDownloadStrategy < AbstractFileDownloadStrategy
ohai "Downloading #{url}" ohai "Downloading #{url}"
use_cached_location = cached_location.exist? cached_location_valid = cached_location.exist?
v = version 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)) resolve_url_basename_time_file_size(url, timeout: Utils::Timer.remaining!(end_time))
rescue ErrorDuringExecution rescue ErrorDuringExecution
raise unless use_cached_location raise unless cached_location_valid
end end
# Authorization is no longer valid after redirects # Authorization is no longer valid after redirects
meta[:headers]&.delete_if { |header| header.start_with?("Authorization") } if is_redirection 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 # The cached location is no longer fresh if either:
if cached_location.exist? && last_modified && last_modified > cached_location.mtime # - Last-Modified value is newer than the file's timestamp
use_cached_location = false # - 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 end
if use_cached_location if cached_location_valid
puts "Already downloaded: #{cached_location}" puts "Already downloaded: #{cached_location}"
else else
begin begin