forked from Homebrew/brew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
header_match.rb
91 lines (76 loc) 路 2.93 KB
/
header_match.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# typed: false
# frozen_string_literal: true
require_relative "page_match"
module Homebrew
module Livecheck
module Strategy
# The {HeaderMatch} strategy follows all URL redirections and scans
# the resulting headers for matching text using the provided regex.
#
# @api private
class HeaderMatch
extend T::Sig
NICE_NAME = "Header match"
# 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.
PRIORITY = 0
# The `Regexp` used to determine if the strategy applies to the URL.
URL_MATCH_REGEX = %r{^https?://}i.freeze
# The header fields to check when a `strategy` block isn't provided.
DEFAULT_HEADERS_TO_CHECK = ["content-disposition", "location"].freeze
# Whether the strategy can be applied to the provided URL.
# The strategy will technically match any HTTP URL but is
# only usable with a `livecheck` block containing a regex
# or block.
sig { params(url: String).returns(T::Boolean) }
def self.match?(url)
URL_MATCH_REGEX.match?(url)
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),
cask: T.nilable(Cask::Cask),
block: T.nilable(T.proc.params(arg0: T::Hash[String, String]).returns(T.nilable(String))),
).returns(T::Hash[Symbol, T.untyped])
}
def self.find_versions(url, regex, cask: nil, &block)
match_data = { matches: {}, regex: regex, url: url }
headers = Strategy.page_headers(url)
# Merge the headers from all responses into one hash
merged_headers = headers.reduce(&:merge)
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
else
value = nil
DEFAULT_HEADERS_TO_CHECK.each do |header_name|
header_value = merged_headers[header_name]
next if header_value.blank?
if regex
value = header_value[regex, 1]
else
v = Version.parse(header_value, detected_from_url: true)
value = v.to_s unless v.null?
end
break if value.present?
end
value
end
match_data[:matches][version] = Version.new(version) if version
match_data
end
end
end
end
end