ruby-link-checker 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -0
- data/README.md +153 -19
- data/UPGRADING.md +46 -0
- data/lib/ruby-link-checker/async/http/checker.rb +19 -6
- data/lib/ruby-link-checker/async/http/result.rb +1 -1
- data/lib/ruby-link-checker/cache.rb +90 -21
- data/lib/ruby-link-checker/checker.rb +136 -13
- data/lib/ruby-link-checker/config.rb +79 -0
- data/lib/ruby-link-checker/net/http/checker.rb +3 -2
- data/lib/ruby-link-checker/net/http/result.rb +1 -1
- data/lib/ruby-link-checker/redirect.rb +11 -0
- data/lib/ruby-link-checker/result.rb +96 -1
- data/lib/ruby-link-checker/tasks.rb +24 -12
- data/lib/ruby-link-checker/typhoeus/hydra/checker.rb +59 -11
- data/lib/ruby-link-checker/typhoeus/hydra/result.rb +1 -1
- data/lib/ruby-link-checker/version.rb +1 -1
- data/lib/ruby-link-checker.rb +1 -0
- metadata +18 -51
- data/.github/workflows/coverage.yml +0 -24
- data/.github/workflows/danger-comment.yml +0 -17
- data/.github/workflows/danger.yml +0 -12
- data/.github/workflows/lint.yml +0 -20
- data/.github/workflows/release.yml +0 -14
- data/.github/workflows/test.yml +0 -25
- data/.gitignore +0 -6
- data/.rspec +0 -2
- data/.rubocop.yml +0 -26
- data/.rubocop_todo.yml +0 -332
- data/AGENTS.md +0 -23
- data/CONTRIBUTING.md +0 -129
- data/Dangerfile +0 -6
- data/Gemfile +0 -27
- data/RELEASING.md +0 -37
- data/Rakefile +0 -26
- data/benchmarks/check.rb +0 -38
- data/benchmarks/data/opensearch.org/links.txt +0 -4270
- data/benchmarks/data/opensearch.org/small.txt +0 -100
- data/ruby-link-checker.gemspec +0 -22
- data/spec/fixtures/200.yml +0 -65
- data/spec/fixtures/301_200.yml +0 -102
- data/spec/fixtures/301_301.yml +0 -114
- data/spec/fixtures/301_400_301_200.yml +0 -185
- data/spec/fixtures/301_403.yml +0 -78
- data/spec/fixtures/308_429.yml +0 -108
- data/spec/fixtures/404.yml +0 -57
- data/spec/fixtures/404_200.yml +0 -89
- data/spec/fixtures/404_404.yml +0 -81
- data/spec/fixtures/429_200.yml +0 -135
- data/spec/fixtures/429_429_200.yml +0 -201
- data/spec/ruby-link-checker/async/http/checker_spec.rb +0 -184
- data/spec/ruby-link-checker/callbacks_spec.rb +0 -76
- data/spec/ruby-link-checker/checker_spec.rb +0 -97
- data/spec/ruby-link-checker/config_spec.rb +0 -45
- data/spec/ruby-link-checker/errors/redirect_loop_error_spec.rb +0 -18
- data/spec/ruby-link-checker/net/http/checker_spec.rb +0 -93
- data/spec/ruby-link-checker/result_error_spec.rb +0 -34
- data/spec/ruby-link-checker/result_spec.rb +0 -41
- data/spec/ruby-link-checker/task_spec.rb +0 -20
- data/spec/ruby-link-checker/tasks_spec.rb +0 -91
- data/spec/ruby-link-checker/typhoeus/hydra/checker_spec.rb +0 -180
- data/spec/ruby-link-checker/version_spec.rb +0 -9
- data/spec/spec_helper.rb +0 -19
- data/spec/support/config.rb +0 -8
- data/spec/support/link_checker.rb +0 -609
- data/spec/support/vcr.rb +0 -13
- data/spec/support/with_result.rb +0 -15
- data/spec/support/with_url.rb +0 -14
|
@@ -5,6 +5,15 @@ module LinkChecker
|
|
|
5
5
|
include LinkChecker::Config
|
|
6
6
|
include LinkChecker::Callbacks
|
|
7
7
|
|
|
8
|
+
# Events published once per link with its final result.
|
|
9
|
+
OUTCOME_EVENTS = %i[error failure success blocked].freeze
|
|
10
|
+
|
|
11
|
+
# Settings that do not change the request or response, excluded from request identity.
|
|
12
|
+
IDENTITY_EXCLUDED_ATTRIBUTES = %i[
|
|
13
|
+
logger cache deduplicate blocked_status_codes detect_challenges max_concurrency_per_host min_host_interval
|
|
14
|
+
rate_limit_retries rate_limit_status_codes rate_limit_backoff max_rate_limit_delay
|
|
15
|
+
].freeze
|
|
16
|
+
|
|
8
17
|
attr_reader :results
|
|
9
18
|
|
|
10
19
|
def initialize(options = {})
|
|
@@ -13,9 +22,11 @@ module LinkChecker
|
|
|
13
22
|
end
|
|
14
23
|
raise ArgumentError, "Missing methods." if methods&.none?
|
|
15
24
|
@logger ||= options[:logger] || LinkChecker::Config.logger || LinkChecker::Logger.default
|
|
16
|
-
@results = { error: [], failure: [], success: [] } unless options.key?(:results) && !options[:results]
|
|
25
|
+
@results = { error: [], failure: [], success: [], blocked: [] } unless options.key?(:results) && !options[:results]
|
|
17
26
|
@pending = {}
|
|
18
27
|
@successful = {}
|
|
28
|
+
@host_next_start = {}
|
|
29
|
+
@host_backoff = Hash.new(0)
|
|
19
30
|
@cache_store = Cache.new(cache, logger: @logger) if cache
|
|
20
31
|
end
|
|
21
32
|
|
|
@@ -41,22 +52,118 @@ module LinkChecker
|
|
|
41
52
|
|
|
42
53
|
cached_result = @successful[key] if deduplicate
|
|
43
54
|
cached_result ||= @cache_store&.fetch(key, uri)
|
|
44
|
-
if cached_result
|
|
55
|
+
if cached_result&.success?
|
|
45
56
|
@successful[key] ||= cached_result if deduplicate
|
|
46
|
-
|
|
47
|
-
result.cached = true
|
|
48
|
-
publish(:result, result)
|
|
49
|
-
publish(:success, result)
|
|
50
|
-
return
|
|
57
|
+
return publish_cached(:success, cached_result, subscriber)
|
|
51
58
|
end
|
|
59
|
+
return publish_cached(:blocked, cached_result, subscriber) if blocked_result?(cached_result)
|
|
52
60
|
|
|
53
61
|
entry = { subscribers: [subscriber], events: [] }
|
|
54
62
|
@pending[key] = entry if deduplicate
|
|
55
63
|
execute_check(uri, options) { |event, result| handle_shared_result(key, entry, uri, event, result) }
|
|
56
64
|
end
|
|
57
65
|
|
|
66
|
+
# Finishes a run. Checkers that queue requests run them first; Net::HTTP checks links as they are submitted.
|
|
67
|
+
def run
|
|
68
|
+
_finish_run
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Removes cache entries that this checker did not use, returning the number of entries removed.
|
|
72
|
+
def prune_cache!
|
|
73
|
+
@cache_store ? @cache_store.prune! : 0
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Whether a final, non-successful HTTP result should be reported as blocked rather than failed.
|
|
77
|
+
def blocked_result?(result)
|
|
78
|
+
return false if result.nil? || result.error? || result.success? || result.redirect?
|
|
79
|
+
return true if blocked_status_codes.include?(result.code)
|
|
80
|
+
|
|
81
|
+
detect_challenges && result.challenge?
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Request headers for uri: the user-agent, then global headers, then every matching host_headers entry.
|
|
85
|
+
# Later values replace earlier ones with the same case-insensitive name.
|
|
86
|
+
def request_headers_for(uri)
|
|
87
|
+
host = _host_key(uri)
|
|
88
|
+
sources = [{ 'User-Agent' => user_agent }, headers]
|
|
89
|
+
sources += host_headers.filter_map { |pattern, values| values if host && _host_match?(pattern, host) }
|
|
90
|
+
sources.each_with_object({}) do |values, merged|
|
|
91
|
+
values.each do |name, value|
|
|
92
|
+
merged.delete_if { |existing, _value| existing.casecmp?(name.to_s) }
|
|
93
|
+
merged[name.to_s] = value
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# The host that per-host limits apply to, or nil when the URI has none.
|
|
99
|
+
def _host_key(uri)
|
|
100
|
+
URI(uri.to_s).host&.downcase
|
|
101
|
+
rescue URI::InvalidURIError
|
|
102
|
+
nil
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Seconds to wait before the next request to host may start, without reserving it.
|
|
106
|
+
def _host_wait(host)
|
|
107
|
+
return 0 unless host && @host_next_start[host]
|
|
108
|
+
|
|
109
|
+
[@host_next_start[host] - _monotonic_time, 0].max
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# Reserves the next start for host, spaced by min_host_interval, and returns the seconds to wait for it.
|
|
113
|
+
def _reserve_host_slot(host)
|
|
114
|
+
return 0 unless host
|
|
115
|
+
|
|
116
|
+
wait = _host_wait(host)
|
|
117
|
+
@host_next_start[host] = _monotonic_time + wait + min_host_interval if min_host_interval.positive?
|
|
118
|
+
wait
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Whether a response should be retried with per-host rate limit backoff.
|
|
122
|
+
def rate_limited?(result)
|
|
123
|
+
rate_limit_retries.positive? && !result.error? && rate_limit_status_codes.include?(result.code)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Pauses every request to host after a rate limited result and returns the pause in seconds.
|
|
127
|
+
# The pause doubles with each rate limited response from host, starting at rate_limit_backoff,
|
|
128
|
+
# but rate limited responses that arrive while host is already paused don't escalate it.
|
|
129
|
+
# Retry-After is honored as a minimum, and every pause is capped at max_rate_limit_delay.
|
|
130
|
+
def _rate_limit!(host, result)
|
|
131
|
+
return 0 unless host
|
|
132
|
+
|
|
133
|
+
@host_backoff[host] += 1 unless _host_wait(host).positive? && @host_backoff[host].positive?
|
|
134
|
+
delay = rate_limit_backoff * (2**(@host_backoff[host] - 1))
|
|
135
|
+
delay = [delay, result.retry_after_delay].max
|
|
136
|
+
delay = [delay, max_rate_limit_delay].min
|
|
137
|
+
@host_next_start[host] = [@host_next_start[host] || 0, _monotonic_time + delay].max
|
|
138
|
+
logger.info "Rate limited by #{host}, pausing requests for #{delay.round(2)}s"
|
|
139
|
+
delay
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Resets the backoff of host after a response that isn't rate limited.
|
|
143
|
+
def _rate_limit_reset!(host)
|
|
144
|
+
@host_backoff.delete(host) if host
|
|
145
|
+
end
|
|
146
|
+
|
|
58
147
|
private
|
|
59
148
|
|
|
149
|
+
def _finish_run
|
|
150
|
+
prune_cache! if @cache_store&.prune?
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def _host_headers_identity
|
|
154
|
+
host_headers.map do |pattern, values|
|
|
155
|
+
[pattern.is_a?(Regexp) ? pattern.inspect : pattern, values.sort_by { |key, _value| key.to_s }]
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def _host_match?(pattern, host)
|
|
160
|
+
pattern.is_a?(Regexp) ? pattern.match?(host) : pattern == host
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def _monotonic_time
|
|
164
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
165
|
+
end
|
|
166
|
+
|
|
60
167
|
def execute_check(uri, options, &handler)
|
|
61
168
|
tasks = Tasks.new(
|
|
62
169
|
self,
|
|
@@ -71,19 +178,29 @@ module LinkChecker
|
|
|
71
178
|
tasks.execute!
|
|
72
179
|
end
|
|
73
180
|
|
|
181
|
+
def publish_cached(event, cached_result, subscriber)
|
|
182
|
+
result = copy_result(cached_result, subscriber)
|
|
183
|
+
result.cached = true
|
|
184
|
+
result.blocked = event == :blocked
|
|
185
|
+
publish(:result, result)
|
|
186
|
+
publish(event, result)
|
|
187
|
+
end
|
|
188
|
+
|
|
74
189
|
def publish(event, result)
|
|
75
190
|
result.checker = self
|
|
76
|
-
|
|
191
|
+
outcome = OUTCOME_EVENTS.include?(event)
|
|
192
|
+
results[event] << result if @results && outcome
|
|
77
193
|
callback(event, result)
|
|
194
|
+
callback(:complete, result) if outcome
|
|
78
195
|
end
|
|
79
196
|
|
|
80
197
|
def handle_shared_result(key, entry, uri, event, result)
|
|
81
198
|
entry[:events] << [event, result]
|
|
82
199
|
subscribers = entry[:subscribers].dup
|
|
83
|
-
if
|
|
200
|
+
if OUTCOME_EVENTS.include?(event)
|
|
84
201
|
@pending.delete(key) if @pending[key].equal?(entry)
|
|
85
|
-
if event
|
|
86
|
-
@successful[key] = result if deduplicate
|
|
202
|
+
if %i[success blocked].include?(event) && request_key(uri) == key
|
|
203
|
+
@successful[key] = result if deduplicate && event == :success
|
|
87
204
|
@cache_store&.store(key, result)
|
|
88
205
|
end
|
|
89
206
|
end
|
|
@@ -93,7 +210,8 @@ module LinkChecker
|
|
|
93
210
|
def copy_result(result, subscriber)
|
|
94
211
|
result.dup.tap do |copy|
|
|
95
212
|
copy.uri = URI(subscriber.first)
|
|
96
|
-
|
|
213
|
+
# Without redirects, the final URL is the submitted one, including its own fragment.
|
|
214
|
+
copy.result_uri = result.redirects.empty? ? URI(subscriber.first) : result.result_uri.dup
|
|
97
215
|
copy.options = subscriber.last
|
|
98
216
|
end
|
|
99
217
|
end
|
|
@@ -102,10 +220,15 @@ module LinkChecker
|
|
|
102
220
|
parsed = URI(uri.to_s)
|
|
103
221
|
return unless %w[http https].include?(parsed.scheme) && parsed.host && !parsed.userinfo
|
|
104
222
|
|
|
105
|
-
|
|
223
|
+
# Fragments are never sent over HTTP, so links that differ only by #fragment share a check.
|
|
224
|
+
parsed.fragment = nil
|
|
225
|
+
|
|
226
|
+
attributes = Config::ATTRIBUTES - IDENTITY_EXCLUDED_ATTRIBUTES
|
|
106
227
|
attributes += %i[timeout connecttimeout read_timeout open_timeout protocol].select { |key| respond_to?(key) }
|
|
107
228
|
settings = attributes.to_h { |key| [key, send(key)] }
|
|
108
229
|
settings[:headers] = headers.sort_by { |key, _value| key.to_s }
|
|
230
|
+
settings.delete(:host_headers)
|
|
231
|
+
settings[:host_headers] = _host_headers_identity if host_headers.any?
|
|
109
232
|
Digest::SHA256.hexdigest(JSON.generate([task_klass.name, parsed.to_s, settings]))
|
|
110
233
|
rescue URI::InvalidURIError, JSON::GeneratorError, TypeError
|
|
111
234
|
nil
|
|
@@ -8,10 +8,13 @@ module LinkChecker
|
|
|
8
8
|
methods
|
|
9
9
|
user_agent
|
|
10
10
|
headers
|
|
11
|
+
host_headers
|
|
11
12
|
logger
|
|
12
13
|
retries
|
|
13
14
|
max_redirects
|
|
14
15
|
retry_status_codes
|
|
16
|
+
blocked_status_codes
|
|
17
|
+
detect_challenges
|
|
15
18
|
retry_after
|
|
16
19
|
retry_backoff
|
|
17
20
|
retry_jitter
|
|
@@ -19,6 +22,12 @@ module LinkChecker
|
|
|
19
22
|
respect_retry_after
|
|
20
23
|
deduplicate
|
|
21
24
|
cache
|
|
25
|
+
max_concurrency_per_host
|
|
26
|
+
min_host_interval
|
|
27
|
+
rate_limit_retries
|
|
28
|
+
rate_limit_status_codes
|
|
29
|
+
rate_limit_backoff
|
|
30
|
+
max_rate_limit_delay
|
|
22
31
|
].freeze
|
|
23
32
|
|
|
24
33
|
attr_accessor(*Config::ATTRIBUTES)
|
|
@@ -27,10 +36,13 @@ module LinkChecker
|
|
|
27
36
|
self.methods = %w[HEAD GET]
|
|
28
37
|
self.user_agent = "Ruby Link Checker/#{LinkChecker::VERSION}"
|
|
29
38
|
self.headers = {}
|
|
39
|
+
self.host_headers = {}
|
|
30
40
|
self.logger = nil
|
|
31
41
|
self.retries = 0
|
|
32
42
|
self.max_redirects = 20
|
|
33
43
|
self.retry_status_codes = [408, 425, 429, 500, 502, 503, 504]
|
|
44
|
+
self.blocked_status_codes = [401, 403, 429, 999]
|
|
45
|
+
self.detect_challenges = true
|
|
34
46
|
self.retry_after = 0
|
|
35
47
|
self.retry_backoff = 1
|
|
36
48
|
self.retry_jitter = 0
|
|
@@ -38,6 +50,22 @@ module LinkChecker
|
|
|
38
50
|
self.respect_retry_after = false
|
|
39
51
|
self.deduplicate = false
|
|
40
52
|
self.cache = nil
|
|
53
|
+
self.max_concurrency_per_host = nil
|
|
54
|
+
self.min_host_interval = 0
|
|
55
|
+
self.rate_limit_retries = 2
|
|
56
|
+
self.rate_limit_status_codes = [429]
|
|
57
|
+
self.rate_limit_backoff = 1
|
|
58
|
+
self.max_rate_limit_delay = 30
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def host_headers=(value)
|
|
62
|
+
value ||= {}
|
|
63
|
+
valid = value.is_a?(Hash) && value.all? do |host, host_values|
|
|
64
|
+
(host.is_a?(String) || host.is_a?(Regexp)) && host_values.is_a?(Hash)
|
|
65
|
+
end
|
|
66
|
+
raise ArgumentError, "Invalid host headers: #{value}" unless valid
|
|
67
|
+
|
|
68
|
+
@host_headers = value.transform_keys { |host| host.is_a?(String) ? host.downcase : host }
|
|
41
69
|
end
|
|
42
70
|
|
|
43
71
|
def retries=(value)
|
|
@@ -60,6 +88,20 @@ module LinkChecker
|
|
|
60
88
|
@retry_status_codes = value&.dup
|
|
61
89
|
end
|
|
62
90
|
|
|
91
|
+
def blocked_status_codes=(value)
|
|
92
|
+
unless value.nil? || (value.is_a?(Array) && value.all? { |code| code.is_a?(Integer) && code.between?(100, 999) })
|
|
93
|
+
raise ArgumentError, "Invalid blocked status codes: #{value}"
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
@blocked_status_codes = value&.dup || []
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def detect_challenges=(value)
|
|
100
|
+
raise ArgumentError, "Invalid detect_challenges: #{value}" unless [true, false].include?(value)
|
|
101
|
+
|
|
102
|
+
@detect_challenges = value
|
|
103
|
+
end
|
|
104
|
+
|
|
63
105
|
def retry_after=(value)
|
|
64
106
|
validate_number!(:retry_after, value, 0)
|
|
65
107
|
@retry_after = value
|
|
@@ -77,6 +119,43 @@ module LinkChecker
|
|
|
77
119
|
@retry_jitter = value
|
|
78
120
|
end
|
|
79
121
|
|
|
122
|
+
def max_concurrency_per_host=(value)
|
|
123
|
+
unless value.nil? || (value.is_a?(Integer) && value.positive?)
|
|
124
|
+
raise ArgumentError, "Invalid concurrency limit per host: #{value}"
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
@max_concurrency_per_host = value
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def min_host_interval=(value)
|
|
131
|
+
validate_number!(:min_host_interval, value, 0)
|
|
132
|
+
@min_host_interval = value
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def rate_limit_retries=(value)
|
|
136
|
+
raise ArgumentError, "Invalid number of rate limit retries: #{value}" unless value.is_a?(Integer) && value >= 0
|
|
137
|
+
|
|
138
|
+
@rate_limit_retries = value
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def rate_limit_status_codes=(value)
|
|
142
|
+
unless value.nil? || (value.is_a?(Array) && value.all? { |code| code.is_a?(Integer) && code.between?(100, 999) })
|
|
143
|
+
raise ArgumentError, "Invalid rate limit status codes: #{value}"
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
@rate_limit_status_codes = value&.dup || []
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def rate_limit_backoff=(value)
|
|
150
|
+
validate_number!(:rate_limit_backoff, value, 0)
|
|
151
|
+
@rate_limit_backoff = value
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def max_rate_limit_delay=(value)
|
|
155
|
+
validate_number!(:max_rate_limit_delay, value, 0)
|
|
156
|
+
@max_rate_limit_delay = value
|
|
157
|
+
end
|
|
158
|
+
|
|
80
159
|
def max_retry_delay=(value)
|
|
81
160
|
validate_number!(:max_retry_delay, value, 0)
|
|
82
161
|
@max_retry_delay = value
|
|
@@ -4,12 +4,13 @@ module LinkChecker
|
|
|
4
4
|
class Task < ::LinkChecker::Task
|
|
5
5
|
def run!
|
|
6
6
|
sleep(delay) if delay.positive?
|
|
7
|
+
host_wait = checker._reserve_host_slot(checker._host_key(uri))
|
|
8
|
+
sleep(host_wait) if host_wait.positive?
|
|
7
9
|
::Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
|
8
10
|
http.read_timeout = checker.read_timeout if checker.read_timeout
|
|
9
11
|
http.open_timeout = checker.open_timeout if checker.open_timeout
|
|
10
12
|
request = ::Net::HTTPGenericRequest.new(method, false, true, uri)
|
|
11
|
-
request[
|
|
12
|
-
checker.headers.each { |key, value| request[key] = value }
|
|
13
|
+
checker.request_headers_for(uri).each { |key, value| request[key] = value }
|
|
13
14
|
response = http.request(request)
|
|
14
15
|
result! Result.new(uri, method, original_uri, request, response, options)
|
|
15
16
|
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LinkChecker
|
|
4
|
+
# A redirect hop: uri responded with code and redirected to the resolved target uri, to.
|
|
5
|
+
Redirect = Data.define(:uri, :code, :to) do
|
|
6
|
+
# Whether the hop was a permanent redirect (301 or 308).
|
|
7
|
+
def permanent?
|
|
8
|
+
[301, 308].include?(code)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -4,7 +4,7 @@ require 'socket'
|
|
|
4
4
|
module LinkChecker
|
|
5
5
|
class Result
|
|
6
6
|
attr_accessor :uri, :result_uri, :method, :options, :checker
|
|
7
|
-
attr_writer :cached
|
|
7
|
+
attr_writer :cached, :blocked, :redirects
|
|
8
8
|
|
|
9
9
|
def initialize(current_uri, method, original_uri, options = {})
|
|
10
10
|
@uri = original_uri
|
|
@@ -25,10 +25,43 @@ module LinkChecker
|
|
|
25
25
|
false
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
+
# A final HTTP result whose status code or bot challenge matched the checker's blocked settings.
|
|
29
|
+
def blocked?
|
|
30
|
+
!!@blocked
|
|
31
|
+
end
|
|
32
|
+
|
|
28
33
|
def error?
|
|
29
34
|
false
|
|
30
35
|
end
|
|
31
36
|
|
|
37
|
+
# Seconds the server asked to wait with Retry-After (delta-seconds or an HTTP date), or 0.
|
|
38
|
+
def retry_after_delay
|
|
39
|
+
value = Array(response_headers['Retry-After'] || response_headers['retry-after']).first.to_s.strip
|
|
40
|
+
return value.to_i if /\A\d+\z/.match?(value)
|
|
41
|
+
|
|
42
|
+
[Time.httpdate(value) - Time.now, 0].max
|
|
43
|
+
rescue ArgumentError
|
|
44
|
+
0
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Known bot challenges, detected from response headers: the name of the challenge, the header, and its value.
|
|
48
|
+
CHALLENGES = [
|
|
49
|
+
# https://developers.cloudflare.com/cloudflare-challenges/challenge-types/challenge-pages/detect-response/
|
|
50
|
+
%w[Cloudflare cf-mitigated challenge]
|
|
51
|
+
].freeze
|
|
52
|
+
|
|
53
|
+
# The name of the bot challenge that the response carries, such as "Cloudflare", or nil.
|
|
54
|
+
def challenge
|
|
55
|
+
CHALLENGES.find do |_name, header, value|
|
|
56
|
+
Array(response_headers[header]).join(',').split(',').map(&:strip).include?(value)
|
|
57
|
+
end&.first
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Whether the response carries a known bot challenge.
|
|
61
|
+
def challenge?
|
|
62
|
+
!challenge.nil?
|
|
63
|
+
end
|
|
64
|
+
|
|
32
65
|
def redirect?
|
|
33
66
|
false
|
|
34
67
|
end
|
|
@@ -37,6 +70,16 @@ module LinkChecker
|
|
|
37
70
|
nil
|
|
38
71
|
end
|
|
39
72
|
|
|
73
|
+
# The redirect hops that led to this result, oldest first.
|
|
74
|
+
def redirects
|
|
75
|
+
@redirects || []
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Whether any redirect hop that led to this result was permanent (301 or 308).
|
|
79
|
+
def permanent_redirect?
|
|
80
|
+
redirects.any?(&:permanent?)
|
|
81
|
+
end
|
|
82
|
+
|
|
40
83
|
def request_headers
|
|
41
84
|
{}
|
|
42
85
|
end
|
|
@@ -57,9 +100,47 @@ module LinkChecker
|
|
|
57
100
|
nil
|
|
58
101
|
end
|
|
59
102
|
|
|
103
|
+
# :success, :failure, :blocked, or :error for a final result, or :redirect for a redirect hop.
|
|
104
|
+
def outcome
|
|
105
|
+
if success? then :success
|
|
106
|
+
elsif blocked? then :blocked
|
|
107
|
+
elsif error? then :error
|
|
108
|
+
elsif redirect? then :redirect
|
|
109
|
+
else :failure
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# A stable, human-readable explanation of the outcome, such as "HTTP 404" or "Cloudflare challenge (HTTP 403)".
|
|
114
|
+
def reason
|
|
115
|
+
name = challenge unless success?
|
|
116
|
+
name ? "#{name} challenge (HTTP #{code})" : "HTTP #{code}"
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# A serializable summary of the result.
|
|
120
|
+
def to_h
|
|
121
|
+
{
|
|
122
|
+
url: uri.to_s,
|
|
123
|
+
result_url: result_uri.to_s,
|
|
124
|
+
method: method,
|
|
125
|
+
code: code.is_a?(Integer) ? code : nil,
|
|
126
|
+
outcome: outcome,
|
|
127
|
+
reason: reason,
|
|
128
|
+
redirects: redirects.map { |hop| { url: hop.uri.to_s, code: hop.code, to: hop.to.to_s } },
|
|
129
|
+
permanent_redirect: permanent_redirect?,
|
|
130
|
+
blocked: blocked?,
|
|
131
|
+
challenge: challenge,
|
|
132
|
+
cached: cached?,
|
|
133
|
+
error: error&.message,
|
|
134
|
+
error_type: error_type,
|
|
135
|
+
options: options
|
|
136
|
+
}
|
|
137
|
+
end
|
|
138
|
+
|
|
60
139
|
def to_s
|
|
61
140
|
status_s = if success?
|
|
62
141
|
'OK'
|
|
142
|
+
elsif blocked?
|
|
143
|
+
'BLOCKED'
|
|
63
144
|
elsif failure?
|
|
64
145
|
'FAIL'
|
|
65
146
|
elsif redirect?
|
|
@@ -87,6 +168,20 @@ module LinkChecker
|
|
|
87
168
|
error.class.name
|
|
88
169
|
end
|
|
89
170
|
|
|
171
|
+
def reason
|
|
172
|
+
case error
|
|
173
|
+
when LinkChecker::Errors::RedirectLoopError then 'Redirect loop'
|
|
174
|
+
when LinkChecker::Errors::RedirectLimitError then 'Too many redirects'
|
|
175
|
+
when URI::InvalidURIError then 'Invalid URL'
|
|
176
|
+
else
|
|
177
|
+
case error_type
|
|
178
|
+
when :timeout then 'Timeout'
|
|
179
|
+
when :connection then 'Connection error'
|
|
180
|
+
else "Error (#{error.class.name})"
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
90
185
|
def error_type
|
|
91
186
|
return :timeout if error.is_a?(Timeout::Error)
|
|
92
187
|
return :timeout if defined?(::Async::TimeoutError) && error.is_a?(::Async::TimeoutError)
|
|
@@ -9,6 +9,7 @@ module LinkChecker
|
|
|
9
9
|
def initialize(checker, task_klass, uri, methods, options = {})
|
|
10
10
|
@uri = uri
|
|
11
11
|
@retries_left = checker.retries
|
|
12
|
+
@rate_limit_retries_left = checker.rate_limit_retries
|
|
12
13
|
@retry_count = 0
|
|
13
14
|
@methods_left = methods.dup
|
|
14
15
|
@methods = methods.dup
|
|
@@ -16,6 +17,7 @@ module LinkChecker
|
|
|
16
17
|
@checker = checker
|
|
17
18
|
@logger = checker.logger
|
|
18
19
|
@redirects = [uri]
|
|
20
|
+
@hops = []
|
|
19
21
|
@options = options
|
|
20
22
|
raise ArgumentError, :tasks_klass unless @task_klass && @task_klass < ::LinkChecker::Task
|
|
21
23
|
end
|
|
@@ -25,7 +27,12 @@ module LinkChecker
|
|
|
25
27
|
end
|
|
26
28
|
|
|
27
29
|
def execute!
|
|
28
|
-
if
|
|
30
|
+
if rate_limit_retry?
|
|
31
|
+
@rate_limit_retries_left -= 1
|
|
32
|
+
checker._rate_limit!(checker._host_key(@result.result_uri), @result)
|
|
33
|
+
retry! @result
|
|
34
|
+
_queue_task(uri, method, original_uri || uri, options)
|
|
35
|
+
elsif retry?
|
|
29
36
|
@retries_left -= 1
|
|
30
37
|
@retry_count += 1
|
|
31
38
|
retry! @result
|
|
@@ -37,6 +44,9 @@ module LinkChecker
|
|
|
37
44
|
_queue_task(uri, method, original_uri || uri, options)
|
|
38
45
|
elsif @result && result.error?
|
|
39
46
|
error! @result
|
|
47
|
+
elsif checker.blocked_result?(@result)
|
|
48
|
+
@result.blocked = true
|
|
49
|
+
blocked! @result
|
|
40
50
|
else
|
|
41
51
|
failure! @result
|
|
42
52
|
end
|
|
@@ -65,6 +75,10 @@ module LinkChecker
|
|
|
65
75
|
!first_time? && retries_left > 0 && retryable?
|
|
66
76
|
end
|
|
67
77
|
|
|
78
|
+
def rate_limit_retry?
|
|
79
|
+
!first_time? && @rate_limit_retries_left.positive? && result && checker.rate_limited?(result)
|
|
80
|
+
end
|
|
81
|
+
|
|
68
82
|
def retryable?
|
|
69
83
|
return false unless result
|
|
70
84
|
return result.error? || result.failure? if checker.retry_status_codes.nil?
|
|
@@ -75,21 +89,11 @@ module LinkChecker
|
|
|
75
89
|
|
|
76
90
|
def _retry_delay
|
|
77
91
|
delay = checker.retry_after.zero? ? 0 : checker.retry_after * (checker.retry_backoff**(@retry_count - 1))
|
|
78
|
-
delay = [delay,
|
|
92
|
+
delay = [delay, result.retry_after_delay].max if checker.respect_retry_after
|
|
79
93
|
delay *= 1 + (rand * checker.retry_jitter) if checker.retry_jitter.positive?
|
|
80
94
|
[delay, checker.max_retry_delay].min
|
|
81
95
|
end
|
|
82
96
|
|
|
83
|
-
def _server_retry_delay
|
|
84
|
-
headers = result.response_headers
|
|
85
|
-
value = Array(headers['Retry-After'] || headers['retry-after']).first.to_s.strip
|
|
86
|
-
return value.to_i if /\A\d+\z/.match?(value)
|
|
87
|
-
|
|
88
|
-
[Time.httpdate(value) - Time.now, 0].max
|
|
89
|
-
rescue ArgumentError
|
|
90
|
-
0
|
|
91
|
-
end
|
|
92
|
-
|
|
93
97
|
def _queue_task(uri, method, original_uri, options = {}, delay: 0)
|
|
94
98
|
task = new_task(uri, method, original_uri, options)
|
|
95
99
|
task.delay = delay
|
|
@@ -114,12 +118,18 @@ module LinkChecker
|
|
|
114
118
|
|
|
115
119
|
def _handle_result(result)
|
|
116
120
|
@result = result
|
|
121
|
+
result.redirects = @hops.dup.freeze unless result.redirect?
|
|
117
122
|
retry_text = " (#{_retries_left_s})" if retry? && (result.error? || result.failure?)
|
|
123
|
+
retry_text = ' (rate limited)' if !first_time? && @rate_limit_retries_left.positive? && checker.rate_limited?(result)
|
|
118
124
|
logger.info "#{' ' * (redirects.count - 1)}#{result}#{retry_text}"
|
|
119
125
|
result! result
|
|
126
|
+
unless result.error? || checker.rate_limited?(result)
|
|
127
|
+
checker._rate_limit_reset!(checker._host_key(result.result_uri))
|
|
128
|
+
end
|
|
120
129
|
if result.redirect?
|
|
121
130
|
redirect! result
|
|
122
131
|
redirected_to_uri = URI.join(result.result_uri, result.redirect_to)
|
|
132
|
+
@hops << Redirect.new(uri: result.result_uri, code: result.code, to: redirected_to_uri)
|
|
123
133
|
if redirects.include?(redirected_to_uri)
|
|
124
134
|
raise LinkChecker::Errors::RedirectLoopError,
|
|
125
135
|
redirects.push(redirected_to_uri)
|
|
@@ -134,11 +144,13 @@ module LinkChecker
|
|
|
134
144
|
success! result
|
|
135
145
|
else
|
|
136
146
|
@redirects = [uri]
|
|
147
|
+
@hops = []
|
|
137
148
|
execute!
|
|
138
149
|
end
|
|
139
150
|
rescue StandardError => e
|
|
140
151
|
logger.error("#{self}##{__method__}") { e } rescue nil
|
|
141
152
|
error_result = ResultError.new(result.result_uri, result.method, result.uri, e, options)
|
|
153
|
+
error_result.redirects = @hops.dup.freeze
|
|
142
154
|
result! error_result
|
|
143
155
|
error! error_result
|
|
144
156
|
end
|