ruby-link-checker 0.4.0 → 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.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +34 -0
  3. data/LICENSE.md +0 -0
  4. data/README.md +227 -19
  5. data/UPGRADING.md +46 -0
  6. data/lib/ruby-link-checker/async/http/checker.rb +46 -7
  7. data/lib/ruby-link-checker/async/http/config.rb +4 -0
  8. data/lib/ruby-link-checker/async/http/result.rb +5 -1
  9. data/lib/ruby-link-checker/async/http.rb +0 -0
  10. data/lib/ruby-link-checker/cache.rb +170 -0
  11. data/lib/ruby-link-checker/callbacks.rb +0 -0
  12. data/lib/ruby-link-checker/checker.rb +200 -5
  13. data/lib/ruby-link-checker/config.rb +141 -0
  14. data/lib/ruby-link-checker/errors/base_error.rb +0 -0
  15. data/lib/ruby-link-checker/errors/connection_error.rb +23 -0
  16. data/lib/ruby-link-checker/errors/redirect_limit_error.rb +15 -0
  17. data/lib/ruby-link-checker/errors/redirect_loop_error.rb +0 -0
  18. data/lib/ruby-link-checker/errors.rb +2 -0
  19. data/lib/ruby-link-checker/logger.rb +0 -0
  20. data/lib/ruby-link-checker/net/http/checker.rb +5 -3
  21. data/lib/ruby-link-checker/net/http/config.rb +0 -0
  22. data/lib/ruby-link-checker/net/http/result.rb +5 -1
  23. data/lib/ruby-link-checker/net/http.rb +0 -0
  24. data/lib/ruby-link-checker/redirect.rb +11 -0
  25. data/lib/ruby-link-checker/result.rb +120 -0
  26. data/lib/ruby-link-checker/task.rb +2 -0
  27. data/lib/ruby-link-checker/tasks.rb +53 -7
  28. data/lib/ruby-link-checker/typhoeus/hydra/checker.rb +79 -9
  29. data/lib/ruby-link-checker/typhoeus/hydra/config.rb +0 -0
  30. data/lib/ruby-link-checker/typhoeus/hydra/result.rb +5 -1
  31. data/lib/ruby-link-checker/typhoeus/hydra.rb +0 -0
  32. data/lib/ruby-link-checker/version.rb +1 -1
  33. data/lib/ruby-link-checker.rb +2 -0
  34. data/lib/ruby_link_checker.rb +0 -0
  35. metadata +26 -72
  36. data/CONTRIBUTING.md +0 -125
  37. data/Dangerfile +0 -6
  38. data/Gemfile +0 -24
  39. data/Gemfile.lock +0 -334
  40. data/RELEASING.md +0 -60
  41. data/Rakefile +0 -18
  42. data/benchmarks/check.rb +0 -38
  43. data/benchmarks/data/opensearch.org/links.txt +0 -4270
  44. data/benchmarks/data/opensearch.org/small.txt +0 -100
  45. data/coverage/assets/0.13.2/DataTables-1.10.20/images/sort_asc.png +0 -0
  46. data/coverage/assets/0.13.2/DataTables-1.10.20/images/sort_asc_disabled.png +0 -0
  47. data/coverage/assets/0.13.2/DataTables-1.10.20/images/sort_both.png +0 -0
  48. data/coverage/assets/0.13.2/DataTables-1.10.20/images/sort_desc.png +0 -0
  49. data/coverage/assets/0.13.2/DataTables-1.10.20/images/sort_desc_disabled.png +0 -0
  50. data/coverage/assets/0.13.2/application.css +0 -1
  51. data/coverage/assets/0.13.2/application.js +0 -7
  52. data/coverage/assets/0.13.2/colorbox/border.png +0 -0
  53. data/coverage/assets/0.13.2/colorbox/controls.png +0 -0
  54. data/coverage/assets/0.13.2/colorbox/loading.gif +0 -0
  55. data/coverage/assets/0.13.2/colorbox/loading_background.png +0 -0
  56. data/coverage/assets/0.13.2/favicon_green.png +0 -0
  57. data/coverage/assets/0.13.2/favicon_red.png +0 -0
  58. data/coverage/assets/0.13.2/favicon_yellow.png +0 -0
  59. data/coverage/assets/0.13.2/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  60. data/coverage/assets/0.13.2/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  61. data/coverage/assets/0.13.2/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  62. data/coverage/assets/0.13.2/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  63. data/coverage/assets/0.13.2/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  64. data/coverage/assets/0.13.2/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  65. data/coverage/assets/0.13.2/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  66. data/coverage/assets/0.13.2/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  67. data/coverage/assets/0.13.2/images/ui-icons_222222_256x240.png +0 -0
  68. data/coverage/assets/0.13.2/images/ui-icons_2e83ff_256x240.png +0 -0
  69. data/coverage/assets/0.13.2/images/ui-icons_454545_256x240.png +0 -0
  70. data/coverage/assets/0.13.2/images/ui-icons_888888_256x240.png +0 -0
  71. data/coverage/assets/0.13.2/images/ui-icons_cd0a0a_256x240.png +0 -0
  72. data/coverage/assets/0.13.2/loading.gif +0 -0
  73. data/coverage/assets/0.13.2/magnify.png +0 -0
  74. data/coverage/index.html +0 -10746
  75. data/ruby-link-checker.gemspec +0 -22
  76. data/spec/fixtures/200.yml +0 -65
  77. data/spec/fixtures/301_200.yml +0 -102
  78. data/spec/fixtures/301_301.yml +0 -114
  79. data/spec/fixtures/301_400_301_200.yml +0 -185
  80. data/spec/fixtures/301_403.yml +0 -78
  81. data/spec/fixtures/308_429.yml +0 -108
  82. data/spec/fixtures/404.yml +0 -57
  83. data/spec/fixtures/404_200.yml +0 -89
  84. data/spec/fixtures/404_404.yml +0 -81
  85. data/spec/fixtures/429_200.yml +0 -135
  86. data/spec/fixtures/429_429_200.yml +0 -201
  87. data/spec/ruby-link-checker/async/http/checker_spec.rb +0 -99
  88. data/spec/ruby-link-checker/callbacks_spec.rb +0 -76
  89. data/spec/ruby-link-checker/checker_spec.rb +0 -10
  90. data/spec/ruby-link-checker/config_spec.rb +0 -39
  91. data/spec/ruby-link-checker/errors/redirect_loop_error_spec.rb +0 -18
  92. data/spec/ruby-link-checker/net/http/checker_spec.rb +0 -78
  93. data/spec/ruby-link-checker/result_error_spec.rb +0 -17
  94. data/spec/ruby-link-checker/result_spec.rb +0 -41
  95. data/spec/ruby-link-checker/task_spec.rb +0 -20
  96. data/spec/ruby-link-checker/tasks_spec.rb +0 -47
  97. data/spec/ruby-link-checker/typhoeus/hydra/checker_spec.rb +0 -93
  98. data/spec/ruby-link-checker/version_spec.rb +0 -9
  99. data/spec/spec_helper.rb +0 -19
  100. data/spec/support/config.rb +0 -8
  101. data/spec/support/link_checker.rb +0 -369
  102. data/spec/support/vcr.rb +0 -13
  103. data/spec/support/with_result.rb +0 -15
  104. data/spec/support/with_url.rb +0 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 897f3eea2de5db8a96be6cb0479ffcac9cfb626266dddf48c3b76faa7cdca60b
