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.
data/README.md CHANGED
@@ -5,73 +5,66 @@ Fast Ruby Link Checker
5
5
 
6
6
  [![Gem Version](http://img.shields.io/gem/v/ruby-link-checker.svg)](http://badge.fury.io/rb/ruby-link-checker)
7
7
  [![Build Status](https://github.com/dblock/ruby-link-checker/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/dblock/ruby-link-checker/actions/workflows/test.yml)
8
- [![codecov](https://codecov.io/gh/dblock/ruby-link-checker/graph/badge.svg)](https://codecov.io/gh/dblock/ruby-link-checker)
8
+ [![Coverage Status](https://coveralls.io/repos/github/dblock/ruby-link-checker/badge.svg?branch=main)](https://coveralls.io/github/dblock/ruby-link-checker?branch=main)
9
9
 
10
- A fast Ruby link checker with support for multiple HTTP libraries. Does not parse documents, just checks links. Fast. Anecdotal benchmarking on a M1 mac and T1 Internet yields ~50 URLs per second with `LinkChecker::Typhoeus::Hydra`.
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`.
11
11
 
12
12
  ## Table of Contents
13
13
 
14
+ - [Installation](#installation)
14
15
  - [Usage](#usage)
15
- - [Dependencies](#dependencies)
16
- - [Basic Usage](#basic-usage)
17
16
  - [Passing Options](#passing-options)
18
- - [Checkers](#checkers)
19
- - [LinkChecker::Typhoeus::Hydra](#linkcheckertyphoeushydra)
20
- - [LinkChecker::Async::HTTP](#linkcheckerasynchttp)
21
- - [LinkChecker::Net::HTTP](#linkcheckernethttp)
22
- - [Options](#options)
23
- - [Retries](#retries)
24
- - [Redirects](#redirects)
25
- - [Retry After](#retry-after)
26
- - [Retry Policy](#retry-policy)
27
- - [Rate Limits](#rate-limits)
28
- - [Blocked Results](#blocked-results)
29
- - [Per-Host Limits](#per-host-limits)
30
- - [Results](#results)
31
- - [Methods](#methods)
32
- - [Deduplication](#deduplication)
33
- - [Caching](#caching)
34
- - [Logger](#logger)
35
- - [User-Agent](#user-agent)
36
- - [Headers](#headers)
37
- - [Global Configuration](#global-configuration)
38
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)
39
37
  - [Benchmarks](#benchmarks)
40
38
  - [Contributing](#contributing)
41
39
  - [Copyright and License](#copyright-and-license)
42
40
 
43
- ## Usage
44
-
45
- ### Dependencies
41
+ ## Installation
46
42
 
47
- The [`LinkChecker::Typhoeus::Hydra`](lib/ruby-link-checker/typhoeus/hydra/checker.rb) link checker is recommended.
48
-
49
- 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.
50
44
 
51
45
  ```ruby
52
46
  gem 'typhoeus'
53
47
  gem 'ruby-link-checker'
54
48
  ```
55
49
 
56
- ### Basic Usage
50
+ ## Usage
57
51
 
58
52
  ```ruby
59
53
  require 'typhoeus'
60
54
  require 'ruby-link-checker'
61
55
 
62
- # create a new checker instance
63
56
  checker = LinkChecker::Typhoeus::Hydra::Checker.new
64
57
 
65
- # queue URLs to check
66
- links = [...]
67
58
  links.each do |url|
68
- checker.check url
59
+ checker.check(url)
60
+ end
61
+
62
+ checker.on :failure, :error do |result|
63
+ puts "FAIL: #{result}"
69
64
  end
70
65
 
71
- # run the checks
72
- checker.run
66
+ checker.run # blocks until every link is checked
73
67
 
74
- # display buckets of results
75
68
  checker.results.each_pair do |bucket, results|
76
69
  puts "#{bucket}: #{results.size}"
77
70
  end
@@ -79,57 +72,115 @@ end
79
72
 
80
73
  ### Passing Options
81
74
 
82
- You can pipe custom options through `check` and retrieve them in events as follows.
75
+ Options passed to `check` are returned with every result of that link.
83
76
 
84
77
  ```ruby
85
- checker.check 'https://www.example.org', { location: 'page.html' }
78
+ checker.check('https://www.example.org', location: 'page.html')
86
79
 
87
80
  checker.on :success do |result|
88
- result.options # contains { location: 'page.html' }
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
89
92
  end
90
93
  ```
91
94
 
92
- ### Checkers
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 |
93
120
 
94
- #### [LinkChecker::Typhoeus::Hydra](lib/ruby-link-checker/typhoeus/hydra/checker.rb)
121
+ ### Results
95
122
 
96
- Fast link checker that uses [Typhoeus](https://typhoeus.github.io/).
123
+ Checkers collect final results by outcome. Pass `results: false` to only publish events.
97
124
 
98
125
  ```ruby
99
- require 'typhoeus'
100
- require 'ruby-link-checker'
126
+ checker.results # => { success: [...], failure: [...], blocked: [...], error: [...] }
127
+ ```
101
128
 
102
- # create a new instance of a checker
103
- checker = LinkChecker::Typhoeus::Hydra::Checker.new(
104
- hydra: {
105
- # lower than the Typhoeus default of 200, seems to start breaking around 50+
106
- max_concurrency: 25
107
- }
108
- )
129
+ Results have the following properties. See [result.rb](lib/ruby-link-checker/result.rb) for details.
109
130
 
110
- # log requests and response codes
111
- checker.logger.level = Logger::INFO
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. |
112
152
 
113
- links = [...] # array of URLs
114
- links.each do |url|
115
- checker.check url
116
- end
153
+ `result.reason` is one of the following.
117
154
 
118
- # examine failures and errors as they come
119
- checker.on :error, :failure do |result|
120
- puts "FAIL: #{result}"
121
- end
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.
122
161
 
123
- # execute Hydra#run, will block until all requests have completed
124
- 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.
125
163
 
126
- # examine results
127
- checker.results.each_pair do |bucket, results|
128
- puts "#{bucket}: #{results.size}"
164
+ ```ruby
165
+ checker.on :complete do |result|
166
+ puts JSON.generate(result.to_h) unless result.success?
129
167
  end
130
168
  ```
131
169
 
132
- You can pass `Typhoeus` timeout options into a new instance of a checker, or configure timeouts globally.
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.
133
184
 
134
185
  ```ruby
135
186
  LinkChecker::Typhoeus::Hydra.configure do |config|
@@ -138,89 +189,40 @@ LinkChecker::Typhoeus::Hydra.configure do |config|
138
189
  end
139
190
  ```
140
191
 
141
- #### [LinkChecker::Async::HTTP](lib/ruby-link-checker/async/http/checker.rb)
192
+ ### [LinkChecker::Async::HTTP](lib/ruby-link-checker/async/http/checker.rb)
142
193
 
143
- Fast, concurrent link checker that uses [async-http](https://github.com/socketry/async-http).
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.
144
195
 
145
196
  ```ruby
146
197
  require 'async'
147
198
  require 'async/http'
148
199
  require 'ruby-link-checker'
149
200
 
150
- # create a new instance of a checker
151
- checker = LinkChecker::Async::HTTP::Checker.new
152
-
153
- # log requests and response codes
154
- checker.logger.level = Logger::INFO
155
-
156
- links = [...] # array of URLs
157
- links.each do |url|
158
- checker.check url
159
- end
160
-
161
- # examine failures and errors as they come
162
- checker.on :error, :failure do |result|
163
- puts "FAIL: #{result}"
164
- end
165
-
166
- # runs all queued checks concurrently, will block until all requests have completed
167
- checker.run
168
-
169
- # examine results
170
- checker.results.each_pair do |bucket, results|
171
- puts "#{bucket}: #{results.size}"
172
- end
201
+ checker = LinkChecker::Async::HTTP::Checker.new(max_concurrency: 25, protocol: :http2)
173
202
  ```
174
203
 
175
- You can pass `Async::HTTP` timeout options into a new instance of a checker, or configure timeouts globally.
204
+ Configure timeouts and the protocol per checker, or for every Async checker.
176
205
 
177
206
  ```ruby
178
207
  LinkChecker::Async::HTTP.configure do |config|
179
208
  config.read_timeout = 5
180
209
  config.open_timeout = 10
210
+ config.protocol = :http1
181
211
  end
182
212
  ```
183
213
 
184
- You can also force a specific HTTP protocol version, `:http1` or `:http2`, per instance or globally. By default, `async-http` negotiates the protocol version with the server.
185
-
186
- ```ruby
187
- LinkChecker::Async::HTTP.configure do |config|
188
- config.protocol = :http2
189
- end
190
- ```
191
-
192
- 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)
193
215
 
194
- ```ruby
195
- checker = LinkChecker::Async::HTTP::Checker.new(max_concurrency: 25)
196
- ```
197
-
198
- #### [LinkChecker::Net::HTTP](lib/ruby-link-checker/net/http/checker.rb)
199
-
200
- 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).
201
217
 
202
218
  ```ruby
203
219
  require 'net/http'
204
220
  require 'ruby-link-checker'
205
221
 
206
- # create a new instance of a checker
207
- checker = LinkChecker::Net::HTTP::Checker.new
208
-
209
- # log requests and response codes
210
- checker.logger.level = Logger::INFO
211
-
212
- links = [...] # array of URLs
213
- links.each do |url|
214
- checker.check url
215
- end
216
-
217
- # examine results
218
- checker.results.each_pair do |bucket, results|
219
- puts "#{bucket}: #{results.size}"
220
- end
222
+ checker = LinkChecker::Net::HTTP::Checker.new(read_timeout: 5)
221
223
  ```
222
224
 
223
- You can pass `Net::HTTP` timeout options into a new instance of a checker, or configure timeouts globally.
225
+ Configure timeouts per checker, as above, or for every Net::HTTP checker.
224
226
 
225
227
  ```ruby
226
228
  LinkChecker::Net::HTTP.configure do |config|
@@ -229,25 +231,36 @@ LinkChecker::Net::HTTP.configure do |config|
229
231
  end
230
232
  ```
231
233
 
232
- ### Options
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
+ ```
233
246
 
234
- #### Retries
247
+ ### Methods
235
248
 
236
- By default link checkers only retry [rate-limited responses](#rate-limits). Set `retries` to enable retries for transient connection errors/timeouts and HTTP statuses 408, 425, 429, 500, 502, 503, and 504. Permanent HTTP failures and invalid URLs are not retried. HEAD-to-GET fallback still applies independently of the retry policy. The retry budget is shared across methods: with `retries: 2`, repeated transient failures produce `HEAD`, `HEAD`, `HEAD`, then `GET`.
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`.
237
250
 
238
251
  ```ruby
239
- checker = LinkChecker::Net::HTTP::Checker.new(retries: 1)
252
+ checker = LinkChecker::Net::HTTP::Checker.new(methods: %w[HEAD])
240
253
  ```
241
254
 
242
- #### Redirects
255
+ ### Redirects
243
256
 
244
- Checkers follow up to 20 redirects per attempt and detect redirect loops. Relative redirects are resolved against the current response URL. Set `max_redirects` to change the limit, or use `0` to disable redirects.
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.
245
258
 
246
259
  ```ruby
247
260
  checker = LinkChecker::Typhoeus::Hydra::Checker.new(max_redirects: 10)
248
261
  ```
249
262
 
250
- Final results expose the redirect hops that led to them as `result.redirects`, oldest first, each a `LinkChecker::Redirect` with the responding `uri`, its status `code`, the resolved target `to`, and `permanent?` for 301 and 308. Use `result.permanent_redirect?` to suggest updating links that moved permanently. Only the hops of the final attempt are included: when `HEAD` fails after redirects and `GET` is tried, `redirects` lists the `GET` hops. Redirect loop and limit errors include the hops that led to them.
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.
251
264
 
252
265
  ```ruby
253
266
  checker.on :success do |result|
@@ -255,151 +268,180 @@ checker.on :success do |result|
255
268
  end
256
269
  ```
257
270
 
258
- #### Retry After
271
+ ### Meta Refresh
259
272
 
260
- Set `retry_after` to a minimum delay in seconds (default `0`). Optional `retry_backoff` multiplies that delay on successive retries (default `1`), and `retry_jitter` adds a random fraction between `0` and the configured value (default `0`, maximum `1`). All delays are capped by `max_retry_delay` (default `60` seconds).
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.
261
276
 
262
277
  ```ruby
263
- checker = LinkChecker::Typhoeus::Hydra::Checker.new(
264
- retries: 2,
265
- retry_after: 1,
266
- retry_backoff: 2,
267
- retry_jitter: 0.1,
268
- respect_retry_after: true,
269
- max_retry_delay: 60
270
- )
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
271
280
  ```
272
281
 
273
- With `respect_retry_after: true` (default `false`), HTTP `Retry-After` delta-seconds and dates provide a minimum delay, subject to the same cap. Invalid or past dates are ignored. Async delays yield without blocking other requests. Hydra queues delayed retries after the active batch drains, so actual delays can exceed the configured minimum without blocking completion callbacks. Delays apply to retries, not ordinary HEAD-to-GET fallback.
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)`.
274
283
 
275
- #### Retry Policy
284
+ ### Fragments
276
285
 
277
- Customize `retry_status_codes` to choose retryable HTTP statuses. Set it to `nil` to restore the previous behavior of retrying every failure and error, including malformed URLs and programming errors.
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)`.
278
287
 
279
288
  ```ruby
280
- checker = LinkChecker::Net::HTTP::Checker.new(retries: 1, retry_status_codes: [429, 503])
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
281
295
  ```
282
296
 
283
- #### Rate Limits
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.
284
298
 
285
- Rate-limited responses (429 Too Many Requests) are retried with exponential backoff per host, on their own budget, independently of `retries`. A rate-limited response pauses every later request to the same host, not just the retried one, for `rate_limit_backoff` seconds (default `1`), doubling with each further rate-limited response from that host (1s, 2s, 4s, ...). A `Retry-After` header that asks for longer is honored, and every pause is capped at `max_rate_limit_delay` seconds (default `30`). Rate-limited responses that arrive while the host is already paused, such as the rest of a burst, don't increase the backoff, and any response from the host that isn't rate limited resets it.
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]`.
286
303
 
287
- Each link is retried up to `rate_limit_retries` times (default `2`) when rate limited. After that, the general [retry policy](#retry-policy) applies, and a link that's still rate limited is reported as [blocked](#blocked-results) (or failed if 429 isn't in `blocked_status_codes`). Set `rate_limit_retries: 0` to disable rate limit backoff entirely.
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.
288
305
 
289
306
  ```ruby
290
- checker = LinkChecker::Typhoeus::Hydra::Checker.new(
291
- rate_limit_retries: 3,
292
- rate_limit_backoff: 2,
293
- max_rate_limit_delay: 60,
294
- rate_limit_status_codes: [429, 503]
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
+ )
295
312
  )
296
313
  ```
297
314
 
298
- Use `rate_limit_status_codes` (default `[429]`) to treat other statuses, such as 503, as rate limits. Pauses use the same per-host scheduling as [per-host limits](#per-host-limits): Hydra holds back requests to a paused host until the active batch drains, but can't recall requests it already started, so combine rate limit backoff with `max_concurrency_per_host` to keep bursts small. Rate limit retries publish `:retry` like other retries.
315
+ Set `check_fragments: false` to ignore fragments.
299
316
 
300
- #### Blocked Results
317
+ ```ruby
318
+ checker = LinkChecker::Net::HTTP::Checker.new(check_fragments: false)
319
+ ```
301
320
 
302
- Bot protection often answers automated requests with 401, 403, 429, or a non-standard status, such as LinkedIn's 999, even though the page exists. By default, checkers report links with those final status codes as blocked (unverified) instead of failed. Customize `blocked_status_codes` (default `[401, 403, 429, 999]`) to choose the statuses, or set it to `[]` to report them as failures, as versions before 0.6.0 did.
321
+ ### URL Rewrites
303
322
 
304
- ```ruby
305
- checker = LinkChecker::Typhoeus::Hydra::Checker.new(
306
- blocked_status_codes: [403, 999]
307
- )
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.
308
324
 
309
- checker.on :blocked do |result|
310
- puts "UNVERIFIED: #{result}" # result.blocked? == true, result.failure? == false
311
- end
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
312
330
  ```
313
331
 
314
- A link is blocked when its final result, after retries, redirects, and the `HEAD` to `GET` fallback, has one of those status codes. Blocked results publish `:blocked` instead of `:failure` and are collected in `results[:blocked]`. Retries still apply first, so a 429 that is in `retry_status_codes` is retried before being reported as blocked. Errors, such as timeouts, are never blocked. Blocked results are only cached with a [`blocked_ttl`](#caching).
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.
315
333
 
316
- By default, `detect_challenges` also reports non-successful responses that carry a known bot challenge as blocked, whatever their status code. Combined with a narrower `blocked_status_codes`, such as `[]`, this lets an ordinary 403 remain a failure while a challenged one is blocked. Challenge detection currently recognizes Cloudflare's [`cf-mitigated: challenge`](https://developers.cloudflare.com/cloudflare-challenges/challenge-types/challenge-pages/detect-response/) response header. Set `detect_challenges: false` to report challenged responses as failures.
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.
317
348
 
318
349
  ```ruby
319
- checker = LinkChecker::Typhoeus::Hydra::Checker.new(detect_challenges: false)
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])
320
352
  ```
321
353
 
322
- #### Per-Host Limits
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.
323
359
 
324
- Many links to the same host arrive as one 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 consecutive 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.
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.
325
361
 
326
362
  ```ruby
327
363
  checker = LinkChecker::Typhoeus::Hydra::Checker.new(
328
- max_concurrency_per_host: 2,
329
- min_host_interval: 0.5
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
330
371
  )
331
372
  ```
332
373
 
333
- Limits per host apply in addition to the global `max_concurrency` of `LinkChecker::Async::HTTP` and Hydra's `max_concurrency`. Hydra holds requests per host and hands them to Hydra as slots free up; requests that must wait for `min_host_interval` are handed over once the active batch drains, like delayed retries, so actual intervals can exceed the minimum. `LinkChecker::Net::HTTP` is sequential, so only `min_host_interval` changes its behavior. Neither setting affects [deduplication](#deduplication) or [caching](#caching) identity.
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.
334
375
 
335
- #### Results
376
+ ### Rate Limits
336
377
 
337
- By default checkers collect results.
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.
338
379
 
339
- ```ruby
340
- checker = LinkChecker::Net::HTTP::Checker.new
341
- ...
342
- 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`.
343
381
 
344
- checker.results # => { error: [...], failure: [...], success: [...], blocked: [...] }
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
+ )
345
389
  ```
346
390
 
347
- You can disable this with `results: false`.
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.
348
396
 
349
397
  ```ruby
350
- checker = LinkChecker::Net::HTTP::Checker.new(results: false)
351
- ...
352
- checker.run
398
+ checker = LinkChecker::Typhoeus::Hydra::Checker.new(blocked_status_codes: [403, 999])
353
399
 
354
- checker.results # => nil
400
+ checker.on :blocked do |result|
401
+ puts "UNVERIFIED: #{result}" # result.blocked? == true, result.failure? == false
402
+ end
355
403
  ```
356
404
 
357
- #### Methods
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).
358
406
 
359
- By default checkers try a `HEAD` request, followed by a `GET` if `HEAD` fails. You can change this behavior by specifying other methods.
407
+ ### Per-Host Limits
360
408
 
361
- The following examples disables `GET` and only makes `HEAD` requests.
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.
362
410
 
363
411
  ```ruby
364
- checker = LinkChecker::Net::HTTP::Checker.new(methods: %w[HEAD])
412
+ checker = LinkChecker::Typhoeus::Hydra::Checker.new(max_concurrency_per_host: 2, min_host_interval: 0.5)
365
413
  ```
366
414
 
367
- #### Deduplication
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
368
418
 
369
- Set `deduplicate: true` to coalesce identical in-flight requests and reuse successful results for the lifetime of the checker. Failures and errors are rechecked after completion. Each submission still receives its own result events and metadata, including events emitted before it joined an in-flight check. Deduplication is disabled by default.
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.
370
420
 
371
421
  ```ruby
372
422
  checker = LinkChecker::Typhoeus::Hydra::Checker.new(deduplicate: true)
373
423
  checker.check('https://www.example.org', location: 'one.html')
374
424
  checker.check('https://www.example.org', location: 'two.html')
375
- checker.run # one check, two sets of result events
425
+ checker.run # one check, two sets of events
376
426
  ```
377
427
 
378
- Identity includes the URL with its query values, but not its `#fragment`, which is never sent to the server, so links that differ only by fragment share one check. Each submission's results still report its own URL, fragment included. Identity also includes the transport, methods, headers, host headers, user-agent, and request-related settings. Changing those settings prevents reuse. URLs containing user-info credentials bypass deduplication and caching.
379
-
380
- #### 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.
381
429
 
382
- Persistent success caching is disabled by default. Enable it with an explicit file path and positive TTL in seconds. Recent successful checks can then be reused across checker instances; failures and errors are never stored.
430
+ ### Caching
383
431
 
384
- Add an optional `blocked_ttl` in seconds to also reuse [blocked results](#blocked-results) for a shorter period, so that bot-walled hosts aren't requested on every run but are still rechecked regularly. Blocked results are not cached without it. A cached blocked result is reused only while the checker's `blocked_status_codes` or `detect_challenges` still classify it as blocked; otherwise the link is checked again. Cache hits for blocked results emit `:result`, `:blocked`, and `:complete` with `cached? == true`.
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.
385
433
 
386
434
  ```ruby
387
435
  checker = LinkChecker::Typhoeus::Hydra::Checker.new(
388
- blocked_status_codes: [401, 403, 429, 999],
389
436
  cache: { file: 'tmp/link-checker/cache.json', ttl: 7 * 24 * 3600, blocked_ttl: 24 * 3600 }
390
437
  )
391
- ``` Cache writes are atomic and locked, and unreadable or malformed files fall back to normal checking.
392
-
393
- ```ruby
394
- checker = LinkChecker::Typhoeus::Hydra::Checker.new(
395
- deduplicate: true,
396
- cache: { file: 'tmp/link-checker/cache.json', ttl: 3600 }
397
- )
398
438
  ```
399
439
 
400
- Each cache hit emits `:result` and `:success` with the new submission's metadata and `cached? == true`. Persistent hits return `LinkChecker::CachedResult` with the HTTP method, status, original/final URIs, redirect hops, and metadata; native request/response objects and headers are not persisted. Results whose final URL or any redirect hop contains user-info credentials are not written. Cache files written by versions before 0.6.0 are ignored and rewritten. Add the cache directory to your ignore rules.
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.
401
443
 
402
- Entries for links that are no longer checked stay in the cache file until they expire. Set `prune: true` to remove every entry that the checker didn't use when `checker.run` finishes, or call `checker.prune_cache!`, which returns the number of entries removed. Pruning is off by default: enable it only when each run checks every link that should stay cached, not for partial runs, such as incremental builds, and not with a cache file shared by checkers that check different links.
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.
403
445
 
404
446
  ```ruby
405
447
  checker = LinkChecker::Typhoeus::Hydra::Checker.new(
@@ -407,150 +449,52 @@ checker = LinkChecker::Typhoeus::Hydra::Checker.new(
407
449
  )
408
450
  ```
409
451
 
410
- Caching alone does not coalesce in-flight checks. With deduplication enabled, successful results remain reusable within that checker even after the persistent TTL expires. Cached successes do not replay historical redirect/retry events.
411
-
412
- #### Logger
413
-
414
- Pass your own logger.
415
-
416
- ```ruby
417
- checker = LinkChecker::Net::HTTP::Checker.new(logger: Logger.new(STDOUT))
418
- ```
419
-
420
- #### User-Agent
452
+ ### Headers
421
453
 
422
- Pass your own user-agent. Default is `Ruby Link Checker/x.y.z`.
423
-
424
- ```ruby
425
- checker = LinkChecker::Net::HTTP::Checker.new(user_agent: 'Custom Agent/1.0')
426
- ```
427
-
428
- #### Headers
429
-
430
- 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.
431
455
 
432
456
  ```ruby
433
457
  checker = LinkChecker::Net::HTTP::Checker.new(
458
+ user_agent: 'Custom Agent/1.0',
434
459
  headers: {
435
460
  'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
436
- 'Accept-Language' => 'en-US,en;q=0.5',
437
- 'DNT' => '1'
461
+ 'Accept-Language' => 'en-US,en;q=0.5'
438
462
  }
439
463
  )
440
464
  ```
441
465
 
442
- Set `host_headers` to add or replace headers for specific hosts, on top of the global `headers`. Keys are an exact host name (case-insensitive) or a `Regexp` matched against the lower-cased host. Every matching entry applies, in order, and replaces earlier values with the same case-insensitive name, including `User-Agent`.
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.
443
467
 
444
468
  ```ruby
445
469
  checker = LinkChecker::Typhoeus::Hydra::Checker.new(
446
470
  host_headers: {
447
- 'www.npmjs.com' => { 'Accept' => 'text/html' },
448
471
  'api.github.com' => { 'Authorization' => "Bearer #{ENV.fetch('GITHUB_TOKEN')}" },
449
472
  /(\A|\.)medium\.com\z/ => { 'Cookie' => 'consent=1' }
450
473
  }
451
474
  )
452
475
  ```
453
476
 
454
- Host headers are chosen for each request from that request's own host, including every redirect hop, so a token configured for one host is never sent to a different host that it redirects to. Global `headers` are sent to every host, including redirect targets. The headers sent for a URL, global headers merged with its matching host headers, are part of its [deduplication](#deduplication) and [caching](#caching) identity: the cache file stores a hash of them, not the values themselves. Changing the host headers of one host only invalidates cached results for the URLs on the hosts it matches.
455
-
456
- ### Global Configuration
457
-
458
- All options can also be configured globally.
459
-
460
- ```ruby
461
- LinkChecker.configure do |config|
462
- config.user_agent = 'Custom Agent/1.0'
463
- config.headers = { 'DNT' => '1' }
464
- config.methods = ['HEAD', 'GET']
465
- config.logger = ::Logger.new(STDOUT)
466
- end
467
- ```
468
-
469
- ### Callbacks and Events
470
-
471
- Events enable processing of results as they become available.
472
-
473
- ```ruby
474
- checker.on :complete do |result|
475
- puts result # the final result of each link
476
- end
477
-
478
- checker.on :error, :failure do |result|
479
- puts result # error or failure
480
- end
481
- ```
482
-
483
- 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.
484
-
485
- | Event | Fires | Description |
486
- |-----------|------------------------|--------------------------------------------------------------------------|
487
- | :result | Once per attempt | A new response or error, including redirects and attempts that are retried or followed by another method. |
488
- | :redirect | Once per redirect hop | A redirect response that is being followed. |
489
- | :retry | Once per retry | A request is being retried on failure or error. |
490
- | :success | Once per link (final) | A valid URL, usually a 2xx response from the server. |
491
- | :failure | Once per link (final) | A failed URL, usually a 4xx or a 5xx response from the server. |
492
- | :blocked | Once per link (final) | A URL blocked by bot protection, see [Blocked Results](#blocked-results). |
493
- | :error | Once per link (final) | An error, such as an invalid URL or a network timeout. |
494
- | :complete | Once per link (final) | The final result, after the outcome event, whatever the outcome. |
495
-
496
- 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.
497
-
498
- | Event | Method | Code |
499
- |-----------|--------|------|
500
- | :result | HEAD | 301 |
501
- | :redirect | HEAD | 301 |
502
- | :result | HEAD | 405 |
503
- | :result | GET | 301 |
504
- | :redirect | GET | 301 |
505
- | :result | GET | 200 |
506
- | :success | GET | 200 |
507
- | :complete | GET | 200 |
508
-
509
- Cache hits publish `:result`, `:success`, and `:complete`. With [deduplication](#deduplication), every submission of a URL receives its own copy of each event.
510
-
511
- 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.
512
478
 
513
- | Property | Description |
514
- |-------------------|-----------------------------------------------------------------|
515
- | :url | The original URL before redirects. |
516
- | :result_url | The last URL, different from `url` in case of redirects. |
517
- | :method | The result HTTP method. |
518
- | :code | HTTP error code. |
519
- | :request_headers | Request headers. |
520
- | :redirect_to | A redirect URL in case of redirects. |
521
- | :error | A raised error in case of errors. |
522
- | :error_type | `:timeout`, `:connection`, or `:other` for errors; otherwise `nil`. |
523
- | :response_headers | Response headers, accessible using `[]`. |
524
- | :cached? | Whether this result was reused from a successful check. |
525
- | :blocked? | Whether this final result was reported as blocked. |
526
- | :redirects | The redirect hops that led to this result, see [Redirects](#redirects). |
527
- | :permanent_redirect? | Whether any of those hops was a permanent redirect (301 or 308). |
479
+ ### Logger
528
480
 
529
- | :outcome | `:success`, `:failure`, `:blocked`, or `:error` for a final result, `:redirect` for a redirect hop. |
530
- | :reason | A stable explanation of the outcome, see below. |
531
- | :to_h | A serializable summary of the result, see below. |
532
-
533
- `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.
534
-
535
- `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.
536
482
 
537
483
  ```ruby
538
- checker.on :complete do |result|
539
- puts JSON.generate(result.to_h) unless result.success?
540
- end
484
+ checker = LinkChecker::Net::HTTP::Checker.new(logger: Logger.new($stdout))
541
485
  ```
542
486
 
543
- See [result.rb](lib/ruby-link-checker/result.rb) for more details.
544
-
545
487
  ## Benchmarks
546
488
 
547
- See [benchmarks/check.rb](benchmarks/check.rb) for a script comparing the performance of the three checkers (`LinkChecker::Net::HTTP`, `LinkChecker::Typhoeus::Hydra`, and `LinkChecker::Async::HTTP`) against a fixed set of URLs.
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.
548
490
 
549
491
  ```
550
492
  bundle exec ruby -Ilib benchmarks/check.rb
493
+ bundle exec ruby -Ilib benchmarks/features.rb
494
+ bundle exec ruby -Ilib benchmarks/html.rb
551
495
  ```
552
496
 
553
- Anecdotal results show `LinkChecker::Async::HTTP` using significantly less CPU time than `LinkChecker::Typhoeus::Hydra` (both concurrent checkers), while wall-clock time is comparable between the two and depends on timeouts and the responsiveness of the URLs being checked. `LinkChecker::Net::HTTP` checks links sequentially and is significantly slower in wall-clock time for large batches of URLs; it's commented out by default in the script.
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.
554
498
 
555
499
  ## Contributing
556
500