From df702964032f0a48ff4a6b838ec9552e3392e0f2 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 13 Dec 2020 12:23:59 +0100 Subject: [PATCH] Allow extracting file name in `FollowRedirection` strategy. --- .../livecheck/strategy/follow_redirection.rb | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/livecheck/strategy/follow_redirection.rb b/Library/Homebrew/livecheck/strategy/follow_redirection.rb index f27e6176d3..d50b4c8eb3 100644 --- a/Library/Homebrew/livecheck/strategy/follow_redirection.rb +++ b/Library/Homebrew/livecheck/strategy/follow_redirection.rb @@ -30,15 +30,37 @@ module Homebrew # Checks the final URL for new versions after following all redirections, # using the provided regex for matching. sig { params(url: String, regex: T.nilable(Regexp)).returns(T::Hash[Symbol, T.untyped]) } - def self.find_versions(url, regex) - raise ArgumentError, "A regular expression is required for the #{NICE_NAME} strategy." if regex.nil? - + def self.find_versions(url, regex, &block) match_data = { matches: {}, regex: regex, url: url } - if (location = Strategy.page_headers(url)["location"]) && (match = location[regex, 1]) - match_data[:matches][match] = Version.new(match) + data = { headers: Strategy.page_headers(url) } + + if (filename = data[:headers]["content-disposition"]) + if regex + data[:version] ||= location[regex, 1] + else + v = Version.parse(filename, detected_from_url: true) + data[:version] ||= v.to_s unless v.null? + end end + if (location = data[:headers]["location"]) + if regex + data[:version] ||= location[regex, 1] + else + v = Version.parse(location, detected_from_url: true) + data[:version] ||= v.to_s unless v.null? + end + end + + version = if block + block.call(data) + else + data[:version] + end + + match_data[:matches][version] = Version.new(version) if version + match_data end end