ruby-link-checker 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -0
- data/README.md +153 -19
- data/UPGRADING.md +46 -0
- data/lib/ruby-link-checker/async/http/checker.rb +19 -6
- data/lib/ruby-link-checker/async/http/result.rb +1 -1
- data/lib/ruby-link-checker/cache.rb +90 -21
- data/lib/ruby-link-checker/checker.rb +136 -13
- data/lib/ruby-link-checker/config.rb +79 -0
- data/lib/ruby-link-checker/net/http/checker.rb +3 -2
- data/lib/ruby-link-checker/net/http/result.rb +1 -1
- data/lib/ruby-link-checker/redirect.rb +11 -0
- data/lib/ruby-link-checker/result.rb +96 -1
- data/lib/ruby-link-checker/tasks.rb +24 -12
- data/lib/ruby-link-checker/typhoeus/hydra/checker.rb +59 -11
- data/lib/ruby-link-checker/typhoeus/hydra/result.rb +1 -1
- data/lib/ruby-link-checker/version.rb +1 -1
- data/lib/ruby-link-checker.rb +1 -0
- metadata +18 -51
- data/.github/workflows/coverage.yml +0 -24
- data/.github/workflows/danger-comment.yml +0 -17
- data/.github/workflows/danger.yml +0 -12
- data/.github/workflows/lint.yml +0 -20
- data/.github/workflows/release.yml +0 -14
- data/.github/workflows/test.yml +0 -25
- data/.gitignore +0 -6
- data/.rspec +0 -2
- data/.rubocop.yml +0 -26
- data/.rubocop_todo.yml +0 -332
- data/AGENTS.md +0 -23
- data/CONTRIBUTING.md +0 -129
- data/Dangerfile +0 -6
- data/Gemfile +0 -27
- data/RELEASING.md +0 -37
- data/Rakefile +0 -26
- data/benchmarks/check.rb +0 -38
- data/benchmarks/data/opensearch.org/links.txt +0 -4270
- data/benchmarks/data/opensearch.org/small.txt +0 -100
- data/ruby-link-checker.gemspec +0 -22
- data/spec/fixtures/200.yml +0 -65
- data/spec/fixtures/301_200.yml +0 -102
- data/spec/fixtures/301_301.yml +0 -114
- data/spec/fixtures/301_400_301_200.yml +0 -185
- data/spec/fixtures/301_403.yml +0 -78
- data/spec/fixtures/308_429.yml +0 -108
- data/spec/fixtures/404.yml +0 -57
- data/spec/fixtures/404_200.yml +0 -89
- data/spec/fixtures/404_404.yml +0 -81
- data/spec/fixtures/429_200.yml +0 -135
- data/spec/fixtures/429_429_200.yml +0 -201
- data/spec/ruby-link-checker/async/http/checker_spec.rb +0 -184
- data/spec/ruby-link-checker/callbacks_spec.rb +0 -76
- data/spec/ruby-link-checker/checker_spec.rb +0 -97
- data/spec/ruby-link-checker/config_spec.rb +0 -45
- data/spec/ruby-link-checker/errors/redirect_loop_error_spec.rb +0 -18
- data/spec/ruby-link-checker/net/http/checker_spec.rb +0 -93
- data/spec/ruby-link-checker/result_error_spec.rb +0 -34
- data/spec/ruby-link-checker/result_spec.rb +0 -41
- data/spec/ruby-link-checker/task_spec.rb +0 -20
- data/spec/ruby-link-checker/tasks_spec.rb +0 -91
- data/spec/ruby-link-checker/typhoeus/hydra/checker_spec.rb +0 -180
- data/spec/ruby-link-checker/version_spec.rb +0 -9
- data/spec/spec_helper.rb +0 -19
- data/spec/support/config.rb +0 -8
- data/spec/support/link_checker.rb +0 -609
- data/spec/support/vcr.rb +0 -13
- data/spec/support/with_result.rb +0 -15
- data/spec/support/with_url.rb +0 -14
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5797b1459b6dfe9254188079c8940286e4b49437808cf5f74b11f6423f2d5677
|
|
4
|
+
data.tar.gz: b2cfb5abb84a0226f1067f74ecaeef5152a3b5c697ad8962b9503f57854f0f4a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 546bc8134e07bab2f58bc50ea3fe9f60e2f79252301b41321ca4b08b4c8600c646da30907b742fffb2f8af46e24fa8531ccd4c969bef3575fbe33872acd8c765
|
|
7
|
+
data.tar.gz: 3894bcd493c8ea47b894f22293ab04ffe347333eb96e974df2ce9cc8082cd046742ad2e48a31e6a0c43a6cb2de748f73fec6dd74bc58f0f6516975bd644e302d
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
### 0.6.0 (2026/11/22)
|
|
2
|
+
|
|
3
|
+
* [#31](https://github.com/dblock/ruby-link-checker/pull/31): Updated ruby-gem-release to 0.5.0 - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/Copilot).
|
|
4
|
+
* [#38](https://github.com/dblock/ruby-link-checker/pull/38): Added `blocked_status_codes` (default `[401, 403, 429, 999]`) and `detect_challenges` (enabled by default, recognizes Cloudflare's `cf-mitigated: challenge`) to report links blocked by bot walls as `:blocked` instead of `:failure`; set `blocked_status_codes: []` and `detect_challenges: false` to restore the previous behavior - [@dblock](https://github.com/dblock).
|
|
5
|
+
* [#39](https://github.com/dblock/ruby-link-checker/pull/39): Added a `:complete` event published once per link with its final result and documented which events fire per attempt and which fire once per link - [@dblock](https://github.com/dblock).
|
|
6
|
+
* [#40](https://github.com/dblock/ruby-link-checker/pull/40): Added `max_concurrency_per_host` and `min_host_interval` to limit concurrent and back-to-back requests to the same host - [@dblock](https://github.com/dblock).
|
|
7
|
+
* [#42](https://github.com/dblock/ruby-link-checker/pull/42): Added `host_headers` to send headers to specific hosts, chosen per redirect hop so that credentials are not forwarded to other hosts - [@dblock](https://github.com/dblock).
|
|
8
|
+
* [#43](https://github.com/dblock/ruby-link-checker/pull/43): Added `result.redirects` and `result.permanent_redirect?` with the redirect hops that led to a final result, persisted in the cache - [@dblock](https://github.com/dblock).
|
|
9
|
+
* [#44](https://github.com/dblock/ruby-link-checker/pull/44): Added an optional `blocked_ttl` cache setting to reuse blocked results for a shorter period than successes - [@dblock](https://github.com/dblock).
|
|
10
|
+
* [#46](https://github.com/dblock/ruby-link-checker/pull/46): Added per-host exponential backoff for rate-limited (429) responses with `rate_limit_retries` (default `2`, `0` disables), `rate_limit_backoff`, `max_rate_limit_delay`, and `rate_limit_status_codes`, honoring `Retry-After` - [@dblock](https://github.com/dblock).
|
|
11
|
+
* [#47](https://github.com/dblock/ruby-link-checker/pull/47): Added a logo and wordmark, excluded from the gem package - [@dblock](https://github.com/dblock).
|
|
12
|
+
* [#48](https://github.com/dblock/ruby-link-checker/pull/48): Updated ruby-gem-release to 0.9.0, added Commonmarker for Markdown changelogs, and added a release dry run - [@dblock](https://github.com/dblock).
|
|
13
|
+
* [#49](https://github.com/dblock/ruby-link-checker/pull/49): Fixed loading the gem on Ruby 4.0 by declaring the `logger` dependency, and added a `Package` workflow that installs the built gem in a clean environment - [@dblock](https://github.com/dblock).
|
|
14
|
+
* [#50](https://github.com/dblock/ruby-link-checker/pull/50): Ignored URL fragments in deduplication and cache identity, so links that differ only by `#fragment` share one check - [@dblock](https://github.com/dblock).
|
|
15
|
+
* [#51](https://github.com/dblock/ruby-link-checker/pull/51): Added `result.outcome`, a stable `result.reason`, and a serializable `result.to_h` - [@dblock](https://github.com/dblock).
|
|
16
|
+
* [#52](https://github.com/dblock/ruby-link-checker/pull/52): Added cache pruning with `cache: { prune: true }` and `checker.prune_cache!` to remove entries for links that are no longer checked - [@dblock](https://github.com/dblock).
|
|
17
|
+
* [#53](https://github.com/dblock/ruby-link-checker/pull/53): Packaged only the library and its documentation, disabled the local Bundler `rake release` tasks, pinned the release workflow to a commit, updated `actions/checkout`, and removed the `git` pin now that Danger supports git 6 - [@dblock](https://github.com/dblock).
|
|
18
|
+
* [#54](https://github.com/dblock/ruby-link-checker/pull/54): Added UPGRADING.md with instructions for upgrading to 0.6.0 - [@dblock](https://github.com/dblock).
|
|
19
|
+
|
|
1
20
|
### 0.5.1 (2026/11/08)
|
|
2
21
|
|
|
3
22
|
* [#30](https://github.com/dblock/ruby-link-checker/pull/30): Updated ruby-gem-release to 0.3.1 - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/Copilot).
|
data/README.md
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
<picture>
|
|
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>
|
|
5
|
+
|
|
1
6
|
Fast Ruby Link Checker
|
|
2
7
|
======================
|
|
3
8
|
|
|
@@ -22,6 +27,9 @@ A fast Ruby link checker with support for multiple HTTP libraries. Does not pars
|
|
|
22
27
|
- [Redirects](#redirects)
|
|
23
28
|
- [Retry After](#retry-after)
|
|
24
29
|
- [Retry Policy](#retry-policy)
|
|
30
|
+
- [Rate Limits](#rate-limits)
|
|
31
|
+
- [Blocked Results](#blocked-results)
|
|
32
|
+
- [Per-Host Limits](#per-host-limits)
|
|
25
33
|
- [Results](#results)
|
|
26
34
|
- [Methods](#methods)
|
|
27
35
|
- [Deduplication](#deduplication)
|
|
@@ -192,7 +200,7 @@ checker = LinkChecker::Async::HTTP::Checker.new(max_concurrency: 25)
|
|
|
192
200
|
|
|
193
201
|
#### [LinkChecker::Net::HTTP](lib/ruby-link-checker/net/http/checker.rb)
|
|
194
202
|
|
|
195
|
-
Slow, sequential checker.
|
|
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).
|
|
196
204
|
|
|
197
205
|
```ruby
|
|
198
206
|
require 'net/http'
|
|
@@ -228,7 +236,7 @@ end
|
|
|
228
236
|
|
|
229
237
|
#### Retries
|
|
230
238
|
|
|
231
|
-
By default link checkers
|
|
239
|
+
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`.
|
|
232
240
|
|
|
233
241
|
```ruby
|
|
234
242
|
checker = LinkChecker::Net::HTTP::Checker.new(retries: 1)
|
|
@@ -242,6 +250,14 @@ Checkers follow up to 20 redirects per attempt and detect redirect loops. Relati
|
|
|
242
250
|
checker = LinkChecker::Typhoeus::Hydra::Checker.new(max_redirects: 10)
|
|
243
251
|
```
|
|
244
252
|
|
|
253
|
+
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.
|
|
254
|
+
|
|
255
|
+
```ruby
|
|
256
|
+
checker.on :success do |result|
|
|
257
|
+
puts "#{result.uri} moved permanently to #{result.result_uri}" if result.permanent_redirect?
|
|
258
|
+
end
|
|
259
|
+
```
|
|
260
|
+
|
|
245
261
|
#### Retry After
|
|
246
262
|
|
|
247
263
|
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).
|
|
@@ -267,16 +283,68 @@ Customize `retry_status_codes` to choose retryable HTTP statuses. Set it to `nil
|
|
|
267
283
|
checker = LinkChecker::Net::HTTP::Checker.new(retries: 1, retry_status_codes: [429, 503])
|
|
268
284
|
```
|
|
269
285
|
|
|
286
|
+
#### Rate Limits
|
|
287
|
+
|
|
288
|
+
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.
|
|
289
|
+
|
|
290
|
+
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.
|
|
291
|
+
|
|
292
|
+
```ruby
|
|
293
|
+
checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
294
|
+
rate_limit_retries: 3,
|
|
295
|
+
rate_limit_backoff: 2,
|
|
296
|
+
max_rate_limit_delay: 60,
|
|
297
|
+
rate_limit_status_codes: [429, 503]
|
|
298
|
+
)
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
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.
|
|
302
|
+
|
|
303
|
+
#### Blocked Results
|
|
304
|
+
|
|
305
|
+
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.
|
|
306
|
+
|
|
307
|
+
```ruby
|
|
308
|
+
checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
309
|
+
blocked_status_codes: [403, 999]
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
checker.on :blocked do |result|
|
|
313
|
+
puts "UNVERIFIED: #{result}" # result.blocked? == true, result.failure? == false
|
|
314
|
+
end
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
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).
|
|
318
|
+
|
|
319
|
+
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.
|
|
320
|
+
|
|
321
|
+
```ruby
|
|
322
|
+
checker = LinkChecker::Typhoeus::Hydra::Checker.new(detect_challenges: false)
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
#### Per-Host Limits
|
|
326
|
+
|
|
327
|
+
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.
|
|
328
|
+
|
|
329
|
+
```ruby
|
|
330
|
+
checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
331
|
+
max_concurrency_per_host: 2,
|
|
332
|
+
min_host_interval: 0.5
|
|
333
|
+
)
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
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.
|
|
337
|
+
|
|
270
338
|
#### Results
|
|
271
339
|
|
|
272
340
|
By default checkers collect results.
|
|
273
341
|
|
|
274
342
|
```ruby
|
|
275
|
-
checker = LinkChecker::Net::HTTP::Checker.new
|
|
343
|
+
checker = LinkChecker::Net::HTTP::Checker.new
|
|
276
344
|
...
|
|
277
345
|
checker.run
|
|
278
346
|
|
|
279
|
-
checker.results # => { error: [...], failure: [...], success: [...] }
|
|
347
|
+
checker.results # => { error: [...], failure: [...], success: [...], blocked: [...] }
|
|
280
348
|
```
|
|
281
349
|
|
|
282
350
|
You can disable this with `results: false`.
|
|
@@ -310,11 +378,20 @@ checker.check('https://www.example.org', location: 'two.html')
|
|
|
310
378
|
checker.run # one check, two sets of result events
|
|
311
379
|
```
|
|
312
380
|
|
|
313
|
-
Identity includes the
|
|
381
|
+
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.
|
|
314
382
|
|
|
315
383
|
#### Caching
|
|
316
384
|
|
|
317
|
-
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.
|
|
385
|
+
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.
|
|
386
|
+
|
|
387
|
+
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`.
|
|
388
|
+
|
|
389
|
+
```ruby
|
|
390
|
+
checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
391
|
+
blocked_status_codes: [401, 403, 429, 999],
|
|
392
|
+
cache: { file: 'tmp/link-checker/cache.json', ttl: 7 * 24 * 3600, blocked_ttl: 24 * 3600 }
|
|
393
|
+
)
|
|
394
|
+
``` Cache writes are atomic and locked, and unreadable or malformed files fall back to normal checking.
|
|
318
395
|
|
|
319
396
|
```ruby
|
|
320
397
|
checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
@@ -323,7 +400,15 @@ checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
|
323
400
|
)
|
|
324
401
|
```
|
|
325
402
|
|
|
326
|
-
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, and metadata; native request/response objects and headers are not persisted.
|
|
403
|
+
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.
|
|
404
|
+
|
|
405
|
+
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.
|
|
406
|
+
|
|
407
|
+
```ruby
|
|
408
|
+
checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
409
|
+
cache: { file: 'tmp/link-checker/cache.json', ttl: 7 * 24 * 3600, prune: true }
|
|
410
|
+
)
|
|
411
|
+
```
|
|
327
412
|
|
|
328
413
|
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.
|
|
329
414
|
|
|
@@ -357,6 +442,20 @@ checker = LinkChecker::Net::HTTP::Checker.new(
|
|
|
357
442
|
)
|
|
358
443
|
```
|
|
359
444
|
|
|
445
|
+
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`.
|
|
446
|
+
|
|
447
|
+
```ruby
|
|
448
|
+
checker = LinkChecker::Typhoeus::Hydra::Checker.new(
|
|
449
|
+
host_headers: {
|
|
450
|
+
'www.npmjs.com' => { 'Accept' => 'text/html' },
|
|
451
|
+
'api.github.com' => { 'Authorization' => "Bearer #{ENV.fetch('GITHUB_TOKEN')}" },
|
|
452
|
+
/(\A|\.)medium\.com\z/ => { 'Cookie' => 'consent=1' }
|
|
453
|
+
}
|
|
454
|
+
)
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
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. Host headers are part of the [deduplication](#deduplication) and [caching](#caching) identity, and so are their values: the cache file stores a hash of them, not the values themselves.
|
|
458
|
+
|
|
360
459
|
### Global Configuration
|
|
361
460
|
|
|
362
461
|
All options can also be configured globally.
|
|
@@ -375,8 +474,8 @@ end
|
|
|
375
474
|
Events enable processing of results as they become available.
|
|
376
475
|
|
|
377
476
|
```ruby
|
|
378
|
-
checker.on :
|
|
379
|
-
puts result #
|
|
477
|
+
checker.on :complete do |result|
|
|
478
|
+
puts result # the final result of each link
|
|
380
479
|
end
|
|
381
480
|
|
|
382
481
|
checker.on :error, :failure do |result|
|
|
@@ -384,15 +483,33 @@ checker.on :error, :failure do |result|
|
|
|
384
483
|
end
|
|
385
484
|
```
|
|
386
485
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
| Event
|
|
390
|
-
|
|
391
|
-
| :
|
|
392
|
-
| :
|
|
393
|
-
| :
|
|
394
|
-
| :
|
|
395
|
-
| :
|
|
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.
|
|
396
513
|
|
|
397
514
|
Events are called with results, which contain the following properties.
|
|
398
515
|
|
|
@@ -408,6 +525,23 @@ Events are called with results, which contain the following properties.
|
|
|
408
525
|
| :error_type | `:timeout`, `:connection`, or `:other` for errors; otherwise `nil`. |
|
|
409
526
|
| :response_headers | Response headers, accessible using `[]`. |
|
|
410
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). |
|
|
531
|
+
|
|
532
|
+
| :outcome | `:success`, `:failure`, `:blocked`, or `:error` for a final result, `:redirect` for a redirect hop. |
|
|
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.
|
|
539
|
+
|
|
540
|
+
```ruby
|
|
541
|
+
checker.on :complete do |result|
|
|
542
|
+
puts JSON.generate(result.to_h) unless result.success?
|
|
543
|
+
end
|
|
544
|
+
```
|
|
411
545
|
|
|
412
546
|
See [result.rb](lib/ruby-link-checker/result.rb) for more details.
|
|
413
547
|
|
|
@@ -423,7 +557,7 @@ Anecdotal results show `LinkChecker::Async::HTTP` using significantly less CPU t
|
|
|
423
557
|
|
|
424
558
|
## Contributing
|
|
425
559
|
|
|
426
|
-
You're encouraged to contribute to ruby-link-checker. See [CONTRIBUTING](CONTRIBUTING.md) for details.
|
|
560
|
+
You're encouraged to contribute to ruby-link-checker. See [CONTRIBUTING](CONTRIBUTING.md) for details, [CHANGELOG](CHANGELOG.md) for changes, [UPGRADING](UPGRADING.md) for upgrade instructions, and [RELEASING](RELEASING.md) for how releases are made.
|
|
427
561
|
|
|
428
562
|
## Copyright and License
|
|
429
563
|
|
data/UPGRADING.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Upgrading ruby-link-checker
|
|
2
|
+
|
|
3
|
+
## Upgrading to 0.6.0
|
|
4
|
+
|
|
5
|
+
### Blocked Results
|
|
6
|
+
|
|
7
|
+
Links whose final response is 401, 403, 429, or 999, or carries a known bot challenge, such as Cloudflare's `cf-mitigated: challenge`, are now reported as blocked instead of failed. They publish `:blocked` instead of `:failure`, have `blocked? == true` and `failure? == false`, and are collected in `results[:blocked]`, a new key in the results hash.
|
|
8
|
+
|
|
9
|
+
If you fail a build on `:failure` or `results[:failure]`, decide whether blocked links should fail it too, and handle `:blocked` or subscribe to `:complete`, which fires once per link with any outcome. To restore the previous behavior, report blocked links as failures:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
LinkChecker.configure do |config|
|
|
13
|
+
config.blocked_status_codes = []
|
|
14
|
+
config.detect_challenges = false
|
|
15
|
+
end
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
See [Blocked Results](README.md#blocked-results) for details.
|
|
19
|
+
|
|
20
|
+
### Rate Limits
|
|
21
|
+
|
|
22
|
+
Rate-limited responses (429) are now retried up to twice by default, with exponential backoff per host that honors `Retry-After`, even when `retries` is `0`. A rate-limited link can take about 3 more seconds to report, and other links to the same host wait during the backoff. To restore the previous behavior, disable it:
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
LinkChecker.configure do |config|
|
|
26
|
+
config.rate_limit_retries = 0
|
|
27
|
+
end
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
See [Rate Limits](README.md#rate-limits) for details.
|
|
31
|
+
|
|
32
|
+
### Events
|
|
33
|
+
|
|
34
|
+
A new `:complete` event fires once per link with its final result. `:result` still fires for every attempt, including redirect hops and a failed `HEAD` before a `GET`. If you report links from `:result`, switch to `:complete` or the outcome events. See [Callbacks and Events](README.md#callbacks-and-events).
|
|
35
|
+
|
|
36
|
+
### Caching
|
|
37
|
+
|
|
38
|
+
The cache file format changed to store redirect hops and blocked results. Caches written by earlier versions are ignored and rewritten, so the first run after upgrading checks every link again. Links that differ only by `#fragment` now share one cache entry and one check.
|
|
39
|
+
|
|
40
|
+
### LinkChecker::Net::HTTP
|
|
41
|
+
|
|
42
|
+
`LinkChecker::Net::HTTP::Checker` now responds to `run`, which finishes a run, for example to [prune the cache](README.md#caching). Links are still checked as they are submitted, so calling `run` is optional unless you prune the cache automatically.
|
|
43
|
+
|
|
44
|
+
### Ruby 4.0
|
|
45
|
+
|
|
46
|
+
The gem now declares its `logger` dependency, which Ruby 4.0 no longer includes by default. Earlier versions failed to load on Ruby 4.0 unless another gem in the bundle depended on `logger`.
|
|
@@ -3,7 +3,7 @@ module LinkChecker
|
|
|
3
3
|
module HTTP
|
|
4
4
|
class Task < ::LinkChecker::Task
|
|
5
5
|
def run!
|
|
6
|
-
checker._queue(delay: delay) { _run! }
|
|
6
|
+
checker._queue(delay: delay, host: checker._host_key(uri)) { _run! }
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
private
|
|
@@ -11,7 +11,7 @@ module LinkChecker
|
|
|
11
11
|
def _run!
|
|
12
12
|
client = checker._client_for(uri)
|
|
13
13
|
headers = ::Protocol::HTTP::Headers[
|
|
14
|
-
|
|
14
|
+
checker.request_headers_for(uri).map { |key, value| [key.downcase, value] }
|
|
15
15
|
]
|
|
16
16
|
request = ::Protocol::HTTP::Request[method, uri.request_uri, headers]
|
|
17
17
|
response = _call(client, request)
|
|
@@ -42,6 +42,7 @@ module LinkChecker
|
|
|
42
42
|
end
|
|
43
43
|
@queue = []
|
|
44
44
|
@clients = {}
|
|
45
|
+
@host_semaphores = {}
|
|
45
46
|
if max_concurrency && (!max_concurrency.is_a?(Integer) || max_concurrency < 1)
|
|
46
47
|
raise ArgumentError, "Invalid concurrency limit: #{max_concurrency}"
|
|
47
48
|
end
|
|
@@ -58,20 +59,26 @@ module LinkChecker
|
|
|
58
59
|
@semaphore = ::Async::Semaphore.new(max_concurrency) if max_concurrency
|
|
59
60
|
_flush!
|
|
60
61
|
end
|
|
62
|
+
_finish_run
|
|
61
63
|
ensure
|
|
62
64
|
@barrier = nil
|
|
63
65
|
@semaphore = nil
|
|
66
|
+
@host_semaphores = {}
|
|
64
67
|
_close_clients!
|
|
65
68
|
end
|
|
66
69
|
|
|
67
|
-
def _queue(delay: 0, &block)
|
|
70
|
+
def _queue(delay: 0, host: nil, &block)
|
|
68
71
|
if @barrier
|
|
69
72
|
@barrier.async do
|
|
70
73
|
sleep(delay) if delay.positive?
|
|
71
|
-
|
|
74
|
+
_with_host_limit(host) do
|
|
75
|
+
host_wait = _reserve_host_slot(host)
|
|
76
|
+
sleep(host_wait) if host_wait.positive?
|
|
77
|
+
@semaphore ? @semaphore.acquire(&block) : block.call
|
|
78
|
+
end
|
|
72
79
|
end
|
|
73
80
|
else
|
|
74
|
-
@queue << [delay, block]
|
|
81
|
+
@queue << [delay, host, block]
|
|
75
82
|
end
|
|
76
83
|
end
|
|
77
84
|
|
|
@@ -105,7 +112,13 @@ module LinkChecker
|
|
|
105
112
|
def _flush!
|
|
106
113
|
queue = @queue
|
|
107
114
|
@queue = []
|
|
108
|
-
queue.each { |delay, block| _queue(delay: delay, &block) }
|
|
115
|
+
queue.each { |delay, host, block| _queue(delay: delay, host: host, &block) }
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def _with_host_limit(host, &)
|
|
119
|
+
return yield unless host && max_concurrency_per_host
|
|
120
|
+
|
|
121
|
+
(@host_semaphores[host] ||= ::Async::Semaphore.new(max_concurrency_per_host)).acquire(&)
|
|
109
122
|
end
|
|
110
123
|
|
|
111
124
|
def _close_clients!
|
|
@@ -8,21 +8,26 @@ require 'uri'
|
|
|
8
8
|
|
|
9
9
|
module LinkChecker
|
|
10
10
|
class CachedResult < Result
|
|
11
|
-
attr_reader :code
|
|
11
|
+
attr_reader :code, :challenge
|
|
12
12
|
|
|
13
13
|
def initialize(data, uri)
|
|
14
14
|
@code = data.fetch('code')
|
|
15
15
|
super(URI(data.fetch('result_uri')), data.fetch('method'), URI(uri.to_s))
|
|
16
|
+
@redirects = data.fetch('redirects').map do |hop_uri, code, to|
|
|
17
|
+
Redirect.new(uri: URI(hop_uri), code: code, to: URI(to))
|
|
18
|
+
end.freeze
|
|
19
|
+
@blocked = data['blocked'] == true
|
|
20
|
+
@challenge = data['challenge'] if data['challenge'].is_a?(String)
|
|
16
21
|
@cached = true
|
|
17
22
|
end
|
|
18
23
|
|
|
19
24
|
def success?
|
|
20
|
-
|
|
25
|
+
!blocked?
|
|
21
26
|
end
|
|
22
27
|
end
|
|
23
28
|
|
|
24
29
|
class Cache
|
|
25
|
-
VERSION =
|
|
30
|
+
VERSION = 2
|
|
26
31
|
|
|
27
32
|
def initialize(options, logger:)
|
|
28
33
|
unless options.is_a?(Hash) && options[:file].is_a?(String) && !options[:file].strip.empty?
|
|
@@ -30,21 +35,33 @@ module LinkChecker
|
|
|
30
35
|
end
|
|
31
36
|
|
|
32
37
|
@ttl = options[:ttl]
|
|
33
|
-
|
|
34
|
-
|
|
38
|
+
raise ArgumentError, 'Cache requires a positive, finite TTL in seconds' unless seconds?(@ttl)
|
|
39
|
+
|
|
40
|
+
@blocked_ttl = options[:blocked_ttl]
|
|
41
|
+
unless @blocked_ttl.nil? || seconds?(@blocked_ttl)
|
|
42
|
+
raise ArgumentError, 'Cache requires a positive, finite blocked TTL in seconds'
|
|
35
43
|
end
|
|
36
44
|
|
|
45
|
+
@prune = options.fetch(:prune, false)
|
|
46
|
+
raise ArgumentError, 'Cache prune must be true or false' unless [true, false].include?(@prune)
|
|
47
|
+
|
|
37
48
|
@file = File.expand_path(options[:file])
|
|
38
49
|
@logger = logger
|
|
50
|
+
@seen = Set.new
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Whether to prune entries that were not used when a run finishes.
|
|
54
|
+
def prune?
|
|
55
|
+
@prune
|
|
39
56
|
end
|
|
40
57
|
|
|
41
58
|
def fetch(key, uri)
|
|
59
|
+
@seen << key
|
|
42
60
|
data = read_entries[key]
|
|
43
|
-
return unless fresh?(data) && data['code'].is_a?(Integer)
|
|
44
|
-
return unless data['
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
return unless %w[http https].include?(parsed.scheme) && parsed.host && !parsed.userinfo
|
|
61
|
+
return unless fresh?(data) && data['code'].is_a?(Integer)
|
|
62
|
+
return unless blocked?(data) ? data['code'].between?(100, 999) : data['code'].between?(200, 299)
|
|
63
|
+
return unless data['method'].is_a?(String) && safe_uri?(data['result_uri'])
|
|
64
|
+
return unless valid_redirects?(data['redirects'])
|
|
48
65
|
|
|
49
66
|
CachedResult.new(data, uri)
|
|
50
67
|
rescue URI::InvalidURIError
|
|
@@ -52,38 +69,90 @@ module LinkChecker
|
|
|
52
69
|
end
|
|
53
70
|
|
|
54
71
|
def store(key, result)
|
|
55
|
-
|
|
72
|
+
@seen << key
|
|
73
|
+
return unless result.success? || (result.blocked? && @blocked_ttl)
|
|
56
74
|
return if URI(result.result_uri.to_s).userinfo
|
|
75
|
+
return if result.redirects.any? { |hop| URI(hop.uri.to_s).userinfo || URI(hop.to.to_s).userinfo }
|
|
57
76
|
|
|
58
|
-
|
|
59
|
-
FileUtils.mkdir_p(directory)
|
|
60
|
-
File.open("#{@file}.lock", File::RDWR | File::CREAT, 0o600) do |lock|
|
|
61
|
-
lock.flock(File::LOCK_EX)
|
|
62
|
-
entries = read_entries.select { |_entry_key, data| fresh?(data) }
|
|
77
|
+
update_entries do |entries|
|
|
63
78
|
entries[key] = {
|
|
64
79
|
'checked_at' => Time.now.to_f,
|
|
65
80
|
'method' => result.method,
|
|
66
81
|
'code' => result.code,
|
|
67
|
-
'result_uri' => result.result_uri.to_s
|
|
82
|
+
'result_uri' => result.result_uri.to_s,
|
|
83
|
+
'redirects' => result.redirects.map { |hop| [hop.uri.to_s, hop.code, hop.to.to_s] }
|
|
68
84
|
}
|
|
85
|
+
entries[key].merge!('blocked' => true, 'challenge' => result.challenge) if result.blocked?
|
|
86
|
+
entries
|
|
87
|
+
end
|
|
88
|
+
rescue IOError, SystemCallError, JSON::GeneratorError
|
|
89
|
+
@logger.warn("Unable to write link cache #{@file}; result was not cached")
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Removes entries that this cache instance neither read nor wrote, and expired entries.
|
|
93
|
+
# Returns the number of entries removed.
|
|
94
|
+
def prune!
|
|
95
|
+
return 0 unless File.exist?(@file)
|
|
96
|
+
|
|
97
|
+
removed = 0
|
|
98
|
+
update_entries do |entries|
|
|
99
|
+
kept = entries.slice(*@seen)
|
|
100
|
+
removed = read_entries.size - kept.size
|
|
101
|
+
kept
|
|
102
|
+
end
|
|
103
|
+
@logger.info("Pruned #{removed} unused #{removed == 1 ? 'entry' : 'entries'} from link cache #{@file}")
|
|
104
|
+
removed
|
|
105
|
+
rescue IOError, SystemCallError, JSON::GeneratorError
|
|
106
|
+
@logger.warn("Unable to prune link cache #{@file}")
|
|
107
|
+
0
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
private
|
|
111
|
+
|
|
112
|
+
# Atomically replaces the fresh entries with those returned by the block, under an exclusive lock.
|
|
113
|
+
def update_entries
|
|
114
|
+
directory = File.dirname(@file)
|
|
115
|
+
FileUtils.mkdir_p(directory)
|
|
116
|
+
File.open("#{@file}.lock", File::RDWR | File::CREAT, 0o600) do |lock|
|
|
117
|
+
lock.flock(File::LOCK_EX)
|
|
118
|
+
entries = yield(read_entries.select { |_entry_key, data| fresh?(data) })
|
|
69
119
|
Tempfile.create(['link-checker-', '.json'], directory) do |file|
|
|
70
120
|
file.write(JSON.generate('version' => VERSION, 'entries' => entries))
|
|
71
121
|
file.flush
|
|
72
122
|
File.rename(file.path, @file)
|
|
73
123
|
end
|
|
74
124
|
end
|
|
75
|
-
rescue IOError, SystemCallError, JSON::GeneratorError
|
|
76
|
-
@logger.warn("Unable to write link cache #{@file}; result was not cached")
|
|
77
125
|
end
|
|
78
126
|
|
|
79
|
-
|
|
127
|
+
def seconds?(value)
|
|
128
|
+
value.is_a?(Numeric) && value.real? && value.finite? && value.positive?
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def safe_uri?(value)
|
|
132
|
+
return false unless value.is_a?(String)
|
|
133
|
+
|
|
134
|
+
parsed = URI(value)
|
|
135
|
+
%w[http https].include?(parsed.scheme) && parsed.host && !parsed.userinfo
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def valid_redirects?(redirects)
|
|
139
|
+
redirects.is_a?(Array) && redirects.all? do |hop|
|
|
140
|
+
hop.is_a?(Array) && hop.size == 3 && safe_uri?(hop[0]) && hop[1].is_a?(Integer) && safe_uri?(hop[2])
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def blocked?(data)
|
|
145
|
+
data['blocked'] == true
|
|
146
|
+
end
|
|
80
147
|
|
|
148
|
+
# Successes expire after ttl and blocked results after blocked_ttl; blocked entries are stale without one.
|
|
81
149
|
def fresh?(data)
|
|
82
150
|
return false unless data.is_a?(Hash)
|
|
83
151
|
|
|
152
|
+
ttl = blocked?(data) ? @blocked_ttl : @ttl
|
|
84
153
|
timestamp = data['checked_at']
|
|
85
154
|
now = Time.now.to_f
|
|
86
|
-
timestamp.is_a?(Numeric) && timestamp.finite? && timestamp <= now && now - timestamp <
|
|
155
|
+
!ttl.nil? && timestamp.is_a?(Numeric) && timestamp.finite? && timestamp <= now && now - timestamp < ttl
|
|
87
156
|
end
|
|
88
157
|
|
|
89
158
|
def read_entries
|