@@ -10,7 +10,7 @@ use reqwest::Url;
10
10
use tokio:: sync:: mpsc;
11
11
use tokio_stream:: wrappers:: ReceiverStream ;
12
12
13
- use lychee_lib:: { Client , Request , Response } ;
13
+ use lychee_lib:: { Client , Request , Response , Uri } ;
14
14
use lychee_lib:: { InputSource , Result } ;
15
15
use lychee_lib:: { ResponseBody , Status } ;
16
16
@@ -275,22 +275,25 @@ async fn handle(
275
275
// - Skip caching excluded links; they might not be excluded in the next run.
276
276
// - Skip caching links for which the status code has been explicitly excluded from the cache.
277
277
let status = response. status ( ) ;
278
+ if ignore_cache ( & uri, status, cache_exclude_status) {
279
+ return response;
280
+ }
281
+
282
+ cache. insert ( uri, status. into ( ) ) ;
283
+ response
284
+ }
285
+
286
+ fn ignore_cache ( uri : & Uri , status : & Status , cache_exclude_status : HashSet < u16 > ) -> bool {
278
287
let status_code = match status. code ( ) {
279
288
None => 0 ,
280
289
Some ( code) => code. as_u16 ( ) ,
281
290
} ;
282
291
283
- if uri. is_file ( )
292
+ return uri. is_file ( )
284
293
|| status. is_excluded ( )
285
294
|| status. is_unsupported ( )
286
295
|| status. is_unknown ( )
287
- || cache_exclude_status. contains ( & status_code)
288
- {
289
- return response;
290
- }
291
-
292
- cache. insert ( uri, status. into ( ) ) ;
293
- response
296
+ || cache_exclude_status. contains ( & status_code) ;
294
297
}
295
298
296
299
fn show_progress (
@@ -338,9 +341,10 @@ fn get_failed_urls(stats: &mut ResponseStats) -> Vec<(InputSource, Url)> {
338
341
339
342
#[ cfg( test) ]
340
343
mod tests {
344
+ use http:: StatusCode ;
341
345
use log:: info;
342
346
343
- use lychee_lib:: { CacheStatus , InputSource , ResponseBody , Uri } ;
347
+ use lychee_lib:: { CacheStatus , ErrorKind , InputSource , ResponseBody , Uri } ;
344
348
345
349
use crate :: formatters;
346
350
@@ -389,4 +393,60 @@ mod tests {
389
393
let buf = String :: from_utf8_lossy ( & buf) ;
390
394
assert_eq ! ( buf, "↻ [200] http://127.0.0.1/ | Cached: OK (cached)\n " ) ;
391
395
}
396
+
397
+ #[ test]
398
+ fn test_ignore_cache ( ) {
399
+ let mut exclude = HashSet :: new ( ) ;
400
+
401
+ // Cache is not ignored
402
+ assert_eq ! (
403
+ false ,
404
+ ignore_cache(
405
+ & Uri :: try_from( "https://[::1]" ) . unwrap( ) ,
406
+ & Status :: Ok ( StatusCode :: OK ) ,
407
+ exclude. clone( )
408
+ )
409
+ ) ;
410
+
411
+ // Cache is ignored for file URLs
412
+ assert_eq ! (
413
+ true ,
414
+ ignore_cache(
415
+ & Uri :: try_from( "file:///home" ) . unwrap( ) ,
416
+ & Status :: Ok ( StatusCode :: OK ) ,
417
+ exclude. clone( )
418
+ )
419
+ ) ;
420
+
421
+ // Cache is ignored for unsupported status
422
+ assert_eq ! (
423
+ true ,
424
+ ignore_cache(
425
+ & Uri :: try_from( "https://[::1]" ) . unwrap( ) ,
426
+ & Status :: Unsupported ( ErrorKind :: EmptyUrl ) ,
427
+ exclude. clone( )
428
+ )
429
+ ) ;
430
+
431
+ // Cache is ignored for unknown status
432
+ assert_eq ! (
433
+ true ,
434
+ ignore_cache(
435
+ & Uri :: try_from( "https://[::1]" ) . unwrap( ) ,
436
+ & Status :: UnknownStatusCode ( StatusCode :: IM_A_TEAPOT ) ,
437
+ exclude. clone( )
438
+ )
439
+ ) ;
440
+
441
+ // Cache is ignored for excluded status codes
442
+ exclude. insert ( 200 ) ;
443
+ assert_eq ! (
444
+ true ,
445
+ ignore_cache(
446
+ & Uri :: try_from( "https://[::1]" ) . unwrap( ) ,
447
+ & Status :: Ok ( StatusCode :: OK ) ,
448
+ exclude. clone( )
449
+ )
450
+ ) ;
451
+ }
392
452
}
0 commit comments