Skip to content

Commit

Permalink
download_strategy: compare cached size to Content-Length
Browse files Browse the repository at this point in the history
  • Loading branch information
EricFromCanada committed Mar 11, 2025
1 parent ca33335 commit 14a541f
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Library/Homebrew/download_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ def fetch(timeout: nil)
use_cached_location = cached_location.exist?
use_cached_location = false if version.respond_to?(:latest?) && version.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
Expand All @@ -419,10 +419,12 @@ def fetch(timeout: nil)
# 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
end
# 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
newer_last_modified = last_modified && last_modified > cached_location.mtime
different_file_size = file_size && file_size != cached_location.size
use_cached_location = false if cached_location.exist? && (newer_last_modified || different_file_size)

if use_cached_location
puts "Already downloaded: #{cached_location}"
Expand Down

0 comments on commit 14a541f

Please sign in to comment.