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-14 02:49:32 +01:00
|
|
|
NICE_NAME = "Match HTTP Headers"
|
2020-12-12 21:56:33 +01:00
|
|
|
|
|
|
|
# We set the priority to zero since this cannot
|
|
|
|
# be detected automatically.
|
|
|
|
PRIORITY = 0
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
url.match?(%r{^https?://})
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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]) }
|
2020-12-13 12:23:59 +01:00
|
|
|
def self.find_versions(url, regex, &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-18 21:17:55 +01:00
|
|
|
if block
|
|
|
|
match = block.call(headers)
|
|
|
|
else
|
|
|
|
match = nil
|
2020-12-13 12:23:59 +01:00
|
|
|
|
2020-12-18 21:17:55 +01:00
|
|
|
if (filename = headers["content-disposition"])
|
|
|
|
if regex
|
|
|
|
match ||= location[regex, 1]
|
|
|
|
else
|
|
|
|
v = Version.parse(filename, detected_from_url: true)
|
|
|
|
match ||= v.to_s unless v.null?
|
|
|
|
end
|
2020-12-13 12:23:59 +01:00
|
|
|
end
|
2020-12-12 21:56:33 +01:00
|
|
|
|
2020-12-18 21:17:55 +01:00
|
|
|
if (location = headers["location"])
|
|
|
|
if regex
|
|
|
|
match ||= location[regex, 1]
|
|
|
|
else
|
|
|
|
v = Version.parse(location, detected_from_url: true)
|
|
|
|
match ||= v.to_s unless v.null?
|
|
|
|
end
|
|
|
|
end
|
2020-12-13 12:23:59 +01:00
|
|
|
end
|
|
|
|
|
2020-12-18 21:17:55 +01:00
|
|
|
match_data[:matches][match] = Version.new(match) if match
|
2020-12-13 12:23:59 +01:00
|
|
|
|
2020-12-12 21:56:33 +01:00
|
|
|
match_data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|