ruby-link-checker 0.6.1 → 0.7.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 +9 -0
- data/README.md +263 -319
- data/UPGRADING.md +22 -0
- data/lib/ruby-link-checker/async/http/checker.rb +12 -4
- data/lib/ruby-link-checker/async/http/result.rb +10 -5
- data/lib/ruby-link-checker/cache.rb +25 -3
- data/lib/ruby-link-checker/checker.rb +173 -42
- data/lib/ruby-link-checker/config.rb +44 -0
- data/lib/ruby-link-checker/html.rb +103 -0
- data/lib/ruby-link-checker/net/http/checker.rb +1 -1
- data/lib/ruby-link-checker/net/http/result.rb +5 -1
- data/lib/ruby-link-checker/redirect.rb +7 -1
- data/lib/ruby-link-checker/result.rb +64 -2
- data/lib/ruby-link-checker/task.rb +6 -0
- data/lib/ruby-link-checker/tasks.rb +40 -13
- data/lib/ruby-link-checker/typhoeus/hydra/checker.rb +1 -1
- data/lib/ruby-link-checker/typhoeus/hydra/result.rb +5 -1
- data/lib/ruby-link-checker/url_rewrites.rb +87 -0
- data/lib/ruby-link-checker/version.rb +1 -1
- data/lib/ruby-link-checker.rb +2 -0
- metadata +4 -2
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module LinkChecker
|
|
4
|
-
# A redirect hop: uri responded with code and redirected to the resolved target uri, to.
|
|
4
|
+
# A redirect hop: uri responded with code and redirected to the resolved target uri, to. The code of a hop by a
|
|
5
|
+
# <meta http-equiv="refresh"> of a page is nil.
|
|
5
6
|
Redirect = Data.define(:uri, :code, :to) do
|
|
7
|
+
# Whether the hop was a <meta http-equiv="refresh"> of a page rather than an HTTP redirect.
|
|
8
|
+
def meta_refresh?
|
|
9
|
+
code.nil?
|
|
10
|
+
end
|
|
11
|
+
|
|
6
12
|
# Whether the hop was a permanent redirect (301 or 308).
|
|
7
13
|
def permanent?
|
|
8
14
|
[301, 308].include?(code)
|
|
@@ -4,7 +4,11 @@ require 'socket'
|
|
|
4
4
|
module LinkChecker
|
|
5
5
|
class Result
|
|
6
6
|
attr_accessor :uri, :result_uri, :method, :options, :checker
|
|
7
|
-
attr_writer :cached, :blocked, :redirects
|
|
7
|
+
attr_writer :cached, :blocked, :redirects, :rewritten_uris
|
|
8
|
+
# The fragment that the final page doesn't contain, without the leading #, or nil.
|
|
9
|
+
attr_accessor :missing_fragment
|
|
10
|
+
# The target of the page's <meta http-equiv="refresh"> when it isn't a valid HTTP(S) URL, or nil.
|
|
11
|
+
attr_accessor :invalid_meta_refresh
|
|
8
12
|
|
|
9
13
|
def initialize(current_uri, method, original_uri, options = {})
|
|
10
14
|
@uri = original_uri
|
|
@@ -13,10 +17,56 @@ module LinkChecker
|
|
|
13
17
|
@options = options
|
|
14
18
|
end
|
|
15
19
|
|
|
20
|
+
# Whether the final response succeeded, its page doesn't refresh to an invalid URL, and, when a fragment was
|
|
21
|
+
# checked, the page contains it.
|
|
16
22
|
def success?
|
|
23
|
+
http_success? && !missing_fragment? && !invalid_meta_refresh?
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Whether the page's <meta http-equiv="refresh"> has an invalid target.
|
|
27
|
+
def invalid_meta_refresh?
|
|
28
|
+
!invalid_meta_refresh.nil?
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# The URL that the page's <meta http-equiv="refresh"> refreshes to, as written, or nil when the page wasn't read.
|
|
32
|
+
def meta_refresh
|
|
33
|
+
return @meta_refresh if defined?(@meta_refresh)
|
|
34
|
+
|
|
35
|
+
@meta_refresh = body_checked? && HTML.html?(content_type) ? HTML.meta_refresh(body) : nil
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Whether the final response had a 2xx status.
|
|
39
|
+
def http_success?
|
|
17
40
|
false
|
|
18
41
|
end
|
|
19
42
|
|
|
43
|
+
# Whether a checked fragment was missing from the final page.
|
|
44
|
+
def missing_fragment?
|
|
45
|
+
!missing_fragment.nil?
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# The response body, when it was read.
|
|
49
|
+
def body
|
|
50
|
+
nil
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# The media type and parameters of the response, such as "text/html; charset=utf-8", or nil.
|
|
54
|
+
def content_type
|
|
55
|
+
Array(response_headers['Content-Type'] || response_headers['content-type']).first
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Whether the body of a successful GET was read, so that the page's fragment targets are known.
|
|
59
|
+
def body_checked?
|
|
60
|
+
http_success? && method.to_s.casecmp?('GET')
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# The ids and <a> names of a successful HTML page, or nil when the body wasn't read or isn't HTML.
|
|
64
|
+
def anchors
|
|
65
|
+
return @anchors if defined?(@anchors)
|
|
66
|
+
|
|
67
|
+
@anchors = body_checked? && HTML.html?(content_type) ? HTML.anchors(body).freeze : nil
|
|
68
|
+
end
|
|
69
|
+
|
|
20
70
|
def cached?
|
|
21
71
|
!!@cached
|
|
22
72
|
end
|
|
@@ -70,6 +120,11 @@ module LinkChecker
|
|
|
70
120
|
nil
|
|
71
121
|
end
|
|
72
122
|
|
|
123
|
+
# The URLs that a url_rewrites rule rewrote the submitted URL to and that were checked instead, or [].
|
|
124
|
+
def rewritten_uris
|
|
125
|
+
@rewritten_uris || []
|
|
126
|
+
end
|
|
127
|
+
|
|
73
128
|
# The redirect hops that led to this result, oldest first.
|
|
74
129
|
def redirects
|
|
75
130
|
@redirects || []
|
|
@@ -112,6 +167,9 @@ module LinkChecker
|
|
|
112
167
|
|
|
113
168
|
# A stable, human-readable explanation of the outcome, such as "HTTP 404" or "Cloudflare challenge (HTTP 403)".
|
|
114
169
|
def reason
|
|
170
|
+
return "Missing fragment ##{missing_fragment} (HTTP #{code})" if missing_fragment?
|
|
171
|
+
return "Invalid meta refresh URL (HTTP #{code})" if invalid_meta_refresh?
|
|
172
|
+
|
|
115
173
|
name = challenge unless success?
|
|
116
174
|
name ? "#{name} challenge (HTTP #{code})" : "HTTP #{code}"
|
|
117
175
|
end
|
|
@@ -132,6 +190,8 @@ module LinkChecker
|
|
|
132
190
|
cached: cached?,
|
|
133
191
|
error: error&.message,
|
|
134
192
|
error_type: error_type,
|
|
193
|
+
missing_fragment: missing_fragment,
|
|
194
|
+
rewritten_urls: rewritten_uris.map(&:to_s),
|
|
135
195
|
options: options
|
|
136
196
|
}
|
|
137
197
|
end
|
|
@@ -148,7 +208,9 @@ module LinkChecker
|
|
|
148
208
|
else
|
|
149
209
|
'ERROR'
|
|
150
210
|
end
|
|
151
|
-
|
|
211
|
+
fragment_s = ", missing fragment ##{missing_fragment}" if missing_fragment?
|
|
212
|
+
fragment_s = ", invalid meta refresh URL #{invalid_meta_refresh}" if invalid_meta_refresh?
|
|
213
|
+
"#{method} #{uri}#{result_uri == uri ? nil : ' (' + result_uri.to_s + ')'} : #{status_s} (#{code}#{fragment_s})"
|
|
152
214
|
end
|
|
153
215
|
end
|
|
154
216
|
|
|
@@ -4,6 +4,8 @@ module LinkChecker
|
|
|
4
4
|
|
|
5
5
|
attr_reader :uri, :original_uri, :method, :logger, :options, :checker
|
|
6
6
|
attr_accessor :delay
|
|
7
|
+
# Headers of a URL rewrite to send with the request, in addition to the checker's headers.
|
|
8
|
+
attr_writer :headers
|
|
7
9
|
|
|
8
10
|
def initialize(checker, uri, method, original_uri, options = {})
|
|
9
11
|
@checker = checker
|
|
@@ -15,6 +17,10 @@ module LinkChecker
|
|
|
15
17
|
@delay = 0
|
|
16
18
|
end
|
|
17
19
|
|
|
20
|
+
def headers
|
|
21
|
+
@headers || {}
|
|
22
|
+
end
|
|
23
|
+
|
|
18
24
|
def run!
|
|
19
25
|
raise NotImplementedError
|
|
20
26
|
end
|
|
@@ -6,8 +6,11 @@ module LinkChecker
|
|
|
6
6
|
|
|
7
7
|
attr_reader :result, :uri, :original_uri
|
|
8
8
|
|
|
9
|
-
def initialize(checker, task_klass, uri, methods, options = {})
|
|
9
|
+
def initialize(checker, task_klass, uri, methods, options = {}, headers: {})
|
|
10
10
|
@uri = uri
|
|
11
|
+
# The headers of a URL rewrite are only sent to its host, never to the hosts that it redirects to.
|
|
12
|
+
@headers = headers
|
|
13
|
+
@headers_host = checker._host_key(uri)
|
|
11
14
|
@retries_left = checker.retries
|
|
12
15
|
@rate_limit_retries_left = checker.rate_limit_retries
|
|
13
16
|
@retry_count = 0
|
|
@@ -97,6 +100,7 @@ module LinkChecker
|
|
|
97
100
|
def _queue_task(uri, method, original_uri, options = {}, delay: 0)
|
|
98
101
|
task = new_task(uri, method, original_uri, options)
|
|
99
102
|
task.delay = delay
|
|
103
|
+
task.headers = @headers if @headers.any? && checker._host_key(uri) == @headers_host
|
|
100
104
|
task.on :result do |result|
|
|
101
105
|
_handle_result result
|
|
102
106
|
end
|
|
@@ -106,6 +110,34 @@ module LinkChecker
|
|
|
106
110
|
_handle_result ResultError.new(uri, method, original_uri, e, options)
|
|
107
111
|
end
|
|
108
112
|
|
|
113
|
+
# Follows a redirect hop with code, or a meta refresh without one, to the resolved URI to.
|
|
114
|
+
def _follow!(result, to, code)
|
|
115
|
+
@hops << Redirect.new(uri: result.result_uri, code: code, to: to)
|
|
116
|
+
raise LinkChecker::Errors::RedirectLoopError, redirects.push(to) if redirects.include?(to)
|
|
117
|
+
if redirects.length - 1 >= checker.max_redirects
|
|
118
|
+
raise LinkChecker::Errors::RedirectLimitError.new(checker.max_redirects, redirects + [to])
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
redirects << to
|
|
122
|
+
_queue_task(to, result.method, result.uri, options)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# The resolved target of the meta refresh of a page that was read, or nil. Marks the result when the target isn't
|
|
126
|
+
# a valid HTTP(S) URL.
|
|
127
|
+
def _meta_refresh_target(result)
|
|
128
|
+
refresh = result.meta_refresh if result.http_success?
|
|
129
|
+
return if refresh.nil?
|
|
130
|
+
|
|
131
|
+
to = URI.join(result.result_uri, refresh)
|
|
132
|
+
return to if to.is_a?(URI::HTTP) && to.host
|
|
133
|
+
|
|
134
|
+
result.invalid_meta_refresh = refresh
|
|
135
|
+
nil
|
|
136
|
+
rescue URI::InvalidURIError
|
|
137
|
+
result.invalid_meta_refresh = refresh
|
|
138
|
+
nil
|
|
139
|
+
end
|
|
140
|
+
|
|
109
141
|
def _retries_left_s
|
|
110
142
|
return nil unless retry? && retries_left > 0
|
|
111
143
|
|
|
@@ -119,6 +151,7 @@ module LinkChecker
|
|
|
119
151
|
def _handle_result(result)
|
|
120
152
|
@result = result
|
|
121
153
|
result.redirects = @hops.dup.freeze unless result.redirect?
|
|
154
|
+
refresh_to = _meta_refresh_target(result)
|
|
122
155
|
retry_text = " (#{_retries_left_s})" if retry? && (result.error? || result.failure?)
|
|
123
156
|
retry_text = ' (rate limited)' if !first_time? && @rate_limit_retries_left.positive? && checker.rate_limited?(result)
|
|
124
157
|
logger.info "#{' ' * (redirects.count - 1)}#{result}#{retry_text}"
|
|
@@ -128,18 +161,12 @@ module LinkChecker
|
|
|
128
161
|
end
|
|
129
162
|
if result.redirect?
|
|
130
163
|
redirect! result
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if redirects.length - 1 >= checker.max_redirects
|
|
138
|
-
raise LinkChecker::Errors::RedirectLimitError.new(checker.max_redirects, redirects + [redirected_to_uri])
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
redirects << redirected_to_uri
|
|
142
|
-
_queue_task(redirected_to_uri, result.method, result.uri, options)
|
|
164
|
+
_follow!(result, URI.join(result.result_uri, result.redirect_to), result.code)
|
|
165
|
+
elsif result.invalid_meta_refresh?
|
|
166
|
+
failure! result
|
|
167
|
+
elsif refresh_to
|
|
168
|
+
logger.info "#{' ' * (redirects.count - 1)}#{result.result_uri} : META REFRESH (#{refresh_to})"
|
|
169
|
+
_follow!(result, refresh_to, nil)
|
|
143
170
|
elsif result.success?
|
|
144
171
|
success! result
|
|
145
172
|
else
|
|
@@ -30,6 +30,10 @@ module LinkChecker
|
|
|
30
30
|
request.options[:headers]
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
+
def body
|
|
34
|
+
response&.body
|
|
35
|
+
end
|
|
36
|
+
|
|
33
37
|
def response_headers
|
|
34
38
|
response&.headers || {}
|
|
35
39
|
end
|
|
@@ -46,7 +50,7 @@ module LinkChecker
|
|
|
46
50
|
[301, 302, 303, 307, 308].include?(code)
|
|
47
51
|
end
|
|
48
52
|
|
|
49
|
-
def
|
|
53
|
+
def http_success?
|
|
50
54
|
return false unless response
|
|
51
55
|
|
|
52
56
|
code >= 200 && code <= 299
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LinkChecker
|
|
4
|
+
# Built-in url_rewrites rules.
|
|
5
|
+
module UrlRewrites
|
|
6
|
+
# A rewritten URL and the headers to send with it.
|
|
7
|
+
Target = Data.define(:uri, :headers)
|
|
8
|
+
# The targets that a rule rewrote a URL to, and whether to check the URL itself when one fails.
|
|
9
|
+
Rewrite = Data.define(:targets, :fallback)
|
|
10
|
+
|
|
11
|
+
# Checks npm package pages and npm trends comparisons, which challenge bots, with the npm registry.
|
|
12
|
+
module NPM
|
|
13
|
+
ACCEPT = 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*'
|
|
14
|
+
PACKAGE = %r{\A/(?:package/)?(@[^/]+/[^/]+|[^/@~][^/]*)/?\z}
|
|
15
|
+
COMPARISON = %r{\A/(.+?)/?\z}
|
|
16
|
+
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
# The registry URLs of the packages of uri, with their headers, or nil when uri isn't an npm page.
|
|
20
|
+
def call(uri)
|
|
21
|
+
return unless uri.is_a?(URI::HTTP)
|
|
22
|
+
|
|
23
|
+
names = case uri.host&.downcase
|
|
24
|
+
when 'npmjs.com', 'www.npmjs.com' then PACKAGE.match(uri.path)&.captures
|
|
25
|
+
when 'npmtrends.com', 'www.npmtrends.com' then COMPARISON.match(uri.path)&.[](1)&.split('-vs-')
|
|
26
|
+
end
|
|
27
|
+
names&.map { |name| ["https://registry.npmjs.org/#{name}", { 'Accept' => ACCEPT }] }
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
module_function
|
|
32
|
+
|
|
33
|
+
# The rewrite of uri by the first rule that matches it, or nil.
|
|
34
|
+
def rewrite(rules, uri)
|
|
35
|
+
rules.each do |rule|
|
|
36
|
+
rewrite = rule.respond_to?(:call) ? _call(rule, uri) : _rule(rule, uri)
|
|
37
|
+
return rewrite if rewrite
|
|
38
|
+
end
|
|
39
|
+
nil
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Whether rule is a callable or a Hash with a Regexp :match, a String :to, and optional :headers and :fallback.
|
|
43
|
+
def valid_rule?(rule)
|
|
44
|
+
return true if rule.respond_to?(:call)
|
|
45
|
+
return false unless rule.is_a?(Hash) && (rule.keys - %i[match to headers fallback]).empty?
|
|
46
|
+
|
|
47
|
+
rule[:match].is_a?(Regexp) && rule[:to].is_a?(String) && [nil, true, false].include?(rule[:fallback]) &&
|
|
48
|
+
(rule[:headers].nil? || (rule[:headers].is_a?(Hash) && rule[:headers].all? { |key, value| _header?(key, value) }))
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def _rule(rule, uri)
|
|
52
|
+
url = uri.to_s
|
|
53
|
+
return unless rule[:match].match?(url)
|
|
54
|
+
|
|
55
|
+
_rewrite(uri, [[url.sub(rule[:match], rule[:to]), rule[:headers] || {}]], rule[:fallback] == true)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def _call(rule, uri)
|
|
59
|
+
value = rule.call(uri)
|
|
60
|
+
return if value.nil?
|
|
61
|
+
|
|
62
|
+
value = [value] unless value.is_a?(Array) && !(value.size == 2 && value.last.is_a?(Hash))
|
|
63
|
+
_rewrite(uri, value.map { |target| target.is_a?(Array) ? target : [target, {}] }, false)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def _rewrite(uri, targets, fallback)
|
|
67
|
+
return if targets.empty?
|
|
68
|
+
|
|
69
|
+
Rewrite.new(targets: targets.map { |url, headers| _target(uri, url, headers) }.freeze, fallback: fallback)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def _target(uri, url, headers)
|
|
73
|
+
parsed = URI(url.to_s)
|
|
74
|
+
unless parsed.is_a?(URI::HTTP) && parsed.host && headers.is_a?(Hash) && headers.all? { |key, value| _header?(key, value) }
|
|
75
|
+
raise ArgumentError, "Invalid URL rewrite of #{uri}: #{url.inspect}, #{headers.inspect}."
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
Target.new(uri: parsed, headers: headers.to_h { |key, value| [key.to_s, value] }.freeze)
|
|
79
|
+
rescue URI::InvalidURIError
|
|
80
|
+
raise ArgumentError, "Invalid URL rewrite of #{uri}: #{url.inspect}."
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def _header?(key, value)
|
|
84
|
+
(key.is_a?(String) || key.is_a?(Symbol)) && value.is_a?(String)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
data/lib/ruby-link-checker.rb
CHANGED
|
@@ -9,6 +9,8 @@ require_relative 'ruby-link-checker/task'
|
|
|
9
9
|
require_relative 'ruby-link-checker/tasks'
|
|
10
10
|
require_relative 'ruby-link-checker/checker'
|
|
11
11
|
require_relative 'ruby-link-checker/redirect'
|
|
12
|
+
require_relative 'ruby-link-checker/url_rewrites'
|
|
13
|
+
require_relative 'ruby-link-checker/html'
|
|
12
14
|
require_relative 'ruby-link-checker/result'
|
|
13
15
|
require_relative 'ruby-link-checker/cache'
|
|
14
16
|
require_relative 'ruby-link-checker/net/http'
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ruby-link-checker
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.7.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Daniel Doubrovkine
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-29 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: logger
|
|
@@ -76,6 +76,7 @@ files:
|
|
|
76
76
|
- lib/ruby-link-checker/errors/connection_error.rb
|
|
77
77
|
- lib/ruby-link-checker/errors/redirect_limit_error.rb
|
|
78
78
|
- lib/ruby-link-checker/errors/redirect_loop_error.rb
|
|
79
|
+
- lib/ruby-link-checker/html.rb
|
|
79
80
|
- lib/ruby-link-checker/logger.rb
|
|
80
81
|
- lib/ruby-link-checker/net/http.rb
|
|
81
82
|
- lib/ruby-link-checker/net/http/checker.rb
|
|
@@ -89,6 +90,7 @@ files:
|
|
|
89
90
|
- lib/ruby-link-checker/typhoeus/hydra/checker.rb
|
|
90
91
|
- lib/ruby-link-checker/typhoeus/hydra/config.rb
|
|
91
92
|
- lib/ruby-link-checker/typhoeus/hydra/result.rb
|
|
93
|
+
- lib/ruby-link-checker/url_rewrites.rb
|
|
92
94
|
- lib/ruby-link-checker/version.rb
|
|
93
95
|
- lib/ruby_link_checker.rb
|
|
94
96
|
homepage: http://github.com/dblock/ruby-link-checker
|