2020-12-12 21:56:33 +01:00
|
|
|
# typed: false
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative "page_match"
|
|
|
|
|
|
|
|
module Homebrew
|
|
|
|
module Livecheck
|
|
|
|
module Strategy
|
2020-12-14 02:49:32 +01:00
|
|
|
# The {HeaderMatch} strategy follows all URL redirections and scans
|
|
|
|
# the resulting headers for matching text using the provided regex.
|
2020-12-12 21:56:33 +01:00
|
|
|
#
|
|
|
|
# @api private
|
2020-12-14 02:49:32 +01:00
|
|
|
class HeaderMatch
|
2020-12-12 21:56:33 +01:00
|
|
|
extend T::Sig
|
|
|
|
|
2020-12-19 01:20:00 -05:00
|
|
|
NICE_NAME = "Header match"
|
2020-12-12 21:56:33 +01:00
|
|
|
|
2020-12-19 11:29:56 -05:00
|
|
|
# A priority of zero causes livecheck to skip the strategy. We only
|
|
|
|
# apply {HeaderMatch} using `strategy :header_match` in a `livecheck`
|
|
|
|
# block, as we can't automatically determine when this can be
|
|
|
|
# successfully applied to a URL.
|
2020-12-12 21:56:33 +01:00
|
|
|
PRIORITY = 0
|
|
|
|
|
2020-12-19 11:29:56 -05:00
|
|
|
# The `Regexp` used to determine if the strategy applies to the URL.
|
|
|
|
URL_MATCH_REGEX = %r{^https?://}i.freeze
|
|
|
|
|
2021-07-26 20:42:33 -04:00
|
|
|
# The header fields to check when a `strategy` block isn't provided.
|
|
|
|
DEFAULT_HEADERS_TO_CHECK = ["content-disposition", "location"].freeze
|
|
|
|
|
2020-12-12 21:56:33 +01:00
|
|
|
# Whether the strategy can be applied to the provided URL.
|
2020-12-14 02:49:32 +01:00
|
|
|
# The strategy will technically match any HTTP URL but is
|
|
|
|
# only usable with a `livecheck` block containing a regex
|
|
|
|
# or block.
|
2020-12-12 21:56:33 +01:00
|
|
|
sig { params(url: String).returns(T::Boolean) }
|
|
|
|
def self.match?(url)
|
2020-12-19 11:29:56 -05:00
|
|
|
URL_MATCH_REGEX.match?(url)
|
2020-12-12 21:56:33 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Checks the final URL for new versions after following all redirections,
|
|
|
|
# using the provided regex for matching.
|
2021-04-04 03:00:34 +02:00
|
|
|
sig {
|
|
|
|
params(
|
|
|
|
url: String,
|
|
|
|
regex: T.nilable(Regexp),
|
|
|
|
cask: T.nilable(Cask::Cask),
|
2021-07-26 20:32:10 -04:00
|
|
|
block: T.nilable(T.proc.params(arg0: T::Hash[String, String]).returns(T.nilable(String))),
|
2021-04-04 03:00:34 +02:00
|
|
|
).returns(T::Hash[Symbol, T.untyped])
|
|
|
|
}
|
|
|
|
def self.find_versions(url, regex, cask: nil, &block)
|
2020-12-12 21:56:33 +01:00
|
|
|
match_data = { matches: {}, regex: regex, url: url }
|
|
|
|
|
2020-12-18 21:17:55 +01:00
|
|
|
headers = Strategy.page_headers(url)
|
2020-12-13 12:23:59 +01:00
|
|
|
|
2020-12-19 00:46:18 -05:00
|
|
|
# Merge the headers from all responses into one hash
|
2020-12-19 20:40:29 +01:00
|
|
|
merged_headers = headers.reduce(&:merge)
|
2020-12-19 00:46:18 -05:00
|
|
|
|
2021-07-26 20:32:10 -04:00
|
|
|
version = if block
|
|
|
|
case (value = block.call(merged_headers, regex))
|
|
|
|
when String
|
|
|
|
value
|
|
|
|
when nil
|
|
|
|
return match_data
|
|
|
|
else
|
|
|
|
raise TypeError, "Return value of `strategy :header_match` block must be a string."
|
|
|
|
end
|
2020-12-18 21:17:55 +01:00
|
|
|
else
|
2021-07-26 20:32:10 -04:00
|
|
|
value = nil
|
2021-07-26 20:42:33 -04:00
|
|
|
DEFAULT_HEADERS_TO_CHECK.each do |header_name|
|
|
|
|
header_value = merged_headers[header_name]
|
|
|
|
next if header_value.blank?
|
2020-12-13 12:23:59 +01:00
|
|
|
|
2020-12-18 21:17:55 +01:00
|
|
|
if regex
|
2021-07-26 20:42:33 -04:00
|
|
|
value = header_value[regex, 1]
|
2020-12-18 21:17:55 +01:00
|
|
|
else
|
2021-07-26 20:42:33 -04:00
|
|
|
v = Version.parse(header_value, detected_from_url: true)
|
|
|
|
value = v.to_s unless v.null?
|
2020-12-18 21:17:55 +01:00
|
|
|
end
|
2021-07-26 20:42:33 -04:00
|
|
|
break if value.present?
|
2020-12-18 21:17:55 +01:00
|
|
|
end
|
2021-07-26 20:32:10 -04:00
|
|
|
|
|
|
|
value
|
2020-12-13 12:23:59 +01:00
|
|
|
end
|
|
|
|
|
2021-07-26 20:32:10 -04:00
|
|
|
match_data[:matches][version] = Version.new(version) if version
|
2020-12-13 12:23:59 +01:00
|
|
|
|
2020-12-12 21:56:33 +01:00
|
|
|
match_data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|