4
- data.tar.gz: 06a8c2535dd9d97e6c46d5a8ccf34eee5b17257a6fff31b31d9e9318f501c945
3
+ metadata.gz: 5797b1459b6dfe9254188079c8940286e4b49437808cf5f74b11f6423f2d5677
4
+ data.tar.gz: b2cfb5abb84a0226f1067f74ecaeef5152a3b5c697ad8962b9503f57854f0f4a
5
5
  SHA512:
6
- metadata.gz: b642d6859e0a5133fc1f29a35e1be83159e08df750d809c7c847920861da618edd19519425e6dac60914610b5f9584929f8bfe78b528af049c456eaae9e76048
7
- data.tar.gz: 424910c0aa3167ffcad39e87c143a2fef869db4c8e7050d3b056aabc47bbb34b9ef6651f848a277ae638858f4659f7887e5481e7e5b4490e6d26da298d43f440
6
+ metadata.gz: 546bc8134e07bab2f58bc50ea3fe9f60e2f79252301b41321ca4b08b4c8600c646da30907b742fffb2f8af46e24fa8531ccd4c969bef3575fbe33872acd8c765
7
+ data.tar.gz: 3894bcd493c8ea47b894f22293ab04ffe347333eb96e974df2ce9cc8082cd046742ad2e48a31e6a0c43a6cb2de748f73fec6dd74bc58f0f6516975bd644e302d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,37 @@
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
+
20
+ ### 0.5.1 (2026/11/08)
21
+
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).
23
+ * [#29](https://github.com/dblock/ruby-link-checker/pull/29): Removed accidentally tracked Bundler artifacts, ignored local Bundler configuration and installed gems, and restricted gem packaging to tracked files - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/Copilot).
24
+ * [#28](https://github.com/dblock/ruby-link-checker/pull/28): Updated ruby-gem-release to 0.3.0 and removed the unsupported dry-run release input and instructions - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/Copilot).
25
+
26
+ ### 0.5.0 (2026/11/07)
27
+
28
+ * [#27](https://github.com/dblock/ruby-link-checker/pull/27): Avoided duplicate test and lint runs by checking pull requests and pushes to `main` only - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/Copilot).
29
+ * [#26](https://github.com/dblock/ruby-link-checker/pull/26): Added ruby-gem-release automation with manual, dry-run-first releases and RubyGems Trusted Publishing - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/Copilot).
30
+ * [#23](https://github.com/dblock/ruby-link-checker/pull/23): Fixed chained relative redirects, bounded redirect following with `max_redirects`, enabled plain HTTP in the Net::HTTP checker, and corrected retry documentation - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/Copilot).
31
+ * [#24](https://github.com/dblock/ruby-link-checker/pull/24): Added transient-only retries, opt-in retry delays/backoff/jitter and `Retry-After` handling, transport error categories, and an Async concurrency limit - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/Copilot).
32
+ * [#25](https://github.com/dblock/ruby-link-checker/pull/25): Added opt-in URL deduplication and atomic TTL-based success caching, preserving per-submission metadata and query values - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/Copilot).
33
+ * [#8](https://github.com/dblock/ruby-link-checker/issues/8): Added `protocol` configuration option to `LinkChecker::Async::HTTP` to force HTTP/1 or HTTP/2 - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/Copilot).
34
+
1
35
  ### 0.4.0 (2026/8/17)
2
36
 
3
37
  * [#13](https://github.com/dblock/ruby-link-checker/pull/13): Fix `SystemStackError` caused by infinite recursion in `_handle_result` - [@dblock](https://github.com/dblock), [@Copilot](https://github.com/Copilot).
data/LICENSE.md CHANGED
File without changes
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
 
@@ -19,9 +24,16 @@ A fast Ruby link checker with support for multiple HTTP libraries. Does not pars
19
24
  - [LinkChecker::Net::HTTP](#linkcheckernethttp)
20
25
  - [Options](#options)
21
26
  - [Retries](#retries)
27
+ - [Redirects](#redirects)
22
28
  - [Retry After](#retry-after)
29
+ - [Retry Policy](#retry-policy)
30
+ - [Rate Limits](#rate-limits)
31
+ - [Blocked Results](#blocked-results)
32
+ - [Per-Host Limits](#per-host-limits)
23
33
  - [Results](#results)
24
34
  - [Methods](#methods)
35
+ - [Deduplication](#deduplication)
36
+ - [Caching](#caching)
25
37
  - [Logger](#logger)
26
38
  - [User-Agent](#user-agent)
27
39
  - [Headers](#headers)
@@ -172,9 +184,23 @@ LinkChecker::Async::HTTP.configure do |config|
172
184
  end
173
185
  ```
174
186
 
187
+ 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.
188
+
189
+ ```ruby
190
+ LinkChecker::Async::HTTP.configure do |config|
191
+ config.protocol = :http2
192
+ end
193
+ ```
194
+
195
+ Async::HTTP reuses connection-pooling clients per scheme/host/port. Its default concurrency is unlimited; set `max_concurrency` to bound active requests without counting delayed retries against that limit.
196
+
197
+ ```ruby
198
+ checker = LinkChecker::Async::HTTP::Checker.new(max_concurrency: 25)
199
+ ```
200
+
175
201
  #### [LinkChecker::Net::HTTP](lib/ruby-link-checker/net/http/checker.rb)
176
202
 
177
- 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).
178
204
 
179
205
  ```ruby
180
206
  require 'net/http'
@@ -210,30 +236,115 @@ end
210
236
 
211
237
  #### Retries
212
238
 
213
- By default link checkers do not retry. You can set a number of times to retry all errors and failures with `retries`.
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`.
214
240
 
215
241
  ```ruby
216
- checker = LinkChecker::Net::HTTP::Checker.new(retry: 1)
242
+ checker = LinkChecker::Net::HTTP::Checker.new(retries: 1)
243
+ ```
244
+
245
+ #### Redirects
246
+
247
+ 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.
248
+
249
+ ```ruby
250
+ checker = LinkChecker::Typhoeus::Hydra::Checker.new(max_redirects: 10)
251
+ ```
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
217
259
  ```
218
260
 
219
261
  #### Retry After
220
262
 
221
- If retry is enabled, link checkers can wait for a number of seconds before retrying.
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).
264
+
265
+ ```ruby
266
+ checker = LinkChecker::Typhoeus::Hydra::Checker.new(
267
+ retries: 2,
268
+ retry_after: 1,
269
+ retry_backoff: 2,
270
+ retry_jitter: 0.1,
271
+ respect_retry_after: true,
272
+ max_retry_delay: 60
273
+ )
274
+ ```
275
+
276
+ 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.
277
+
278
+ #### Retry Policy
279
+
280
+ 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.
281
+
282
+ ```ruby
283
+ checker = LinkChecker::Net::HTTP::Checker.new(retries: 1, retry_status_codes: [429, 503])
284
+ ```
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.
222
320
 
223
321
  ```ruby
224
- checker = LinkChecker::Net::HTTP::Checker.new(retry_after: 1) # wait for 1 second before retrying
322
+ checker = LinkChecker::Typhoeus::Hydra::Checker.new(detect_challenges: false)
225
323
  ```
226
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
+
227
338
  #### Results
228
339
 
229
340
  By default checkers collect results.
230
341
 
231
342
  ```ruby
232
- checker = LinkChecker::Net::HTTP::Checker.new(results: false)
343
+ checker = LinkChecker::Net::HTTP::Checker.new
233
344
  ...
234
345
  checker.run
235
346
 
236
- checker.results # => { error: [...], failure: [...], success: [...] }
347
+ checker.results # => { error: [...], failure: [...], success: [...], blocked: [...] }
237
348
  ```
238
349
 
239
350
  You can disable this with `results: false`.
@@ -256,6 +367,51 @@ The following examples disables `GET` and only makes `HEAD` requests.
256
367
  checker = LinkChecker::Net::HTTP::Checker.new(methods: %w[HEAD])
257
368
  ```
258
369
 
370
+ #### Deduplication
371
+
372
+ 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.
373
+
374
+ ```ruby
375
+ checker = LinkChecker::Typhoeus::Hydra::Checker.new(deduplicate: true)
376
+ checker.check('https://www.example.org', location: 'one.html')
377
+ checker.check('https://www.example.org', location: 'two.html')
378
+ checker.run # one check, two sets of result events
379
+ ```
380
+
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.
382
+
383
+ #### Caching
384
+
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.
395
+
396
+ ```ruby
397
+ checker = LinkChecker::Typhoeus::Hydra::Checker.new(
398
+ deduplicate: true,
399
+ cache: { file: 'tmp/link-checker/cache.json', ttl: 3600 }
400
+ )
401
+ ```
402
+
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
+ ```
412
+
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.
414
+
259
415
  #### Logger
260
416
 
261
417
  Pass your own logger.
@@ -286,6 +442,20 @@ checker = LinkChecker::Net::HTTP::Checker.new(
286
442
  )
287
443
  ```
288
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
+
289
459
  ### Global Configuration
290
460
 
291
461
  All options can also be configured globally.
@@ -304,8 +474,8 @@ end
304
474
  Events enable processing of results as they become available.
305
475
 
306
476
  ```ruby
307
- checker.on :result do |result|
308
- puts result # any result
477
+ checker.on :complete do |result|
478
+ puts result # the final result of each link
309
479
  end
310
480
 
311
481
  checker.on :error, :failure do |result|
@@ -313,15 +483,33 @@ checker.on :error, :failure do |result|
313
483
  end
314
484
  ```
315
485
 
316
- Checkers support the following events.
317
-
318
- | Event | Description |
319
- |----------|----------------------------------------------------------------|
320
- | :retry | A request is being retried on failure or error. |
321
- | :result | A new result, any of success, failure, or error. |
322
- | :success | A valid URL, usually a 2xx response from the server. |
323
- | :failure | A failed URL, usually a 4xx or a 5xx response from the server. |
324
- | :error | An error, such as an invalid URL or a network timeout. |
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.
325
513
 
326
514
  Events are called with results, which contain the following properties.
327
515
 
@@ -334,6 +522,26 @@ Events are called with results, which contain the following properties.
334
522
  | :request_headers | Request headers. |
335
523
  | :redirect_to | A redirect URL in case of redirects. |
336
524
  | :error | A raised error in case of errors. |
525
+ | :error_type | `:timeout`, `:connection`, or `:other` for errors; otherwise `nil`. |
526
+ | :response_headers | Response headers, accessible using `[]`. |
527
+ | :cached? | Whether this result was reused from a successful check. |
528
+ | :blocked? | Whether this final result was reported as blocked. |
529
+ | :redirects | The redirect hops that led to this result, see [Redirects](#redirects). |
530
+ | :permanent_redirect? | Whether any of those hops was a permanent redirect (301 or 308). |
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
+ ```
337
545
 
338
546
  See [result.rb](lib/ruby-link-checker/result.rb) for more details.
339
547
 
@@ -349,7 +557,7 @@ Anecdotal results show `LinkChecker::Async::HTTP` using significantly less CPU t
349
557
 
350
558
  ## Contributing
351
559
 
352
- 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.
353
561
 
354
562
  ## Copyright and License
355
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 { _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
- [['user-agent', checker.user_agent]] + checker.headers.map { |key, value| [key, value] }
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,25 +42,43 @@ module LinkChecker
42
42
  end
43
43
  @queue = []
44
44
  @clients = {}
45
+ @host_semaphores = {}
46
+ if max_concurrency && (!max_concurrency.is_a?(Integer) || max_concurrency < 1)
47
+ raise ArgumentError, "Invalid concurrency limit: #{max_concurrency}"
48
+ end
49
+
45
50
  super
46
51
  end
47
52
 
48
53
  # Runs all queued checks concurrently and blocks until they complete.
49
54
  def run
55
+ require 'async/semaphore'
56
+
50
57
  Barrier do |barrier|
51
58
  @barrier = barrier
59
+ @semaphore = ::Async::Semaphore.new(max_concurrency) if max_concurrency
52
60
  _flush!
53
61
  end
62
+ _finish_run
54
63
  ensure
55
64
  @barrier = nil
65
+ @semaphore = nil
66
+ @host_semaphores = {}
56
67
  _close_clients!
57
68
  end
58
69
 
59
- def _queue(&block)
70
+ def _queue(delay: 0, host: nil, &block)
60
71
  if @barrier
61
- @barrier.async(&block)
72
+ @barrier.async do
73
+ sleep(delay) if delay.positive?
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
79
+ end
62
80
  else
63
- @queue << block
81
+ @queue << [delay, host, block]
64
82
  end
65
83
  end
66
84
 
@@ -69,17 +87,38 @@ module LinkChecker
69
87
  def _client_for(uri)
70
88
  key = [uri.scheme, uri.host, uri.port]
71
89
  @clients[key] ||= begin
72
- endpoint = ::Async::HTTP::Endpoint.parse(uri.to_s, timeout: open_timeout)
90
+ endpoint = ::Async::HTTP::Endpoint.parse(uri.to_s, timeout: open_timeout, **_endpoint_options)
73
91
  ::Async::HTTP::Client.new(endpoint)
74
92
  end
75
93
  end
76
94
 
77
95
  private
78
96
 
97
+ # Maps the configured protocol (:http1 or :http2) to the
98
+ # corresponding async-http protocol implementation.
99
+ def _endpoint_options
100
+ case protocol
101
+ when :http1
102
+ { protocol: ::Async::HTTP::Protocol::HTTP1 }
103
+ when :http2
104
+ { protocol: ::Async::HTTP::Protocol::HTTP2 }
105
+ when nil
106
+ {}
107
+ else
108
+ raise ArgumentError, "Unsupported protocol: #{protocol.inspect}, expected :http1 or :http2"
109
+ end
110
+ end
111
+
79
112
  def _flush!
80
113
  queue = @queue
81
114
  @queue = []
82
- queue.each { |block| @barrier.async(&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(&)
83
122
  end
84
123
 
85
124
  def _close_clients!
@@ -9,6 +9,8 @@ module LinkChecker
9
9
  ATTRIBUTES = %i[
10
10
  read_timeout
11
11
  open_timeout
12
+ protocol
13
+ max_concurrency
12
14
  ].freeze
13
15
 
14
16
  attr_accessor(*Config::ATTRIBUTES)
@@ -16,6 +18,8 @@ module LinkChecker
16
18
  def reset
17
19
  self.read_timeout = nil
18
20
  self.open_timeout = nil
21
+ self.protocol = nil
22
+ self.max_concurrency = nil
19
23
  end
20
24
  end
21
25
 
@@ -43,7 +43,7 @@ module LinkChecker
43
43
  end
44
44
 
45
45
  def failure?
46
- !success? && !redirect?
46
+ !success? && !redirect? && !blocked?
47
47
  end
48
48
 
49
49
  def code
@@ -58,6 +58,10 @@ module LinkChecker
58
58
  Headers.new(request.headers)
59
59
  end
60
60
 
61
+ def response_headers
62
+ response&.headers || {}
63
+ end
64
+
61
65
  def redirect_to
62
66
  return nil unless response
63
67
 
File without changes