ruby-link-checker 0.6.0 → 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 +13 -0
- data/README.md +264 -323
- 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 +189 -54
- 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
data/README.md
CHANGED
|
@@ -1,80 +1,70 @@
|
|
|
1
|
-
<
|
|
2
|
-
<source media="(prefers-color-scheme: dark)" srcset="assets/wordmark-dark.svg">
|
|
3
|
-
<img alt="ruby-link-checker" src="assets/wordmark.svg" width="375">
|
|
4
|
-
</picture>
|
|
1
|
+
<img alt="ruby-link-checker" src="assets/logo.svg" width="128">
|
|
5
2
|
|
|
6
3
|
Fast Ruby Link Checker
|
|
7
4
|
======================
|
|
8
5
|
|
|
9
6
|
[](http://badge.fury.io/rb/ruby-link-checker)
|
|
10
7
|
[](https://github.com/dblock/ruby-link-checker/actions/workflows/test.yml)
|
|
11
|
-
[](https://coveralls.io/github/dblock/ruby-link-checker?branch=main)
|
|
12
9
|
|
|
13
|
-
A fast Ruby link checker with support for multiple HTTP libraries.
|
|
10
|
+
A fast Ruby link checker with support for multiple HTTP libraries. It checks that links work, follows redirects and meta refreshes, and verifies `#fragments`. Anecdotal benchmarking on an M1 Mac checks about 50 URLs per second with `LinkChecker::Typhoeus::Hydra`.
|
|
14
11
|
|
|
15
12
|
## Table of Contents
|
|
16
13
|
|
|
14
|
+
- [Installation](#installation)
|
|
17
15
|
- [Usage](#usage)
|
|
18
|
-
- [Dependencies](#dependencies)
|
|
19
|
-
- [Basic Usage](#basic-usage)
|
|
20
16
|
- [Passing Options](#passing-options)
|
|
21
|
-
- [Checkers](#checkers)
|
|
22
|
-
- [LinkChecker::Typhoeus::Hydra](#linkcheckertyphoeushydra)
|
|
23
|
-
- [LinkChecker::Async::HTTP](#linkcheckerasynchttp)
|
|
24
|
-
- [LinkChecker::Net::HTTP](#linkcheckernethttp)
|
|
25
|
-
- [Options](#options)
|
|
26
|
-
- [Retries](#retries)
|
|
27
|
-
- [Redirects](#redirects)
|
|
28
|
-
- [Retry After](#retry-after)
|
|
29
|
-
- [Retry Policy](#retry-policy)
|
|
30
|
-
- [Rate Limits](#rate-limits)
|
|
31
|
-
- [Blocked Results](#blocked-results)
|
|
32
|
-
- [Per-Host Limits](#per-host-limits)
|
|
33
|
-
- [Results](#results)
|
|
34
|
-
- [Methods](#methods)
|
|
35
|
-
- [Deduplication](#deduplication)
|
|
36
|
-
- [Caching](#caching)
|
|
37
|
-
- [Logger](#logger)
|
|
38
|
-
- [User-Agent](#user-agent)
|
|
39
|
-
- [Headers](#headers)
|
|
40
|
-
- [Global Configuration](#global-configuration)
|
|
41
17
|
- [Callbacks and Events](#callbacks-and-events)
|
|
18
|
+
- [Results](#results)
|
|
19
|
+
- [Checkers](#checkers)
|
|
20
|
+
- [LinkChecker::Typhoeus::Hydra](#linkcheckertyphoeushydra)
|
|
21
|
+
- [LinkChecker::Async::HTTP](#linkcheckerasynchttp)
|
|
22
|
+
- [LinkChecker::Net::HTTP](#linkcheckernethttp)
|
|
23
|
+
- [Configuration](#configuration)
|
|
24
|
+
- [Methods](#methods)
|
|
25
|
+
- [Redirects](#redirects)
|
|
26
|
+
- [Meta Refresh](#meta-refresh)
|
|
27
|
+
- [Fragments](#fragments)
|
|
28
|
+
- [URL Rewrites](#url-rewrites)
|
|
29
|
+
- [Retries](#retries)
|
|
30
|
+
- [Rate Limits](#rate-limits)
|
|
31
|
+
- [Blocked Results](#blocked-results)
|
|
32
|
+
- [Per-Host Limits](#per-host-limits)
|
|
33
|
+
- [Deduplication](#deduplication)
|
|
34
|
+
- [Caching](#caching)
|
|
35
|
+
- [Headers](#headers)
|
|
36
|
+
- [Logger](#logger)
|
|
42
37
|
- [Benchmarks](#benchmarks)
|
|
43
38
|
- [Contributing](#contributing)
|
|
44
39
|
- [Copyright and License](#copyright-and-license)
|
|
45
40
|
|
|
46
|
-
##
|
|
47
|
-
|
|
48
|
-
### Dependencies
|
|
41
|
+
## Installation
|
|
49
42
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
Add `typhoeus` and `ruby-link-checker` to your `Gemfile` and run `bundle install`.
|
|
43
|
+
Add `ruby-link-checker` and the HTTP library of a [checker](#checkers) to your `Gemfile`, and run `bundle install`. [`LinkChecker::Typhoeus::Hydra`](#linkcheckertyphoeushydra) is recommended.
|
|
53
44
|
|
|
54
45
|
```ruby
|
|
55
46
|
gem 'typhoeus'
|
|
56
47
|
gem 'ruby-link-checker'
|
|
57
48
|
```
|
|
58
49
|
|
|
59
|
-
|
|
50
|
+
## Usage
|
|
60
51
|
|
|
61
52
|
```ruby
|
|
62
53
|
require 'typhoeus'
|
|
63
54
|
require 'ruby-link-checker'
|
|
64
55
|
|
|
65
|
-
# create a new checker instance
|
|
66
56
|
checker = LinkChecker::Typhoeus::Hydra::Checker.new
|
|
67
57
|
|
|
68
|
-
# queue URLs to check
|
|
69
|
-
links = [...]
|
|
70
58
|
links.each do |url|
|
|
71
|
-
checker.check
|
|
59
|
+
checker.check(url)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
checker.on :failure, :error do |result|
|
|
63
|
+
puts "FAIL: #{result}"
|
|
72
64
|
end
|
|
73
65
|
|
|
74
|
-
#
|
|
75
|
-
checker.run
|
|
66
|
+
checker.run # blocks until every link is checked
|
|
76
67
|
|
|
77
|
-
# display buckets of results
|
|
78
68
|
checker.results.each_pair do |bucket, results|
|
|
79
69
|
puts "#{bucket}: #{results.size}"
|
|
80
70
|
end
|
|
@@ -82,57 +72,115 @@ end
|
|
|
82
72
|
|
|
83
73
|
### Passing Options
|
|
84
74
|
|
|
85
|
-
|
|
75
|
+
Options passed to `check` are returned with every result of that link.
|
|
86
76
|
|
|
87
77
|
```ruby
|
|
88
|
-
checker.check
|
|
78
|
+
checker.check('https://www.example.org', location: 'page.html')
|
|
89
79
|
|
|
90
80
|
checker.on :success do |result|
|
|
91
|
-
result.options #
|
|
81
|
+
result.options # => { location: 'page.html' }
|
|
82
|
+
end
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Callbacks and Events
|
|
86
|
+
|
|
87
|
+
Events publish results as they become available.
|
|
88
|
+
|
|
89
|
+
```ruby
|
|
90
|
+
checker.on :complete do |result|
|
|
91
|
+
puts result # the final result of each link
|
|
92
92
|
end
|
|
93
93
|
```
|
|
94
94
|
|
|
95
|
-
|
|
95
|
+
Exactly one of `:success`, `:failure`, `:blocked`, or `:error` fires for each link, followed by `:complete` with the same result. Report links from those events: `:result` also fires for every redirect hop and for a failed `HEAD` that is followed by a `GET`, so treating every `:result` that isn't a success as a broken link reports redirects and fallbacks as broken.
|
|
96
|
+
|
|
97
|
+
| Event | Fires | Description |
|
|
98
|
+
|-----------|-----------------------|-------------------------------------------------------------------------------------|
|
|
99
|
+
| :result | Once per attempt | A response or error, including redirects and attempts that are retried or followed by another method. |
|
|
100
|
+
| :redirect | Once per redirect hop | A redirect that is being followed. |
|
|
101
|
+
| :retry | Once per retry | A request that is being retried. |
|
|
102
|
+
| :success | Once per link | A working link, usually a 2xx response. |
|
|
103
|
+
| :failure | Once per link | A broken link, usually a 4xx or 5xx response. |
|
|
104
|
+
| :blocked | Once per link | A link blocked by bot protection, see [Blocked Results](#blocked-results). |
|
|
105
|
+
| :error | Once per link | An error, such as an invalid URL or a timeout. |
|
|
106
|
+
| :complete | Once per link | The final result, after the outcome event. |
|
|
107
|
+
|
|
108
|
+
For example, a link that redirects once to a page that answers `HEAD` with 405 and `GET` with 200 publishes these events, in order.
|
|
109
|
+
|
|
110
|
+
| Event | Method | Code |
|
|
111
|
+
|-----------|--------|------|
|
|
112
|
+
| :result | HEAD | 301 |
|
|
113
|
+
| :redirect | HEAD | 301 |
|
|
114
|
+
| :result | HEAD | 405 |
|
|
115
|
+
| :result | GET | 301 |
|
|
116
|
+
| :redirect | GET | 301 |
|
|
117
|
+
| :result | GET | 200 |
|
|
118
|
+
| :success | GET | 200 |
|
|
119
|
+
| :complete | GET | 200 |
|
|
96
120
|
|
|
97
|
-
|
|
121
|
+
### Results
|
|
98
122
|
|
|
99
|
-
|
|
123
|
+
Checkers collect final results by outcome. Pass `results: false` to only publish events.
|
|
100
124
|
|
|
101
125
|
```ruby
|
|
102
|
-
|
|
103
|
-
|
|
126
|
+
checker.results # => { success: [...], failure: [...], blocked: [...], error: [...] }
|
|
127
|
+
```
|
|
104
128
|
|
|
105
|
-
|
|
106
|
-
checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
107
|
-
hydra: {
|
|
108
|
-
# lower than the Typhoeus default of 200, seems to start breaking around 50+
|
|
109
|
-
max_concurrency: 25
|
|
110
|
-
}
|
|
111
|
-
)
|
|
129
|
+
Results have the following properties. See [result.rb](lib/ruby-link-checker/result.rb) for details.
|
|
112
130
|
|
|
113
|
-
|
|
114
|
-
|
|
131
|
+
| Property | Description |
|
|
132
|
+
|----------------------|-------------------------------------------------------------------------------------|
|
|
133
|
+
| uri | The submitted URL. |
|
|
134
|
+
| result_uri | The last URL, after redirects. |
|
|
135
|
+
| method | The HTTP method. |
|
|
136
|
+
| code | The HTTP status code. |
|
|
137
|
+
| request_headers | The request headers. |
|
|
138
|
+
| response_headers | The response headers, accessible with `[]`. |
|
|
139
|
+
| redirect_to | The target of a redirect. |
|
|
140
|
+
| redirects | The redirect hops that led to the result, see [Redirects](#redirects). |
|
|
141
|
+
| permanent_redirect? | Whether any of those hops was a permanent redirect (301 or 308). |
|
|
142
|
+
| error | The error raised, for errors. |
|
|
143
|
+
| error_type | `:timeout`, `:connection`, or `:other` for errors, otherwise `nil`. |
|
|
144
|
+
| cached? | Whether the result was reused from an earlier check. |
|
|
145
|
+
| blocked? | Whether the final result is blocked. |
|
|
146
|
+
| missing_fragment | The fragment that the page lacks, see [Fragments](#fragments). |
|
|
147
|
+
| invalid_meta_refresh | The URL of an invalid meta refresh, see [Meta Refresh](#meta-refresh). |
|
|
148
|
+
| rewritten_uris | The URLs checked in place of the link, see [URL Rewrites](#url-rewrites). |
|
|
149
|
+
| outcome | `:success`, `:failure`, `:blocked`, or `:error`, or `:redirect` for a redirect hop. |
|
|
150
|
+
| reason | A stable explanation of the outcome. |
|
|
151
|
+
| to_h | A serializable summary. |
|
|
115
152
|
|
|
116
|
-
|
|
117
|
-
links.each do |url|
|
|
118
|
-
checker.check url
|
|
119
|
-
end
|
|
153
|
+
`result.reason` is one of the following.
|
|
120
154
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
155
|
+
* `HTTP <status>`, such as `HTTP 404`.
|
|
156
|
+
* `<name> challenge (HTTP <status>)` for a response with a known bot challenge, such as `Cloudflare challenge (HTTP 403)`.
|
|
157
|
+
* `Missing fragment #<fragment> (HTTP <status>)` for a page without the link's [fragment](#fragments).
|
|
158
|
+
* `Invalid meta refresh URL (HTTP <status>)` for a page whose [meta refresh](#meta-refresh) isn't a valid URL.
|
|
159
|
+
* `Timeout`, `Connection error`, `Redirect loop`, `Too many redirects`, or `Invalid URL`.
|
|
160
|
+
* `Error (<class name>)` for other errors.
|
|
125
161
|
|
|
126
|
-
|
|
127
|
-
checker.run
|
|
162
|
+
`result.to_h` returns a hash with `url`, `result_url`, `method`, `code` (`nil` for errors), `outcome`, `reason`, `redirects` (each with `url`, `code`, and `to`), `permanent_redirect`, `blocked`, `challenge` (such as `Cloudflare`, or `nil`), `cached`, `error` (the message), `error_type`, `missing_fragment`, `rewritten_urls`, and the `options` passed to `check`. It's suitable for JSON reports when those options are.
|
|
128
163
|
|
|
129
|
-
|
|
130
|
-
checker.
|
|
131
|
-
puts
|
|
164
|
+
```ruby
|
|
165
|
+
checker.on :complete do |result|
|
|
166
|
+
puts JSON.generate(result.to_h) unless result.success?
|
|
132
167
|
end
|
|
133
168
|
```
|
|
134
169
|
|
|
135
|
-
|
|
170
|
+
## Checkers
|
|
171
|
+
|
|
172
|
+
### [LinkChecker::Typhoeus::Hydra](lib/ruby-link-checker/typhoeus/hydra/checker.rb)
|
|
173
|
+
|
|
174
|
+
A concurrent checker that uses [Typhoeus](https://typhoeus.github.io/). `checker.run` checks the queued links and blocks until they're done. Hydra's default `max_concurrency` of 200 tends to cause errors above about 50.
|
|
175
|
+
|
|
176
|
+
```ruby
|
|
177
|
+
require 'typhoeus'
|
|
178
|
+
require 'ruby-link-checker'
|
|
179
|
+
|
|
180
|
+
checker = LinkChecker::Typhoeus::Hydra::Checker.new(hydra: { max_concurrency: 25 }, timeout: 5)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
Configure timeouts per checker, as above, or for every Hydra checker.
|
|
136
184
|
|
|
137
185
|
```ruby
|
|
138
186
|
LinkChecker::Typhoeus::Hydra.configure do |config|
|
|
@@ -141,89 +189,40 @@ LinkChecker::Typhoeus::Hydra.configure do |config|
|
|
|
141
189
|
end
|
|
142
190
|
```
|
|
143
191
|
|
|
144
|
-
|
|
192
|
+
### [LinkChecker::Async::HTTP](lib/ruby-link-checker/async/http/checker.rb)
|
|
145
193
|
|
|
146
|
-
|
|
194
|
+
A concurrent checker that uses [async-http](https://github.com/socketry/async-http), and reuses a connection pool per scheme, host, and port. `checker.run` checks the queued links and blocks until they're done. Its concurrency is unlimited by default: set `max_concurrency` to bound active requests, which doesn't count delayed retries. Set `protocol` to `:http1` or `:http2` to force a protocol version instead of negotiating it.
|
|
147
195
|
|
|
148
196
|
```ruby
|
|
149
197
|
require 'async'
|
|
150
198
|
require 'async/http'
|
|
151
199
|
require 'ruby-link-checker'
|
|
152
200
|
|
|
153
|
-
|
|
154
|
-
checker = LinkChecker::Async::HTTP::Checker.new
|
|
155
|
-
|
|
156
|
-
# log requests and response codes
|
|
157
|
-
checker.logger.level = Logger::INFO
|
|
158
|
-
|
|
159
|
-
links = [...] # array of URLs
|
|
160
|
-
links.each do |url|
|
|
161
|
-
checker.check url
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
# examine failures and errors as they come
|
|
165
|
-
checker.on :error, :failure do |result|
|
|
166
|
-
puts "FAIL: #{result}"
|
|
167
|
-
end
|
|
168
|
-
|
|
169
|
-
# runs all queued checks concurrently, will block until all requests have completed
|
|
170
|
-
checker.run
|
|
171
|
-
|
|
172
|
-
# examine results
|
|
173
|
-
checker.results.each_pair do |bucket, results|
|
|
174
|
-
puts "#{bucket}: #{results.size}"
|
|
175
|
-
end
|
|
201
|
+
checker = LinkChecker::Async::HTTP::Checker.new(max_concurrency: 25, protocol: :http2)
|
|
176
202
|
```
|
|
177
203
|
|
|
178
|
-
|
|
204
|
+
Configure timeouts and the protocol per checker, or for every Async checker.
|
|
179
205
|
|
|
180
206
|
```ruby
|
|
181
207
|
LinkChecker::Async::HTTP.configure do |config|
|
|
182
208
|
config.read_timeout = 5
|
|
183
209
|
config.open_timeout = 10
|
|
210
|
+
config.protocol = :http1
|
|
184
211
|
end
|
|
185
212
|
```
|
|
186
213
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
```ruby
|
|
190
|
-
LinkChecker::Async::HTTP.configure do |config|
|
|
191
|
-
config.protocol = :http2
|
|
192
|
-
end
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
Async::HTTP reuses connection-pooling clients per scheme/host/port. Its default concurrency is unlimited; set `max_concurrency` to bound active requests without counting delayed retries against that limit.
|
|
214
|
+
### [LinkChecker::Net::HTTP](lib/ruby-link-checker/net/http/checker.rb)
|
|
196
215
|
|
|
197
|
-
|
|
198
|
-
checker = LinkChecker::Async::HTTP::Checker.new(max_concurrency: 25)
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
#### [LinkChecker::Net::HTTP](lib/ruby-link-checker/net/http/checker.rb)
|
|
202
|
-
|
|
203
|
-
Slow, sequential checker. It checks each link when it's submitted; call `checker.run` afterwards to finish the run, for example to [prune the cache](#caching).
|
|
216
|
+
A slow, sequential checker that checks each link when it's submitted. Call `checker.run` afterwards to finish the run, for example to [prune the cache](#caching).
|
|
204
217
|
|
|
205
218
|
```ruby
|
|
206
219
|
require 'net/http'
|
|
207
220
|
require 'ruby-link-checker'
|
|
208
221
|
|
|
209
|
-
|
|
210
|
-
checker = LinkChecker::Net::HTTP::Checker.new
|
|
211
|
-
|
|
212
|
-
# log requests and response codes
|
|
213
|
-
checker.logger.level = Logger::INFO
|
|
214
|
-
|
|
215
|
-
links = [...] # array of URLs
|
|
216
|
-
links.each do |url|
|
|
217
|
-
checker.check url
|
|
218
|
-
end
|
|
219
|
-
|
|
220
|
-
# examine results
|
|
221
|
-
checker.results.each_pair do |bucket, results|
|
|
222
|
-
puts "#{bucket}: #{results.size}"
|
|
223
|
-
end
|
|
222
|
+
checker = LinkChecker::Net::HTTP::Checker.new(read_timeout: 5)
|
|
224
223
|
```
|
|
225
224
|
|
|
226
|
-
|
|
225
|
+
Configure timeouts per checker, as above, or for every Net::HTTP checker.
|
|
227
226
|
|
|
228
227
|
```ruby
|
|
229
228
|
LinkChecker::Net::HTTP.configure do |config|
|
|
@@ -232,25 +231,36 @@ LinkChecker::Net::HTTP.configure do |config|
|
|
|
232
231
|
end
|
|
233
232
|
```
|
|
234
233
|
|
|
235
|
-
|
|
234
|
+
## Configuration
|
|
235
|
+
|
|
236
|
+
Pass options to a checker, or configure them for every checker.
|
|
237
|
+
|
|
238
|
+
```ruby
|
|
239
|
+
checker = LinkChecker::Net::HTTP::Checker.new(user_agent: 'Custom Agent/1.0')
|
|
240
|
+
|
|
241
|
+
LinkChecker.configure do |config|
|
|
242
|
+
config.user_agent = 'Custom Agent/1.0'
|
|
243
|
+
config.methods = %w[HEAD GET]
|
|
244
|
+
end
|
|
245
|
+
```
|
|
236
246
|
|
|
237
|
-
|
|
247
|
+
### Methods
|
|
238
248
|
|
|
239
|
-
|
|
249
|
+
Checkers request each link with `HEAD`, then with `GET` if `HEAD` fails. Set `methods` to change that, for example to `%w[HEAD]` to never send `GET`. Links whose [fragments](#fragments) are checked, and every link with [`follow_meta_refresh`](#meta-refresh), skip `HEAD`.
|
|
240
250
|
|
|
241
251
|
```ruby
|
|
242
|
-
checker = LinkChecker::Net::HTTP::Checker.new(
|
|
252
|
+
checker = LinkChecker::Net::HTTP::Checker.new(methods: %w[HEAD])
|
|
243
253
|
```
|
|
244
254
|
|
|
245
|
-
|
|
255
|
+
### Redirects
|
|
246
256
|
|
|
247
|
-
Checkers follow up to 20 redirects per attempt
|
|
257
|
+
Checkers follow up to 20 redirects per attempt, including [meta refreshes](#meta-refresh), resolve relative redirects against the current URL, and detect redirect loops. Set `max_redirects` to change the limit, or to `0` to not follow redirects.
|
|
248
258
|
|
|
249
259
|
```ruby
|
|
250
260
|
checker = LinkChecker::Typhoeus::Hydra::Checker.new(max_redirects: 10)
|
|
251
261
|
```
|
|
252
262
|
|
|
253
|
-
|
|
263
|
+
`result.redirects` lists the hops of the final attempt, oldest first, as `LinkChecker::Redirect` objects with the responding `uri`, its status `code`, the target `to`, and `permanent?` for 301 and 308. When `HEAD` fails after redirects and `GET` is tried, it lists the `GET` hops. Redirect loop and limit errors include the hops that led to them. Use `result.permanent_redirect?` to find links to update.
|
|
254
264
|
|
|
255
265
|
```ruby
|
|
256
266
|
checker.on :success do |result|
|
|
@@ -258,151 +268,180 @@ checker.on :success do |result|
|
|
|
258
268
|
end
|
|
259
269
|
```
|
|
260
270
|
|
|
261
|
-
|
|
271
|
+
### Meta Refresh
|
|
262
272
|
|
|
263
|
-
|
|
273
|
+
Pages can redirect with `<meta http-equiv="refresh" content="0; url=/new">` instead of an HTTP redirect. Checkers follow the first valid refresh of an HTML page whose body they read, whatever its delay, and parse `content` as browsers do, so `5, URL='/new'` and `0; /new` also redirect. They ignore refreshes without a URL, which reload the page, and refreshes inside `<noscript>`.
|
|
274
|
+
|
|
275
|
+
Checkers only read the body of `GET` responses, such as those of links with a [fragment](#fragments) or of a `GET` after a failed `HEAD`. Set `follow_meta_refresh: true` to request every link with `GET`, so that every refresh is followed.
|
|
264
276
|
|
|
265
277
|
```ruby
|
|
266
|
-
checker = LinkChecker::
|
|
267
|
-
|
|
268
|
-
retry_after: 1,
|
|
269
|
-
retry_backoff: 2,
|
|
270
|
-
retry_jitter: 0.1,
|
|
271
|
-
respect_retry_after: true,
|
|
272
|
-
max_retry_delay: 60
|
|
273
|
-
)
|
|
278
|
+
checker = LinkChecker::Net::HTTP::Checker.new(follow_meta_refresh: true)
|
|
279
|
+
checker.check('https://www.example.org/old') # follows a meta refresh to /new
|
|
274
280
|
```
|
|
275
281
|
|
|
276
|
-
|
|
282
|
+
A refresh counts as a redirect for `max_redirects` and loop detection, and appears in `result.redirects` with a `nil` `code` and `meta_refresh? == true`; it's never `permanent?`. A refresh to a URL that isn't valid HTTP(S), such as `mailto:`, fails with the page's status: `result.invalid_meta_refresh?` is `true`, `result.invalid_meta_refresh` is the URL as written, and `result.reason` is, for example, `Invalid meta refresh URL (HTTP 200)`.
|
|
277
283
|
|
|
278
|
-
|
|
284
|
+
### Fragments
|
|
279
285
|
|
|
280
|
-
|
|
286
|
+
Checkers verify that the page of a link with a `#fragment` has an element with that `id`, or an `<a>` element with that `name`, so such links are requested with `GET` instead of `HEAD`. A link to a missing fragment fails with the page's status: `result.missing_fragment?` is `true`, `result.missing_fragment` is the fragment without `#`, and `result.reason` is, for example, `Missing fragment #install (HTTP 200)`.
|
|
281
287
|
|
|
282
288
|
```ruby
|
|
283
|
-
checker = LinkChecker::Net::HTTP::Checker.new
|
|
289
|
+
checker = LinkChecker::Net::HTTP::Checker.new
|
|
290
|
+
checker.on :failure do |result|
|
|
291
|
+
puts "#{result.uri} has no ##{result.missing_fragment}" if result.missing_fragment?
|
|
292
|
+
end
|
|
293
|
+
checker.check('https://github.com/dblock/ruby-link-checker#usage') # passes
|
|
294
|
+
checker.check('https://github.com/dblock/ruby-link-checker#nope') # fails
|
|
284
295
|
```
|
|
285
296
|
|
|
286
|
-
|
|
297
|
+
Fragments are percent-decoded, so `#caf%C3%A9` matches `id="café"`, and links to GitHub's rendered Markdown headings, such as `#usage`, match its `user-content-usage` ids. The fragment is checked on the final page after redirects, unless a redirect `Location` has its own fragment, which replaces the submitted one, as in browsers. Pages are scanned for start tags without running JavaScript. The following links pass without checking their fragment.
|
|
287
298
|
|
|
288
|
-
|
|
299
|
+
* Empty fragments and `#top`.
|
|
300
|
+
* [Text fragments](https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Fragment/Text_fragments), such as `#:~:text=install`; in `#install:~:text=gem`, `install` is checked.
|
|
301
|
+
* Pages that aren't `text/html` or `application/xhtml+xml`, such as a PDF linked with `#page=3`.
|
|
302
|
+
* Every link with `methods: %w[HEAD]`.
|
|
289
303
|
|
|
290
|
-
|
|
304
|
+
Set `fragments_to_skip` to skip the anchors that JavaScript adds, such as those of single-page apps. It's a Hash of URL patterns to fragment patterns, each a String that must match exactly or a Regexp. URLs are matched without their fragment, and fragments both decoded and as written. Skipped links are checked with `HEAD`, as if they had no fragment. By default, it skips GitHub's source line anchors, such as `https://github.com/dblock/ruby-link-checker/blob/main/README.md#L10`, which only JavaScript renders.
|
|
291
305
|
|
|
292
306
|
```ruby
|
|
293
|
-
checker = LinkChecker::
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
307
|
+
checker = LinkChecker::Net::HTTP::Checker.new(
|
|
308
|
+
fragments_to_skip: LinkChecker::Config::DEFAULT_FRAGMENTS_TO_SKIP.merge(
|
|
309
|
+
%r{\Ahttps://app\.example\.com/} => /./,
|
|
310
|
+
'https://www.example.org/docs' => 'settings'
|
|
311
|
+
)
|
|
298
312
|
)
|
|
299
313
|
```
|
|
300
314
|
|
|
301
|
-
|
|
315
|
+
Set `check_fragments: false` to ignore fragments.
|
|
302
316
|
|
|
303
|
-
|
|
317
|
+
```ruby
|
|
318
|
+
checker = LinkChecker::Net::HTTP::Checker.new(check_fragments: false)
|
|
319
|
+
```
|
|
304
320
|
|
|
305
|
-
|
|
321
|
+
### URL Rewrites
|
|
306
322
|
|
|
307
|
-
|
|
308
|
-
checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
309
|
-
blocked_status_codes: [403, 999]
|
|
310
|
-
)
|
|
323
|
+
Set `url_rewrites` to check other URLs in place of links, such as the API of a site that blocks link checkers. The first rule that matches a link rewrites it, and other links are checked as usual. Rewrites are off by default.
|
|
311
324
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
325
|
+
`LinkChecker::UrlRewrites::NPM` checks npm packages with the npm registry, which, unlike `www.npmjs.com`, doesn't challenge link checkers. It rewrites `https://www.npmjs.com/package/<name>` and `https://www.npmjs.com/<name>`, including scoped packages such as `@eslint/js`, to `https://registry.npmjs.org/<name>`, and npm trends comparisons, such as `https://npmtrends.com/eslint-vs-prettier`, to every package that they compare.
|
|
326
|
+
|
|
327
|
+
```ruby
|
|
328
|
+
checker = LinkChecker::Net::HTTP::Checker.new(url_rewrites: [LinkChecker::UrlRewrites::NPM])
|
|
329
|
+
checker.check('https://www.npmjs.com/package/eslint') # checks https://registry.npmjs.org/eslint
|
|
315
330
|
```
|
|
316
331
|
|
|
317
|
-
A
|
|
332
|
+
A rule is a Hash whose `match` Regexp and `to` replacement are passed to `String#sub`, so `to` can refer to groups with `\1` or `\k<name>`. Optional `headers` are sent to the host of the rewritten URL, but not to the hosts that it redirects to, and `fallback: true` checks the link itself when the rewritten URL doesn't succeed.
|
|
318
333
|
|
|
319
|
-
|
|
334
|
+
```ruby
|
|
335
|
+
checker = LinkChecker::Net::HTTP::Checker.new(
|
|
336
|
+
url_rewrites: [
|
|
337
|
+
{
|
|
338
|
+
match: %r{\Ahttps://www\.example\.org/wiki/(.+)\z},
|
|
339
|
+
to: 'https://api.example.org/pages/\\1',
|
|
340
|
+
headers: { 'Accept' => 'application/json' },
|
|
341
|
+
fallback: true
|
|
342
|
+
}
|
|
343
|
+
]
|
|
344
|
+
)
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
A rule can also be an object that responds to `call`, such as a lambda. It receives the link's `URI`, and returns `nil` to leave the link alone, a URL, a list of URLs, or `[url, headers]` pairs. `check` raises an `ArgumentError` for rewrites that aren't absolute HTTP(S) URLs.
|
|
320
348
|
|
|
321
349
|
```ruby
|
|
322
|
-
|
|
350
|
+
mirror = ->(uri) { "https://mirror.example.org#{uri.path}" if uri.host == 'www.example.org' }
|
|
351
|
+
checker = LinkChecker::Net::HTTP::Checker.new(url_rewrites: [mirror])
|
|
323
352
|
```
|
|
324
353
|
|
|
325
|
-
|
|
354
|
+
A link rewritten to several URLs succeeds when all of them succeed, and otherwise reports the first one that doesn't. Results keep the submitted link as `result.uri`, with its options, and list every rewritten URL in `result.rewritten_uris`; `result_uri`, `code`, and `redirects` come from the reported URL, or from the link itself after a fallback. Rewritten URLs are [deduplicated](#deduplication) and [cached](#caching) like other links.
|
|
355
|
+
|
|
356
|
+
### Retries
|
|
357
|
+
|
|
358
|
+
By default, checkers only retry [rate-limited responses](#rate-limits). Set `retries` to also retry timeouts, connection errors, and HTTP 408, 425, 429, 500, 502, 503, and 504. Other failures and invalid URLs aren't retried. The `HEAD` to `GET` fallback is independent of retries, which are counted per link: with `retries: 2`, repeated transient failures send `HEAD`, `HEAD`, `HEAD`, then `GET`. Set `retry_status_codes` to choose the retried statuses, or to `nil` to retry every failure and error, including invalid URLs and programming errors.
|
|
326
359
|
|
|
327
|
-
|
|
360
|
+
Retries wait `retry_after` seconds (default `0`), multiplied by `retry_backoff` (default `1`) for each further retry, plus a random fraction of up to `retry_jitter` (default `0`, maximum `1`), and at most `max_retry_delay` seconds (default `60`). With `respect_retry_after: true` (default `false`), a `Retry-After` header, in seconds or as a date, sets a minimum delay with the same cap; invalid and past dates are ignored.
|
|
328
361
|
|
|
329
362
|
```ruby
|
|
330
363
|
checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
331
|
-
|
|
332
|
-
|
|
364
|
+
retries: 2,
|
|
365
|
+
retry_status_codes: [429, 503],
|
|
366
|
+
retry_after: 1,
|
|
367
|
+
retry_backoff: 2,
|
|
368
|
+
retry_jitter: 0.1,
|
|
369
|
+
respect_retry_after: true,
|
|
370
|
+
max_retry_delay: 60
|
|
333
371
|
)
|
|
334
372
|
```
|
|
335
373
|
|
|
336
|
-
|
|
374
|
+
Async checkers wait without blocking other requests. Hydra starts delayed retries after its active batch drains, so delays can be longer than configured. The `HEAD` to `GET` fallback isn't delayed.
|
|
337
375
|
|
|
338
|
-
|
|
376
|
+
### Rate Limits
|
|
339
377
|
|
|
340
|
-
|
|
378
|
+
Rate-limited responses (429) are retried with exponential backoff per host, independently of `retries`. Each one pauses every later request to that host for `rate_limit_backoff` seconds (default `1`), doubling with each further rate-limited response (1s, 2s, 4s, ...), or longer when `Retry-After` asks for it, and at most `max_rate_limit_delay` seconds (default `30`). Rate-limited responses that arrive during a pause, such as the rest of a burst, don't increase the backoff, and any other response from the host resets it.
|
|
341
379
|
|
|
342
|
-
|
|
343
|
-
checker = LinkChecker::Net::HTTP::Checker.new
|
|
344
|
-
...
|
|
345
|
-
checker.run
|
|
380
|
+
Each link is retried up to `rate_limit_retries` times (default `2`), then the [retry policy](#retries) applies, and a link that's still rate limited is [blocked](#blocked-results), or failed if 429 isn't in `blocked_status_codes`. Set `rate_limit_retries: 0` to disable rate limit backoff, and `rate_limit_status_codes` (default `[429]`) to treat other statuses, such as 503, as rate limits. Rate limit retries publish `:retry`.
|
|
346
381
|
|
|
347
|
-
|
|
382
|
+
```ruby
|
|
383
|
+
checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
384
|
+
rate_limit_retries: 3,
|
|
385
|
+
rate_limit_backoff: 2,
|
|
386
|
+
max_rate_limit_delay: 60,
|
|
387
|
+
rate_limit_status_codes: [429, 503]
|
|
388
|
+
)
|
|
348
389
|
```
|
|
349
390
|
|
|
350
|
-
|
|
391
|
+
Pauses use the scheduling of [per-host limits](#per-host-limits). Hydra holds back requests to a paused host until its active batch drains, but can't recall requests that it already started, so combine backoff with `max_concurrency_per_host` to keep bursts small.
|
|
392
|
+
|
|
393
|
+
### Blocked Results
|
|
394
|
+
|
|
395
|
+
Bot protection often answers automated requests with 401, 403, 429, or a non-standard status, such as LinkedIn's 999, although the page exists. Checkers report links whose final status, after retries, redirects, and the `GET` fallback, is in `blocked_status_codes` (default `[401, 403, 429, 999]`) as blocked (unverified) instead of failed. Blocked results publish `:blocked` instead of `:failure`, and are collected in `results[:blocked]`. Errors, such as timeouts, are never blocked. Set `blocked_status_codes: []` to report those statuses as failures, as versions before 0.6.0 did.
|
|
351
396
|
|
|
352
397
|
```ruby
|
|
353
|
-
checker = LinkChecker::
|
|
354
|
-
...
|
|
355
|
-
checker.run
|
|
398
|
+
checker = LinkChecker::Typhoeus::Hydra::Checker.new(blocked_status_codes: [403, 999])
|
|
356
399
|
|
|
357
|
-
checker.
|
|
400
|
+
checker.on :blocked do |result|
|
|
401
|
+
puts "UNVERIFIED: #{result}" # result.blocked? == true, result.failure? == false
|
|
402
|
+
end
|
|
358
403
|
```
|
|
359
404
|
|
|
360
|
-
|
|
405
|
+
With `detect_challenges` (default `true`), unsuccessful responses with a known bot challenge are blocked whatever their status, so with `blocked_status_codes: []`, a plain 403 fails while a challenged one is blocked. It recognizes Cloudflare's [`cf-mitigated: challenge`](https://developers.cloudflare.com/cloudflare-challenges/challenge-types/challenge-pages/detect-response/) header. Set `detect_challenges: false` to report challenges as failures. Blocked results are only cached with a [`blocked_ttl`](#caching).
|
|
361
406
|
|
|
362
|
-
|
|
407
|
+
### Per-Host Limits
|
|
363
408
|
|
|
364
|
-
|
|
409
|
+
Many links to one host arrive as a burst, which bot protection and rate limits react to. Set `max_concurrency_per_host` to bound the requests in flight to one host (default `nil`, unlimited), and `min_host_interval` to the minimum number of seconds between the starts of requests to one host (default `0`). Hosts are compared case-insensitively, ignoring scheme and port, and the limits apply to every request, including redirects and retries.
|
|
365
410
|
|
|
366
411
|
```ruby
|
|
367
|
-
checker = LinkChecker::
|
|
412
|
+
checker = LinkChecker::Typhoeus::Hydra::Checker.new(max_concurrency_per_host: 2, min_host_interval: 0.5)
|
|
368
413
|
```
|
|
369
414
|
|
|
370
|
-
|
|
415
|
+
These limits apply on top of the `max_concurrency` of `LinkChecker::Async::HTTP` and Hydra. Hydra checkers hand requests to Hydra as slots free up, and those that wait for `min_host_interval` once the active batch drains, so intervals can be longer than configured. `LinkChecker::Net::HTTP` is sequential, so only `min_host_interval` changes its behavior. Neither limit affects [deduplication](#deduplication) or [caching](#caching).
|
|
416
|
+
|
|
417
|
+
### Deduplication
|
|
371
418
|
|
|
372
|
-
Set `deduplicate: true` to
|
|
419
|
+
Set `deduplicate: true` to share one check between identical links in flight, and to reuse successes for the lifetime of the checker. Failures and errors are checked again. Each submission still receives its own events and options, including events published before it joined. Deduplication is off by default.
|
|
373
420
|
|
|
374
421
|
```ruby
|
|
375
422
|
checker = LinkChecker::Typhoeus::Hydra::Checker.new(deduplicate: true)
|
|
376
423
|
checker.check('https://www.example.org', location: 'one.html')
|
|
377
424
|
checker.check('https://www.example.org', location: 'two.html')
|
|
378
|
-
checker.run # one check, two sets of
|
|
425
|
+
checker.run # one check, two sets of events
|
|
379
426
|
```
|
|
380
427
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
#### Caching
|
|
428
|
+
Links are identical when their URLs and query values match, along with the transport, methods, headers, host headers, user-agent, and request settings of the checker. The `#fragment`, which isn't sent to servers, isn't compared, so links that only differ by fragment share one check, and each [fragment](#fragments) is checked against the same page. A `GET` that read a page also answers links without a fragment, but a `HEAD` doesn't answer links with one. URLs with user-info credentials are never deduplicated or cached.
|
|
384
429
|
|
|
385
|
-
|
|
430
|
+
### Caching
|
|
386
431
|
|
|
387
|
-
|
|
432
|
+
Set `cache` to a file and a positive `ttl` in seconds to reuse successes across checkers and runs. Failures and errors are never cached. Add a shorter `blocked_ttl` to also reuse [blocked results](#blocked-results), so that bot-walled hosts are still checked regularly, but not on every run; a cached blocked result is only reused while `blocked_status_codes` or `detect_challenges` still block it. Caching is off by default.
|
|
388
433
|
|
|
389
434
|
```ruby
|
|
390
435
|
checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
391
|
-
blocked_status_codes: [401, 403, 429, 999],
|
|
392
436
|
cache: { file: 'tmp/link-checker/cache.json', ttl: 7 * 24 * 3600, blocked_ttl: 24 * 3600 }
|
|
393
437
|
)
|
|
394
|
-
``` Cache writes are atomic and locked, and unreadable or malformed files fall back to normal checking.
|
|
395
|
-
|
|
396
|
-
```ruby
|
|
397
|
-
checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
398
|
-
deduplicate: true,
|
|
399
|
-
cache: { file: 'tmp/link-checker/cache.json', ttl: 3600 }
|
|
400
|
-
)
|
|
401
438
|
```
|
|
402
439
|
|
|
403
|
-
|
|
440
|
+
A cache hit publishes `:result`, `:success` or `:blocked`, and `:complete`, with `cached? == true` and the submission's options, but doesn't replay redirect or retry events. It's a `LinkChecker::CachedResult` with the method, status, submitted and final URLs, and redirect hops, without native requests, responses, or headers. Successful `GET` entries also store the page's anchors, so that a cached page answers links to any of its [fragments](#fragments), and publishes `:failure` for a missing one. Links with a fragment are checked again when the cached success didn't read the page, such as after a `HEAD` or with an entry written before 0.7.0.
|
|
441
|
+
|
|
442
|
+
Writes are atomic and locked, and unreadable or malformed files fall back to checking links. Results whose URLs contain user-info credentials aren't written. Files written before 0.6.0 are ignored and rewritten. Add the cache directory to your ignore rules. Caching alone doesn't share checks in flight; with `deduplicate: true`, successes also stay reusable within a checker after their TTL expires.
|
|
404
443
|
|
|
405
|
-
Entries
|
|
444
|
+
Entries of links that are no longer checked stay in the cache until they expire. Set `prune: true` to remove the entries that a run didn't use when `checker.run` finishes, or call `checker.prune_cache!`, which returns the number of entries removed. Only prune when each run checks every link that should stay cached, not in partial runs, such as incremental builds, or with a cache file shared by checkers of different links.
|
|
406
445
|
|
|
407
446
|
```ruby
|
|
408
447
|
checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
@@ -410,150 +449,52 @@ checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
|
410
449
|
)
|
|
411
450
|
```
|
|
412
451
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
#### Logger
|
|
416
|
-
|
|
417
|
-
Pass your own logger.
|
|
418
|
-
|
|
419
|
-
```ruby
|
|
420
|
-
checker = LinkChecker::Net::HTTP::Checker.new(logger: Logger.new(STDOUT))
|
|
421
|
-
```
|
|
422
|
-
|
|
423
|
-
#### User-Agent
|
|
452
|
+
### Headers
|
|
424
453
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
```ruby
|
|
428
|
-
checker = LinkChecker::Net::HTTP::Checker.new(user_agent: 'Custom Agent/1.0')
|
|
429
|
-
```
|
|
430
|
-
|
|
431
|
-
#### Headers
|
|
432
|
-
|
|
433
|
-
Some sites reject or return errors (403, 404) to requests that look like bots. Pass additional headers to mimic a browser.
|
|
454
|
+
Checkers send a `User-Agent` of `Ruby Link Checker/x.y.z`; set `user_agent` to change it. Some sites reject requests that look like bots, so set `headers` to send other headers, such as those of a browser, to every host, including redirect targets.
|
|
434
455
|
|
|
435
456
|
```ruby
|
|
436
457
|
checker = LinkChecker::Net::HTTP::Checker.new(
|
|
458
|
+
user_agent: 'Custom Agent/1.0',
|
|
437
459
|
headers: {
|
|
438
460
|
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
|
439
|
-
'Accept-Language' => 'en-US,en;q=0.5'
|
|
440
|
-
'DNT' => '1'
|
|
461
|
+
'Accept-Language' => 'en-US,en;q=0.5'
|
|
441
462
|
}
|
|
442
463
|
)
|
|
443
464
|
```
|
|
444
465
|
|
|
445
|
-
Set `host_headers` to add or replace headers for
|
|
466
|
+
Set `host_headers` to add or replace headers for some hosts. Keys are a host name, compared case-insensitively, or a Regexp matched against the lower-cased host. Every matching entry applies, in order, replacing earlier headers with the same case-insensitive name, including `User-Agent`. Host headers are chosen from the host of each request, including every redirect hop, so a token for one host is never sent to another that it redirects to.
|
|
446
467
|
|
|
447
468
|
```ruby
|
|
448
469
|
checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
449
470
|
host_headers: {
|
|
450
|
-
'www.npmjs.com' => { 'Accept' => 'text/html' },
|
|
451
471
|
'api.github.com' => { 'Authorization' => "Bearer #{ENV.fetch('GITHUB_TOKEN')}" },
|
|
452
472
|
/(\A|\.)medium\.com\z/ => { 'Cookie' => 'consent=1' }
|
|
453
473
|
}
|
|
454
474
|
)
|
|
455
475
|
```
|
|
456
476
|
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
### Global Configuration
|
|
460
|
-
|
|
461
|
-
All options can also be configured globally.
|
|
462
|
-
|
|
463
|
-
```ruby
|
|
464
|
-
LinkChecker.configure do |config|
|
|
465
|
-
config.user_agent = 'Custom Agent/1.0'
|
|
466
|
-
config.headers = { 'DNT' => '1' }
|
|
467
|
-
config.methods = ['HEAD', 'GET']
|
|
468
|
-
config.logger = ::Logger.new(STDOUT)
|
|
469
|
-
end
|
|
470
|
-
```
|
|
471
|
-
|
|
472
|
-
### Callbacks and Events
|
|
473
|
-
|
|
474
|
-
Events enable processing of results as they become available.
|
|
475
|
-
|
|
476
|
-
```ruby
|
|
477
|
-
checker.on :complete do |result|
|
|
478
|
-
puts result # the final result of each link
|
|
479
|
-
end
|
|
480
|
-
|
|
481
|
-
checker.on :error, :failure do |result|
|
|
482
|
-
puts result # error or failure
|
|
483
|
-
end
|
|
484
|
-
```
|
|
485
|
-
|
|
486
|
-
Some events fire once per attempt, others fire once per link with its final result. Subscribe to `:complete`, or to the outcome events, to report links: `:result` also fires for each redirect hop and for a failed `HEAD` that is followed by a `GET`, so treating every `:result` that isn't a success as a broken link reports redirects and fallbacks as broken.
|
|
487
|
-
|
|
488
|
-
| Event | Fires | Description |
|
|
489
|
-
|-----------|------------------------|--------------------------------------------------------------------------|
|
|
490
|
-
| :result | Once per attempt | A new response or error, including redirects and attempts that are retried or followed by another method. |
|
|
491
|
-
| :redirect | Once per redirect hop | A redirect response that is being followed. |
|
|
492
|
-
| :retry | Once per retry | A request is being retried on failure or error. |
|
|
493
|
-
| :success | Once per link (final) | A valid URL, usually a 2xx response from the server. |
|
|
494
|
-
| :failure | Once per link (final) | A failed URL, usually a 4xx or a 5xx response from the server. |
|
|
495
|
-
| :blocked | Once per link (final) | A URL blocked by bot protection, see [Blocked Results](#blocked-results). |
|
|
496
|
-
| :error | Once per link (final) | An error, such as an invalid URL or a network timeout. |
|
|
497
|
-
| :complete | Once per link (final) | The final result, after the outcome event, whatever the outcome. |
|
|
498
|
-
|
|
499
|
-
Exactly one of `:success`, `:failure`, `:blocked`, or `:error` fires for each checked link, followed by `:complete` with the same result. For example, checking a URL that redirects once to a page that answers `HEAD` with 405 and `GET` with 200 publishes the following events, in order.
|
|
500
|
-
|
|
501
|
-
| Event | Method | Code |
|
|
502
|
-
|-----------|--------|------|
|
|
503
|
-
| :result | HEAD | 301 |
|
|
504
|
-
| :redirect | HEAD | 301 |
|
|
505
|
-
| :result | HEAD | 405 |
|
|
506
|
-
| :result | GET | 301 |
|
|
507
|
-
| :redirect | GET | 301 |
|
|
508
|
-
| :result | GET | 200 |
|
|
509
|
-
| :success | GET | 200 |
|
|
510
|
-
| :complete | GET | 200 |
|
|
511
|
-
|
|
512
|
-
Cache hits publish `:result`, `:success`, and `:complete`. With [deduplication](#deduplication), every submission of a URL receives its own copy of each event.
|
|
513
|
-
|
|
514
|
-
Events are called with results, which contain the following properties.
|
|
477
|
+
The headers of a URL are part of its [deduplication](#deduplication) and [caching](#caching) identity, so changing the host headers of a host only rechecks the links to that host. The cache file stores a hash of the headers, not their values.
|
|
515
478
|
|
|
516
|
-
|
|
517
|
-
|-------------------|-----------------------------------------------------------------|
|
|
518
|
-
| :url | The original URL before redirects. |
|
|
519
|
-
| :result_url | The last URL, different from `url` in case of redirects. |
|
|
520
|
-
| :method | The result HTTP method. |
|
|
521
|
-
| :code | HTTP error code. |
|
|
522
|
-
| :request_headers | Request headers. |
|
|
523
|
-
| :redirect_to | A redirect URL in case of redirects. |
|
|
524
|
-
| :error | A raised error in case of errors. |
|
|
525
|
-
| :error_type | `:timeout`, `:connection`, or `:other` for errors; otherwise `nil`. |
|
|
526
|
-
| :response_headers | Response headers, accessible using `[]`. |
|
|
527
|
-
| :cached? | Whether this result was reused from a successful check. |
|
|
528
|
-
| :blocked? | Whether this final result was reported as blocked. |
|
|
529
|
-
| :redirects | The redirect hops that led to this result, see [Redirects](#redirects). |
|
|
530
|
-
| :permanent_redirect? | Whether any of those hops was a permanent redirect (301 or 308). |
|
|
479
|
+
### Logger
|
|
531
480
|
|
|
532
|
-
|
|
533
|
-
| :reason | A stable explanation of the outcome, see below. |
|
|
534
|
-
| :to_h | A serializable summary of the result, see below. |
|
|
535
|
-
|
|
536
|
-
`result.reason` is one of `HTTP <status>` (such as `HTTP 404` or `HTTP 403`), `<name> challenge (HTTP <status>)` for an unsuccessful response that carries a known bot challenge (such as `Cloudflare challenge (HTTP 403)`), `Timeout`, `Connection error`, `Redirect loop`, `Too many redirects`, `Invalid URL`, or `Error (<class name>)` for other errors.
|
|
537
|
-
|
|
538
|
-
`result.to_h` returns a hash with the `url`, `result_url`, `method`, `code` (an integer, or `nil` for errors), `outcome`, `reason`, `redirects` (each with `url`, `code`, and `to`), `permanent_redirect`, `blocked`, `challenge` (the name of a detected bot challenge, such as `Cloudflare`, or `nil`), `cached`, `error` (the error message), `error_type`, and the `options` passed to `check`. It's suitable for JSON reports when those options are.
|
|
481
|
+
Checkers log to `LinkChecker.config.logger`; set `logger` to use another one, and its level to `Logger::INFO` to log every request.
|
|
539
482
|
|
|
540
483
|
```ruby
|
|
541
|
-
checker.
|
|
542
|
-
puts JSON.generate(result.to_h) unless result.success?
|
|
543
|
-
end
|
|
484
|
+
checker = LinkChecker::Net::HTTP::Checker.new(logger: Logger.new($stdout))
|
|
544
485
|
```
|
|
545
486
|
|
|
546
|
-
See [result.rb](lib/ruby-link-checker/result.rb) for more details.
|
|
547
|
-
|
|
548
487
|
## Benchmarks
|
|
549
488
|
|
|
550
|
-
|
|
489
|
+
[benchmarks/check.rb](benchmarks/check.rb) compares the checkers on a fixed set of URLs, with network requests. [benchmarks/features.rb](benchmarks/features.rb) measures the time per link of each checker with plain links, [fragments](#fragments), [meta refreshes](#meta-refresh), [URL rewrites](#url-rewrites), and [caching](#caching), with WebMock responses instead of network requests, and [benchmarks/html.rb](benchmarks/html.rb) measures scanning pages for fragments and meta refreshes.
|
|
551
490
|
|
|
552
491
|
```
|
|
553
492
|
bundle exec ruby -Ilib benchmarks/check.rb
|
|
493
|
+
bundle exec ruby -Ilib benchmarks/features.rb
|
|
494
|
+
bundle exec ruby -Ilib benchmarks/html.rb
|
|
554
495
|
```
|
|
555
496
|
|
|
556
|
-
|
|
497
|
+
Anecdotally, `LinkChecker::Async::HTTP` uses much less CPU time than `LinkChecker::Typhoeus::Hydra`, while their wall-clock times are similar and depend on timeouts and the responsiveness of the URLs. `LinkChecker::Net::HTTP` is much slower for many URLs, and is commented out in the script.
|
|
557
498
|
|
|
558
499
|
## Contributing
|
|
559
500
|
|