Skip to content

Commit

Permalink
CurlDownloadStrategy: Ignore invalid last-modified header values
Browse files Browse the repository at this point in the history
- Some download locations return a non-standard formatting of date string for the `Last-Modified` header.
  This causes `Time.parse` to blow up. The user sees `error: argument out of range`.
- In this commit we handle the error and return nil, which `filter_map` (equivalent to `.map.compact`) gets rid of and then `time.last` is as normal.
- Fixes https://github.com/Homebrew/brew/issues/ 17556.
  • Loading branch information
issyl0 committed Jun 26, 2024
1 parent b780a08 commit 3dd840b
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions Library/Homebrew/download_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,13 @@ def resolve_url_basename_time_file_size(url, timeout: nil)
[*parse_content_disposition.call("Content-Disposition: #{header}")]
end

time = parsed_headers
.flat_map { |headers| [*headers["last-modified"]] }
.map { |t| t.match?(/^\d+$/) ? Time.at(t.to_i) : Time.parse(t) }
.last
time = parsed_headers
.flat_map { |headers| [*headers["last-modified"]] }
.filter_map do |t|
t.match?(/^\d+$/) ? Time.at(t.to_i) : Time.parse(t)
rescue ArgumentError
nil
end

file_size = parsed_headers
.flat_map { |headers| [*headers["content-length"]&.to_i] }
Expand All @@ -530,7 +533,7 @@ def resolve_url_basename_time_file_size(url, timeout: nil)
is_redirection = url != final_url
basename = filenames.last || parse_basename(final_url, search_query: !is_redirection)

@resolved_info_cache[url] = [final_url, basename, time, file_size, is_redirection]
@resolved_info_cache[url] = [final_url, basename, time.last, file_size, is_redirection]
end

def _fetch(url:, resolved_url:, timeout:)
Expand Down

0 comments on commit 3dd840b

Please sign in to comment.