-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathcheck.rs
493 lines (442 loc) · 14.3 KB
/
check.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use std::collections::HashSet;
use std::io::{self, Write};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use futures::StreamExt;
use indicatif::ProgressBar;
use indicatif::ProgressStyle;
use reqwest::Url;
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;
use lychee_lib::{Client, ErrorKind, Request, Response, Uri};
use lychee_lib::{InputSource, Result};
use lychee_lib::{ResponseBody, Status};
use crate::archive::{Archive, Suggestion};
use crate::formatters::get_response_formatter;
use crate::formatters::response::ResponseFormatter;
use crate::parse::parse_duration_secs;
use crate::verbosity::Verbosity;
use crate::{cache::Cache, stats::ResponseStats, ExitCode};
use super::CommandParams;
pub(crate) async fn check<S>(
params: CommandParams<S>,
) -> Result<(ResponseStats, Arc<Cache>, ExitCode)>
where
S: futures::Stream<Item = Result<Request>>,
{
// Setup
let (send_req, recv_req) = mpsc::channel(params.cfg.max_concurrency);
let (send_resp, recv_resp) = mpsc::channel(params.cfg.max_concurrency);
let max_concurrency = params.cfg.max_concurrency;
// Measure check time
let start = std::time::Instant::now();
let stats = if params.cfg.verbose.log_level() >= log::Level::Info {
ResponseStats::extended()
} else {
ResponseStats::default()
};
let cache_ref = params.cache.clone();
let client = params.client;
let cache = params.cache;
let cache_exclude_status = params.cfg.cache_exclude_status.into_set();
let accept = params.cfg.accept.into_set();
let pb = if params.cfg.no_progress || params.cfg.verbose.log_level() >= log::Level::Info {
None
} else {
Some(init_progress_bar("Extracting links"))
};
// Start receiving requests
tokio::spawn(request_channel_task(
recv_req,
send_resp,
max_concurrency,
client,
cache,
cache_exclude_status,
accept,
));
let formatter = get_response_formatter(¶ms.cfg.mode);
let show_results_task = tokio::spawn(progress_bar_task(
recv_resp,
params.cfg.verbose,
pb.clone(),
formatter,
stats,
));
// Wait until all messages are sent
send_inputs_loop(params.requests, send_req, pb).await?;
// Wait until all responses are received
let result = show_results_task.await?;
let (pb, mut stats) = result?;
// Store elapsed time in stats
stats.duration_secs = start.elapsed().as_secs();
// Note that print statements may interfere with the progress bar, so this
// must go before printing the stats
if let Some(pb) = &pb {
pb.finish_with_message("Finished extracting links");
}
if params.cfg.suggest {
suggest_archived_links(
params.cfg.archive.unwrap_or_default(),
&mut stats,
!params.cfg.no_progress,
max_concurrency,
parse_duration_secs(params.cfg.timeout),
)
.await;
}
let code = if stats.is_success() {
ExitCode::Success
} else {
ExitCode::LinkCheckFailure
};
Ok((stats, cache_ref, code))
}
async fn suggest_archived_links(
archive: Archive,
stats: &mut ResponseStats,
show_progress: bool,
max_concurrency: usize,
timeout: Duration,
) {
let failed_urls = &get_failed_urls(stats);
let bar = if show_progress {
let bar = init_progress_bar("Searching for alternatives");
bar.set_length(failed_urls.len() as u64);
Some(bar)
} else {
None
};
let suggestions = Mutex::new(&mut stats.suggestion_map);
futures::stream::iter(failed_urls)
.map(|(input, url)| (input, url, archive.get_link(url, timeout)))
.for_each_concurrent(max_concurrency, |(input, url, future)| async {
if let Ok(Some(suggestion)) = future.await {
suggestions
.lock()
.unwrap()
.entry(input.clone())
.or_default()
.insert(Suggestion {
suggestion,
original: url.clone(),
});
}
if let Some(bar) = &bar {
bar.inc(1);
}
})
.await;
if let Some(bar) = &bar {
bar.finish_with_message("Finished searching for alternatives");
}
}
// drops the `send_req` channel on exit
// required for the receiver task to end, which closes send_resp, which allows
// the show_results_task to finish
async fn send_inputs_loop<S>(
requests: S,
send_req: mpsc::Sender<Result<Request>>,
bar: Option<ProgressBar>,
) -> Result<()>
where
S: futures::Stream<Item = Result<Request>>,
{
tokio::pin!(requests);
while let Some(request) = requests.next().await {
let request = request?;
if let Some(pb) = &bar {
pb.inc_length(1);
pb.set_message(request.to_string());
};
send_req
.send(Ok(request))
.await
.expect("Cannot send request");
}
Ok(())
}
/// Reads from the request channel and updates the progress bar status
async fn progress_bar_task(
mut recv_resp: mpsc::Receiver<Response>,
verbose: Verbosity,
pb: Option<ProgressBar>,
formatter: Box<dyn ResponseFormatter>,
mut stats: ResponseStats,
) -> Result<(Option<ProgressBar>, ResponseStats)> {
while let Some(response) = recv_resp.recv().await {
show_progress(
&mut io::stderr(),
&pb,
&response,
formatter.as_ref(),
&verbose,
)?;
stats.add(response);
}
Ok((pb, stats))
}
fn init_progress_bar(initial_message: &'static str) -> ProgressBar {
let bar = ProgressBar::new_spinner().with_style(
ProgressStyle::with_template("{spinner:.162} {pos}/{len:.238} {bar:.162/238} {wide_msg}")
.expect("Valid progress bar")
.progress_chars("━ ━"),
);
bar.set_length(0);
bar.set_message(initial_message);
// report status _at least_ every 500ms
bar.enable_steady_tick(Duration::from_millis(500));
bar
}
async fn request_channel_task(
recv_req: mpsc::Receiver<Result<Request>>,
send_resp: mpsc::Sender<Response>,
max_concurrency: usize,
client: Client,
cache: Arc<Cache>,
cache_exclude_status: HashSet<u16>,
accept: HashSet<u16>,
) {
StreamExt::for_each_concurrent(
ReceiverStream::new(recv_req),
max_concurrency,
|request: Result<Request>| async {
let request = request.expect("cannot read request");
let response = handle(
&client,
cache.clone(),
cache_exclude_status.clone(),
request,
accept.clone(),
)
.await;
send_resp
.send(response)
.await
.expect("cannot send response to queue");
},
)
.await;
}
/// Check a URL and return a response.
///
/// # Errors
///
/// This can fail when the URL could not be parsed to a URI.
async fn check_url(client: &Client, request: Request) -> Response {
// Request was not cached; run a normal check
let uri = request.uri.clone();
let source = request.source.clone();
client.check(request).await.unwrap_or_else(|e| {
log::error!("Error checking URL {}: Cannot parse URL to URI: {}", uri, e);
Response::new(
uri.clone(),
Status::Error(ErrorKind::InvalidURI(uri.clone())),
source,
)
})
}
/// Handle a single request
async fn handle(
client: &Client,
cache: Arc<Cache>,
cache_exclude_status: HashSet<u16>,
request: Request,
accept: HashSet<u16>,
) -> Response {
let uri = request.uri.clone();
if let Some(v) = cache.get(&uri) {
// Found a cached request
// Overwrite cache status in case the URI is excluded in the
// current run
let status = if client.is_excluded(&uri) {
Status::Excluded
} else {
// Can't impl `Status::from(v.value().status)` here because the
// `accepted` status codes might have changed from the previous run
// and they may have an impact on the interpretation of the status
// code.
Status::from_cache_status(v.value().status, &accept)
};
return Response::new(uri.clone(), status, request.source);
}
// Request was not cached; run a normal check
let response = check_url(client, request).await;
// - Never cache filesystem access as it is fast already so caching has no
// benefit.
// - Skip caching unsupported URLs as they might be supported in a
// future run.
// - Skip caching excluded links; they might not be excluded in the next run.
// - Skip caching links for which the status code has been explicitly excluded from the cache.
let status = response.status();
if ignore_cache(&uri, status, &cache_exclude_status) {
return response;
}
cache.insert(uri, status.into());
response
}
/// Returns `true` if the response should be ignored in the cache.
///
/// The response should be ignored if:
/// - The URI is a file URI.
/// - The status is excluded.
/// - The status is unsupported.
/// - The status is unknown.
/// - The status code is excluded from the cache.
fn ignore_cache(uri: &Uri, status: &Status, cache_exclude_status: &HashSet<u16>) -> bool {
let status_code_excluded = status
.code()
.map_or(false, |code| cache_exclude_status.contains(&code.as_u16()));
uri.is_file()
|| status.is_excluded()
|| status.is_unsupported()
|| status.is_unknown()
|| status_code_excluded
}
fn show_progress(
output: &mut dyn Write,
progress_bar: &Option<ProgressBar>,
response: &Response,
formatter: &dyn ResponseFormatter,
verbose: &Verbosity,
) -> Result<()> {
let out = formatter.format_response(response.body());
if let Some(pb) = progress_bar {
pb.inc(1);
pb.set_message(out.clone());
if verbose.log_level() >= log::Level::Info {
pb.println(out);
}
} else if verbose.log_level() >= log::Level::Info
|| (!response.status().is_success() && !response.status().is_excluded())
{
writeln!(output, "{out}")?;
}
Ok(())
}
fn get_failed_urls(stats: &mut ResponseStats) -> Vec<(InputSource, Url)> {
stats
.fail_map
.iter()
.flat_map(|(source, set)| {
set.iter()
.map(move |ResponseBody { uri, status: _ }| (source, uri))
})
.filter_map(|(source, uri)| {
if uri.is_data() || uri.is_mail() || uri.is_file() {
None
} else {
match Url::try_from(uri.as_str()) {
Ok(url) => Some((source.clone(), url)),
Err(_) => None,
}
}
})
.collect()
}
#[cfg(test)]
mod tests {
use crate::{formatters::get_response_formatter, options};
use http::StatusCode;
use log::info;
use lychee_lib::{CacheStatus, ClientBuilder, ErrorKind, InputSource, Uri};
use super::*;
#[test]
fn test_skip_cached_responses_in_progress_output() {
let mut buf = Vec::new();
let response = Response::new(
Uri::try_from("http://127.0.0.1").unwrap(),
Status::Cached(CacheStatus::Ok(200)),
InputSource::Stdin,
);
let formatter = get_response_formatter(&options::OutputMode::Plain);
show_progress(
&mut buf,
&None,
&response,
formatter.as_ref(),
&Verbosity::default(),
)
.unwrap();
info!("{:?}", String::from_utf8_lossy(&buf));
assert!(buf.is_empty());
}
#[test]
fn test_show_cached_responses_in_progress_debug_output() {
let mut buf = Vec::new();
let response = Response::new(
Uri::try_from("http://127.0.0.1").unwrap(),
Status::Cached(CacheStatus::Ok(200)),
InputSource::Stdin,
);
let formatter = get_response_formatter(&options::OutputMode::Plain);
show_progress(
&mut buf,
&None,
&response,
formatter.as_ref(),
&Verbosity::debug(),
)
.unwrap();
assert!(!buf.is_empty());
let buf = String::from_utf8_lossy(&buf);
assert_eq!(buf, "[200] http://127.0.0.1/ | Cached: OK (cached)\n");
}
#[tokio::test]
async fn test_invalid_url() {
// Run a normal request with an invalid Url
let client = ClientBuilder::builder().build().client().unwrap();
let request = Request::try_from("http://\"").unwrap();
let response = check_url(&client, request).await;
assert!(response.status().is_error());
assert!(matches!(
response.status(),
Status::Error(ErrorKind::InvalidURI(_))
));
}
#[test]
fn test_cache_by_default() {
assert!(!ignore_cache(
&Uri::try_from("https://[::1]").unwrap(),
&Status::Ok(StatusCode::OK),
&HashSet::default()
));
}
#[test]
// Cache is ignored for file URLs
fn test_cache_ignore_file_urls() {
assert!(ignore_cache(
&Uri::try_from("file:///home").unwrap(),
&Status::Ok(StatusCode::OK),
&HashSet::default()
));
}
#[test]
// Cache is ignored for unsupported status
fn test_cache_ignore_unsupported_status() {
assert!(ignore_cache(
&Uri::try_from("https://[::1]").unwrap(),
&Status::Unsupported(ErrorKind::EmptyUrl),
&HashSet::default()
));
}
#[test]
// Cache is ignored for unknown status
fn test_cache_ignore_unknown_status() {
assert!(ignore_cache(
&Uri::try_from("https://[::1]").unwrap(),
&Status::UnknownStatusCode(StatusCode::IM_A_TEAPOT),
&HashSet::default()
));
}
#[test]
fn test_cache_ignore_excluded_status() {
// Cache is ignored for excluded status codes
let exclude = [StatusCode::OK.as_u16()].iter().copied().collect();
assert!(ignore_cache(
&Uri::try_from("https://[::1]").unwrap(),
&Status::Ok(StatusCode::OK),
&exclude
));
}
}