mirror of
https://github.com/Hopiu/lychee.git
synced 2026-04-24 06:54:47 +00:00
Skip unsupported schemes (#236)
This commit is contained in:
parent
2a80760f58
commit
f8426bafbf
5 changed files with 43 additions and 5 deletions
|
|
@ -1,2 +1,3 @@
|
|||
https://www.youtube.com/watch?v=NlKuICiT470&list=PLbWDhxwM_45mPVToqaIZNbZeIzFchsKKQ&index=7
|
||||
https://twitter.com/zarfeblong/status/1339742840142872577
|
||||
https://twitter.com/zarfeblong/status/1339742840142872577
|
||||
https://crates.io/crates/lychee
|
||||
3
fixtures/TEST_SCHEMES.txt
Normal file
3
fixtures/TEST_SCHEMES.txt
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
slack://channel?id=123
|
||||
file://foo/bar
|
||||
https://example.org
|
||||
|
|
@ -14,8 +14,8 @@ const MAX_PADDING: usize = 20;
|
|||
pub(crate) fn color_response(response: &ResponseBody) -> String {
|
||||
let out = match response.status {
|
||||
Status::Ok(_) => style(response).green().bright(),
|
||||
Status::Excluded | Status::Unsupported(_) => style(response).dim(),
|
||||
Status::Redirected(_) => style(response),
|
||||
Status::Excluded => style(response).dim(),
|
||||
Status::Timeout(_) => style(response).yellow().bright(),
|
||||
Status::Error(_) => style(response).red().bright(),
|
||||
};
|
||||
|
|
@ -42,6 +42,10 @@ impl ResponseStats {
|
|||
|
||||
pub(crate) fn add(&mut self, response: Response) {
|
||||
let Response(source, ResponseBody { ref status, .. }) = response;
|
||||
if status.is_unsupported() {
|
||||
// Silently skip unsupported URIs
|
||||
return;
|
||||
}
|
||||
|
||||
self.total += 1;
|
||||
|
||||
|
|
@ -51,6 +55,7 @@ impl ResponseStats {
|
|||
Status::Timeout(_) => self.timeouts += 1,
|
||||
Status::Redirected(_) => self.redirects += 1,
|
||||
Status::Excluded => self.excludes += 1,
|
||||
Status::Unsupported(_) => (), // Just skip unsupported URI
|
||||
}
|
||||
|
||||
if matches!(
|
||||
|
|
|
|||
|
|
@ -128,13 +128,26 @@ mod cli {
|
|||
)
|
||||
}
|
||||
|
||||
/// Test unsupported URI schemes
|
||||
#[test]
|
||||
fn test_unsupported_uri_schemes() -> Result<()> {
|
||||
test_json_output!(
|
||||
"TEST_SCHEMES.txt",
|
||||
MockResponseStats {
|
||||
total: 1,
|
||||
successful: 1,
|
||||
..MockResponseStats::default()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_quirks() -> Result<()> {
|
||||
test_json_output!(
|
||||
"TEST_QUIRKS.txt",
|
||||
MockResponseStats {
|
||||
total: 2,
|
||||
successful: 2,
|
||||
total: 3,
|
||||
successful: 3,
|
||||
..MockResponseStats::default()
|
||||
}
|
||||
)
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ use crate::ErrorKind;
|
|||
const ICON_OK: &str = "\u{2714}"; // ✔
|
||||
const ICON_REDIRECTED: &str = "\u{21c4}"; // ⇄
|
||||
const ICON_EXCLUDED: &str = "\u{003f}"; // ?
|
||||
const ICON_UNSUPPORTED: &str = "\u{003f}"; // ? (using same icon, but under different name for explicitness)
|
||||
const ICON_ERROR: &str = "\u{2717}"; // ✗
|
||||
const ICON_TIMEOUT: &str = "\u{29d6}"; // ⧖
|
||||
|
||||
|
|
@ -26,6 +27,10 @@ pub enum Status {
|
|||
Redirected(StatusCode),
|
||||
/// Resource was excluded from checking
|
||||
Excluded,
|
||||
/// The request type is currently not supported,
|
||||
/// for example when the URL scheme is `slack://` or `file://`
|
||||
/// See https://github.com/lycheeverse/lychee/issues/199
|
||||
Unsupported(Box<ErrorKind>),
|
||||
}
|
||||
|
||||
impl Display for Status {
|
||||
|
|
@ -34,9 +39,10 @@ impl Display for Status {
|
|||
Status::Ok(c) => write!(f, "OK ({})", c),
|
||||
Status::Redirected(c) => write!(f, "Redirect ({})", c),
|
||||
Status::Excluded => f.write_str("Excluded"),
|
||||
Status::Error(e) => write!(f, "Failed: {}", e),
|
||||
Status::Timeout(Some(c)) => write!(f, "Timeout ({})", c),
|
||||
Status::Timeout(None) => f.write_str("Timeout"),
|
||||
Status::Unsupported(e) => write!(f, "Unsupported: {}", e),
|
||||
Status::Error(e) => write!(f, "Failed: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -97,6 +103,13 @@ impl Status {
|
|||
matches!(self, Status::Timeout(_))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
/// Returns `true` if a URI is unsupported
|
||||
pub const fn is_unsupported(&self) -> bool {
|
||||
matches!(self, Status::Unsupported(_))
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
/// Return a unicode icon to visualize the status
|
||||
pub const fn icon(&self) -> &str {
|
||||
|
|
@ -106,6 +119,7 @@ impl Status {
|
|||
Status::Excluded => ICON_EXCLUDED,
|
||||
Status::Error(_) => ICON_ERROR,
|
||||
Status::Timeout(_) => ICON_TIMEOUT,
|
||||
Status::Unsupported(_) => ICON_UNSUPPORTED,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -120,6 +134,8 @@ impl From<reqwest::Error> for Status {
|
|||
fn from(e: reqwest::Error) -> Self {
|
||||
if e.is_timeout() {
|
||||
Self::Timeout(e.status())
|
||||
} else if e.is_builder() {
|
||||
Self::Unsupported(Box::new(ErrorKind::ReqwestError(e)))
|
||||
} else {
|
||||
Self::Error(Box::new(ErrorKind::ReqwestError(e)))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue