2022-02-09 23:04:48 +00:00
|
|
|
|
//! Handler of link checking operations.
|
|
|
|
|
|
//!
|
|
|
|
|
|
//! This module defines two structs, [`Client`] and [`ClientBuilder`].
|
|
|
|
|
|
//! `Client` handles incoming requests and returns responses.
|
|
|
|
|
|
//! `ClientBuilder` exposes a finer level of granularity for building
|
|
|
|
|
|
//! a `Client`.
|
|
|
|
|
|
//!
|
|
|
|
|
|
//! For convenience, a free function [`check`] is provided for ad-hoc
|
|
|
|
|
|
//! link checks.
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
#![allow(
|
|
|
|
|
|
clippy::module_name_repetitions,
|
|
|
|
|
|
clippy::struct_excessive_bools,
|
2021-04-16 18:25:22 +00:00
|
|
|
|
clippy::default_trait_access,
|
|
|
|
|
|
clippy::used_underscore_binding
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
)]
|
2023-07-31 14:04:00 +00:00
|
|
|
|
use std::{collections::HashSet, path::Path, sync::Arc, time::Duration};
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
|
Add optional Rustls support (#1099)
* Add optional Rustls support
This commit adds a non-default feature flag to use Rustls instead of OpenSSL.
My personal motivation is to use Lychee on OpenBSD -current, where the
`openssl` crate frequently fails to link against the unreleased system
LibreSSL. Using the `vendored-openssl` feature helps with compilation, but
segfaults at runtime.
The commit adds three feature flags to the library, binary, benchmark, and all
examples:
- The `native-tls` feature flag toggles the `openssl` crate.
- The `rustls-tls` feature flag toggles the `rustls` crate.
- The `email-check` feature flag toggles the `check-if-email-exists` crate,
which is the only existing functionality currently incompatible with Rustls.
By default, `native-tls` and `email-check` are enabled. Thus, Lychee (bin and
lib) can be used as before unless default features are disabled.
To use the Rustls feature, pass `--no-default-features --features rustls` to
cargo check/build/test/..., e.g.,
$ cargo clippy --workspace --all-targets --no-default-features \ --features
rustls-tls -- --deny warnings
Checking email addresses requires both, `native-tls` and `email-check`, to be
enabled. Otherwise, email addresses are excluded.
The `email-check` feature flag is technically not necessary. I preferred it
over `not(rustls-tls)` because it's clearer and it addresses the AGPL license
issue #594. As far as I understand, a Lychee binary compiled without the
`email-check` feature could be distributed with file-based copyleft for the
MPL-licensed dependencies only. But that's out of scope here.
The benchmark shows a performance regression varying between 2% and 4.4% when
using Rustls instead of OpenSSL on my machine.
PS: The `ring` crate needs to be patched on OpenBSD 7.3 and later until the new
xonly patches have been upstreamed, see the `rust-ring` port.
* Use platform native certificates with Rustls
By default, reqwest uses the webpki-roots crate with Rustls, effectively
bundling Mozilla's root certificates.
This commit uses the rustls-native-certs crate instead to use locally
installed root certificates, to minimize the difference between the
native-tls and rustls-tls features.
* Document feature flags
2023-06-16 00:21:57 +00:00
|
|
|
|
#[cfg(all(feature = "email-check", feature = "native-tls"))]
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
use check_if_email_exists::{check_email, CheckEmailInput, Reachable};
|
2023-06-26 10:06:24 +00:00
|
|
|
|
use headers::authorization::Credentials;
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
use http::{
|
2023-06-26 10:06:24 +00:00
|
|
|
|
header::{HeaderMap, HeaderValue, AUTHORIZATION},
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
StatusCode,
|
|
|
|
|
|
};
|
2023-07-31 14:04:00 +00:00
|
|
|
|
use log::{debug, warn};
|
2022-02-11 22:43:47 +00:00
|
|
|
|
use octocrab::Octocrab;
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
use regex::RegexSet;
|
2023-06-23 13:49:05 +00:00
|
|
|
|
use reqwest::{header, redirect, Url};
|
2023-07-13 15:32:41 +00:00
|
|
|
|
use reqwest_cookie_store::CookieStoreMutex;
|
2022-02-13 12:53:46 +00:00
|
|
|
|
use secrecy::{ExposeSecret, SecretString};
|
2021-04-16 18:25:22 +00:00
|
|
|
|
use typed_builder::TypedBuilder;
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
|
filter::{Excludes, Filter, Includes},
|
|
|
|
|
|
quirks::Quirks,
|
2022-05-29 19:41:22 +00:00
|
|
|
|
remap::Remaps,
|
2023-03-10 21:36:45 +00:00
|
|
|
|
retry::RetryExt,
|
Add optional Rustls support (#1099)
* Add optional Rustls support
This commit adds a non-default feature flag to use Rustls instead of OpenSSL.
My personal motivation is to use Lychee on OpenBSD -current, where the
`openssl` crate frequently fails to link against the unreleased system
LibreSSL. Using the `vendored-openssl` feature helps with compilation, but
segfaults at runtime.
The commit adds three feature flags to the library, binary, benchmark, and all
examples:
- The `native-tls` feature flag toggles the `openssl` crate.
- The `rustls-tls` feature flag toggles the `rustls` crate.
- The `email-check` feature flag toggles the `check-if-email-exists` crate,
which is the only existing functionality currently incompatible with Rustls.
By default, `native-tls` and `email-check` are enabled. Thus, Lychee (bin and
lib) can be used as before unless default features are disabled.
To use the Rustls feature, pass `--no-default-features --features rustls` to
cargo check/build/test/..., e.g.,
$ cargo clippy --workspace --all-targets --no-default-features \ --features
rustls-tls -- --deny warnings
Checking email addresses requires both, `native-tls` and `email-check`, to be
enabled. Otherwise, email addresses are excluded.
The `email-check` feature flag is technically not necessary. I preferred it
over `not(rustls-tls)` because it's clearer and it addresses the AGPL license
issue #594. As far as I understand, a Lychee binary compiled without the
`email-check` feature could be distributed with file-based copyleft for the
MPL-licensed dependencies only. But that's out of scope here.
The benchmark shows a performance regression varying between 2% and 4.4% when
using Rustls instead of OpenSSL on my machine.
PS: The `ring` crate needs to be patched on OpenBSD 7.3 and later until the new
xonly patches have been upstreamed, see the `rust-ring` port.
* Use platform native certificates with Rustls
By default, reqwest uses the webpki-roots crate with Rustls, effectively
bundling Mozilla's root certificates.
This commit uses the rustls-native-certs crate instead to use locally
installed root certificates, to minimize the difference between the
native-tls and rustls-tls features.
* Document feature flags
2023-06-16 00:21:57 +00:00
|
|
|
|
types::uri::github::GithubUri,
|
2023-07-31 14:04:00 +00:00
|
|
|
|
utils::fragment_checker::FragmentChecker,
|
2023-06-26 10:06:24 +00:00
|
|
|
|
BasicAuthCredentials, ErrorKind, Request, Response, Result, Status, Uri,
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
Add optional Rustls support (#1099)
* Add optional Rustls support
This commit adds a non-default feature flag to use Rustls instead of OpenSSL.
My personal motivation is to use Lychee on OpenBSD -current, where the
`openssl` crate frequently fails to link against the unreleased system
LibreSSL. Using the `vendored-openssl` feature helps with compilation, but
segfaults at runtime.
The commit adds three feature flags to the library, binary, benchmark, and all
examples:
- The `native-tls` feature flag toggles the `openssl` crate.
- The `rustls-tls` feature flag toggles the `rustls` crate.
- The `email-check` feature flag toggles the `check-if-email-exists` crate,
which is the only existing functionality currently incompatible with Rustls.
By default, `native-tls` and `email-check` are enabled. Thus, Lychee (bin and
lib) can be used as before unless default features are disabled.
To use the Rustls feature, pass `--no-default-features --features rustls` to
cargo check/build/test/..., e.g.,
$ cargo clippy --workspace --all-targets --no-default-features \ --features
rustls-tls -- --deny warnings
Checking email addresses requires both, `native-tls` and `email-check`, to be
enabled. Otherwise, email addresses are excluded.
The `email-check` feature flag is technically not necessary. I preferred it
over `not(rustls-tls)` because it's clearer and it addresses the AGPL license
issue #594. As far as I understand, a Lychee binary compiled without the
`email-check` feature could be distributed with file-based copyleft for the
MPL-licensed dependencies only. But that's out of scope here.
The benchmark shows a performance regression varying between 2% and 4.4% when
using Rustls instead of OpenSSL on my machine.
PS: The `ring` crate needs to be patched on OpenBSD 7.3 and later until the new
xonly patches have been upstreamed, see the `rust-ring` port.
* Use platform native certificates with Rustls
By default, reqwest uses the webpki-roots crate with Rustls, effectively
bundling Mozilla's root certificates.
This commit uses the rustls-native-certs crate instead to use locally
installed root certificates, to minimize the difference between the
native-tls and rustls-tls features.
* Document feature flags
2023-06-16 00:21:57 +00:00
|
|
|
|
#[cfg(all(feature = "email-check", feature = "native-tls"))]
|
|
|
|
|
|
use crate::types::mail;
|
|
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Default number of redirects before a request is deemed as failed, 5.
|
2022-01-07 00:03:10 +00:00
|
|
|
|
pub const DEFAULT_MAX_REDIRECTS: usize = 5;
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Default number of retries before a request is deemed as failed, 3.
|
2022-01-07 00:03:10 +00:00
|
|
|
|
pub const DEFAULT_MAX_RETRIES: u64 = 3;
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// Default wait time in seconds between retries, 1.
|
2022-02-24 11:24:57 +00:00
|
|
|
|
pub const DEFAULT_RETRY_WAIT_TIME_SECS: usize = 1;
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Default timeout in seconds before a request is deemed as failed, 20.
|
2022-02-24 11:24:57 +00:00
|
|
|
|
pub const DEFAULT_TIMEOUT_SECS: usize = 20;
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Default user agent, `lychee-<PKG_VERSION>`.
|
|
|
|
|
|
pub const DEFAULT_USER_AGENT: &str = concat!("lychee/", env!("CARGO_PKG_VERSION"));
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
|
2022-05-13 16:51:11 +00:00
|
|
|
|
// Constants currently not configurable by the user.
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// A timeout for only the connect phase of a [`Client`].
|
2022-05-13 16:51:11 +00:00
|
|
|
|
const CONNECT_TIMEOUT: u64 = 10;
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// TCP keepalive.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// See <https://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html> for more
|
2023-02-09 14:32:16 +00:00
|
|
|
|
/// information.
|
2022-05-13 16:51:11 +00:00
|
|
|
|
const TCP_KEEPALIVE: u64 = 60;
|
|
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Builder for [`Client`].
|
|
|
|
|
|
///
|
|
|
|
|
|
/// See crate-level documentation for usage example.
|
|
|
|
|
|
#[derive(TypedBuilder, Debug, Clone)]
|
2021-04-16 18:25:22 +00:00
|
|
|
|
#[builder(field_defaults(default, setter(into)))]
|
|
|
|
|
|
pub struct ClientBuilder {
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Optional GitHub token used for GitHub links.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This allows much more request before getting rate-limited.
|
|
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// # Rate-limiting Defaults
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// As of Feb 2022, it's 60 per hour without GitHub token v.s.
|
|
|
|
|
|
/// 5000 per hour with token.
|
2022-02-13 12:53:46 +00:00
|
|
|
|
github_token: Option<SecretString>,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// Remap URIs matching a pattern to a different URI.
|
2022-05-29 19:41:22 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// This makes it possible to remap any HTTP/HTTPS endpoint to a different
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// HTTP/HTTPS one. This feature could also be used to proxy
|
2022-05-29 19:41:22 +00:00
|
|
|
|
/// certain requests.
|
|
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// # Usage Notes
|
|
|
|
|
|
///
|
2022-05-29 19:41:22 +00:00
|
|
|
|
/// Use with caution because a large set of remapping rules may cause
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// performance issues.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Furthermore rules are executed sequentially and multiple mappings for
|
|
|
|
|
|
/// the same URI are allowed, so it is up to the library user's discretion to
|
|
|
|
|
|
/// make sure rules don't conflict with each other.
|
2022-05-29 19:41:22 +00:00
|
|
|
|
remaps: Option<Remaps>,
|
|
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Links matching this set of regular expressions are **always** checked.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This has higher precedence over [`ClientBuilder::excludes`], **but**
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// has lower precedence compared to any other `exclude_` fields or
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// [`ClientBuilder::schemes`] below.
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
includes: Option<RegexSet>,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Links matching this set of regular expressions are ignored, **except**
|
|
|
|
|
|
/// when a link also matches against [`ClientBuilder::includes`].
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
excludes: Option<RegexSet>,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// When `true`, exclude all private network addresses.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This effectively turns on the following fields:
|
|
|
|
|
|
/// - [`ClientBuilder::exclude_private_ips`]
|
|
|
|
|
|
/// - [`ClientBuilder::exclude_link_local_ips`]
|
|
|
|
|
|
/// - [`ClientBuilder::exclude_loopback_ips`]
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
exclude_all_private: bool,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// When `true`, exclude private IP addresses.
|
|
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// # IPv4
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// The private address ranges are defined in [IETF RFC 1918] and include:
|
|
|
|
|
|
///
|
|
|
|
|
|
/// - `10.0.0.0/8`
|
|
|
|
|
|
/// - `172.16.0.0/12`
|
|
|
|
|
|
/// - `192.168.0.0/16`
|
|
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// # IPv6
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// The address is a unique local address (`fc00::/7`).
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This property is defined in [IETF RFC 4193].
|
|
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// # Note
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// Unicast site-local network was defined in [IETF RFC 4291], but was fully
|
|
|
|
|
|
/// deprecated in [IETF RFC 3879]. So it is **NOT** considered as private on
|
|
|
|
|
|
/// this purpose.
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// [IETF RFC 1918]: https://tools.ietf.org/html/rfc1918
|
|
|
|
|
|
/// [IETF RFC 4193]: https://tools.ietf.org/html/rfc4193
|
|
|
|
|
|
/// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
|
|
|
|
|
|
/// [IETF RFC 3879]: https://tools.ietf.org/html/rfc3879
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
exclude_private_ips: bool,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// When `true`, exclude link-local IPs.
|
|
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// # IPv4
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// The address is `169.254.0.0/16`.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This property is defined by [IETF RFC 3927].
|
|
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// # IPv6
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// The address is a unicast address with link-local scope, as defined in
|
|
|
|
|
|
/// [RFC 4291].
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// A unicast address has link-local scope if it has the prefix `fe80::/10`,
|
|
|
|
|
|
/// as per [RFC 4291 section 2.4].
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// [IETF RFC 3927]: https://tools.ietf.org/html/rfc3927
|
|
|
|
|
|
/// [RFC 4291]: https://tools.ietf.org/html/rfc4291
|
|
|
|
|
|
/// [RFC 4291 section 2.4]: https://tools.ietf.org/html/rfc4291#section-2.4
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
exclude_link_local_ips: bool,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// When `true`, exclude loopback IP addresses.
|
|
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// # IPv4
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// This is a loopback address (`127.0.0.0/8`).
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This property is defined by [IETF RFC 1122].
|
|
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// # IPv6
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// This is the loopback address (`::1`), as defined in
|
|
|
|
|
|
/// [IETF RFC 4291 section 2.5.3].
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// [IETF RFC 1122]: https://tools.ietf.org/html/rfc1122
|
|
|
|
|
|
/// [IETF RFC 4291 section 2.5.3]: https://tools.ietf.org/html/rfc4291#section-2.5.3
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
exclude_loopback_ips: bool,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2023-07-19 17:58:38 +00:00
|
|
|
|
/// When `true`, check mail addresses.
|
|
|
|
|
|
include_mail: bool,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Maximum number of redirects per request before returning an error.
|
2023-01-16 18:14:09 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// Defaults to [`DEFAULT_MAX_REDIRECTS`].
|
2021-04-16 18:25:22 +00:00
|
|
|
|
#[builder(default = DEFAULT_MAX_REDIRECTS)]
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
max_redirects: usize,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Maximum number of retries per request before returning an error.
|
2023-01-16 18:14:09 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// Defaults to [`DEFAULT_MAX_RETRIES`].
|
2022-01-07 00:03:10 +00:00
|
|
|
|
#[builder(default = DEFAULT_MAX_RETRIES)]
|
|
|
|
|
|
max_retries: u64,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// User-agent used for checking links.
|
|
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// Defaults to [`DEFAULT_USER_AGENT`].
|
|
|
|
|
|
///
|
|
|
|
|
|
/// # Notes
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This may be helpful for bypassing certain firewalls.
|
2021-04-16 18:25:22 +00:00
|
|
|
|
// Faking the user agent is necessary for some websites, unfortunately.
|
|
|
|
|
|
// Otherwise we get a 403 from the firewall (e.g. Sucuri/Cloudproxy on ldra.com).
|
|
|
|
|
|
#[builder(default_code = "String::from(DEFAULT_USER_AGENT)")]
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
user_agent: String,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// When `true`, accept invalid SSL certificates.
|
|
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// # Warning
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// You should think very carefully before allowing invalid SSL
|
|
|
|
|
|
/// certificates. It will accept any certificate for any site to be trusted
|
|
|
|
|
|
/// including expired certificates. This introduces significant
|
|
|
|
|
|
/// vulnerabilities, and should only be used as a last resort.
|
|
|
|
|
|
// TODO: We should add a warning message in CLI. (Lucius, Jan 2023)
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
allow_insecure: bool,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// Set of accepted URL schemes.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Only links with matched URI schemes are checked. This has no effect when
|
|
|
|
|
|
/// it's empty.
|
2021-04-26 16:24:54 +00:00
|
|
|
|
schemes: HashSet<String>,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// Default [headers] for every request.
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// This allows working around validation issues on some websites. See also
|
|
|
|
|
|
/// [here] for usage examples.
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// [headers]: https://docs.rs/http/latest/http/header/struct.HeaderName.html
|
|
|
|
|
|
/// [here]: https://docs.rs/reqwest/latest/reqwest/struct.ClientBuilder.html#method.default_headers
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
custom_headers: HeaderMap,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// HTTP method used for requests, e.g. `GET` or `HEAD`.
|
2021-04-16 18:25:22 +00:00
|
|
|
|
#[builder(default = reqwest::Method::GET)]
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
method: reqwest::Method,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Set of accepted return codes / status codes.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Unmatched return codes/ status codes are deemed as errors.
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
accepted: Option<HashSet<StatusCode>>,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// Response timeout per request in seconds.
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
timeout: Option<Duration>,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// Initial time between retries of failed requests.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Defaults to [`DEFAULT_RETRY_WAIT_TIME_SECS`].
|
2022-02-24 11:24:57 +00:00
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// # Notes
|
|
|
|
|
|
///
|
|
|
|
|
|
/// For each request, the wait time increases using an exponential backoff
|
|
|
|
|
|
/// mechanism. For example, if the value is 1 second, then it waits for
|
|
|
|
|
|
/// 2 ^ (N-1) seconds before the N-th retry.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This prevents spending too much system resources on slow responders and
|
|
|
|
|
|
/// prioritizes other requests.
|
|
|
|
|
|
#[builder(default_code = "Duration::from_secs(DEFAULT_RETRY_WAIT_TIME_SECS as u64)")]
|
|
|
|
|
|
retry_wait_time: Duration,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// When `true`, requires using HTTPS when it's available.
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
2023-02-09 14:32:16 +00:00
|
|
|
|
/// This would treat unencrypted links as errors when HTTPS is available.
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// It has no effect on non-HTTP schemes or if the URL doesn't support
|
|
|
|
|
|
/// HTTPS.
|
2021-09-04 01:21:54 +00:00
|
|
|
|
require_https: bool,
|
2023-07-13 15:32:41 +00:00
|
|
|
|
|
|
|
|
|
|
/// Cookie store used for requests.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// See https://docs.rs/reqwest/latest/reqwest/struct.ClientBuilder.html#method.cookie_store
|
|
|
|
|
|
cookie_jar: Option<Arc<CookieStoreMutex>>,
|
2023-07-31 14:04:00 +00:00
|
|
|
|
|
|
|
|
|
|
/// Enable the checking of fragments in links.
|
|
|
|
|
|
include_fragments: bool,
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-16 18:25:22 +00:00
|
|
|
|
impl Default for ClientBuilder {
|
2022-02-09 23:04:48 +00:00
|
|
|
|
#[must_use]
|
|
|
|
|
|
#[inline]
|
2021-04-16 18:25:22 +00:00
|
|
|
|
fn default() -> Self {
|
|
|
|
|
|
Self::builder().build()
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
}
|
2021-04-16 18:25:22 +00:00
|
|
|
|
}
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
|
2021-04-16 18:25:22 +00:00
|
|
|
|
impl ClientBuilder {
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Instantiates a [`Client`].
|
2021-10-07 16:07:18 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// # Errors
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Returns an `Err` if:
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// - The user-agent contains characters other than ASCII 32-127.
|
|
|
|
|
|
/// - The reqwest client cannot be instantiated. This occurs if a TLS
|
|
|
|
|
|
/// backend cannot be initialized or the resolver fails to load the system
|
|
|
|
|
|
/// configuration. See [here].
|
|
|
|
|
|
/// - The Github client cannot be created. Since the implementation also
|
|
|
|
|
|
/// uses reqwest under the hood, this errors in the same circumstances as
|
|
|
|
|
|
/// the last one.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// [here]: https://docs.rs/reqwest/latest/reqwest/struct.ClientBuilder.html#errors
|
2022-02-09 23:04:48 +00:00
|
|
|
|
pub fn client(self) -> Result<Client> {
|
|
|
|
|
|
let Self {
|
|
|
|
|
|
user_agent,
|
|
|
|
|
|
custom_headers: mut headers,
|
|
|
|
|
|
..
|
|
|
|
|
|
} = self;
|
|
|
|
|
|
|
2023-01-16 18:14:09 +00:00
|
|
|
|
if let Some(prev_user_agent) =
|
|
|
|
|
|
headers.insert(header::USER_AGENT, HeaderValue::try_from(&user_agent)?)
|
|
|
|
|
|
{
|
2023-03-10 21:36:45 +00:00
|
|
|
|
debug!(
|
2023-01-16 18:14:09 +00:00
|
|
|
|
"Found user-agent in headers: {}. Overriding it with {user_agent}.",
|
|
|
|
|
|
prev_user_agent.to_str().unwrap_or("<EFBFBD>"),
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
2022-02-19 00:44:00 +00:00
|
|
|
|
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
headers.insert(
|
|
|
|
|
|
header::TRANSFER_ENCODING,
|
|
|
|
|
|
HeaderValue::from_static("chunked"),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2023-06-23 13:49:05 +00:00
|
|
|
|
// Custom redirect policy to enable logging of redirects.
|
|
|
|
|
|
let max_redirects = self.max_redirects;
|
|
|
|
|
|
let redirect_policy = redirect::Policy::custom(move |attempt| {
|
|
|
|
|
|
if attempt.previous().len() > max_redirects {
|
|
|
|
|
|
attempt.error("too many redirects")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
debug!("Redirecting to {}", attempt.url());
|
|
|
|
|
|
attempt.follow()
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2023-07-13 15:32:41 +00:00
|
|
|
|
let mut builder = reqwest::ClientBuilder::new()
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
.gzip(true)
|
|
|
|
|
|
.default_headers(headers)
|
2021-04-16 18:25:22 +00:00
|
|
|
|
.danger_accept_invalid_certs(self.allow_insecure)
|
2022-05-13 16:51:11 +00:00
|
|
|
|
.connect_timeout(Duration::from_secs(CONNECT_TIMEOUT))
|
|
|
|
|
|
.tcp_keepalive(Duration::from_secs(TCP_KEEPALIVE))
|
2023-06-23 13:49:05 +00:00
|
|
|
|
.redirect(redirect_policy);
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
|
2023-07-13 15:32:41 +00:00
|
|
|
|
if let Some(cookie_jar) = self.cookie_jar {
|
|
|
|
|
|
builder = builder.cookie_provider(cookie_jar);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let reqwest_client = match self.timeout {
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
Some(t) => builder.timeout(t),
|
|
|
|
|
|
None => builder,
|
2023-07-13 15:32:41 +00:00
|
|
|
|
}
|
2022-02-19 00:44:00 +00:00
|
|
|
|
.build()
|
|
|
|
|
|
.map_err(ErrorKind::NetworkRequest)?;
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
|
2023-01-16 18:14:09 +00:00
|
|
|
|
let github_client = match self.github_token.as_ref().map(ExposeSecret::expose_secret) {
|
2022-02-19 00:44:00 +00:00
|
|
|
|
Some(token) if !token.is_empty() => Some(
|
|
|
|
|
|
Octocrab::builder()
|
|
|
|
|
|
.personal_token(token.clone())
|
|
|
|
|
|
.build()
|
2023-01-16 18:14:09 +00:00
|
|
|
|
// this is essentially the same reqwest::ClientBuilder::build error
|
|
|
|
|
|
// see https://docs.rs/octocrab/0.18.1/src/octocrab/lib.rs.html#360-364
|
2022-03-03 09:04:55 +00:00
|
|
|
|
.map_err(ErrorKind::BuildGithubClient)?,
|
2022-02-19 00:44:00 +00:00
|
|
|
|
),
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
_ => None,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
let filter = Filter {
|
2023-01-16 18:14:09 +00:00
|
|
|
|
includes: self.includes.map(|regex| Includes { regex }),
|
|
|
|
|
|
excludes: self.excludes.map(|regex| Excludes { regex }),
|
|
|
|
|
|
schemes: self.schemes,
|
2022-02-09 23:04:48 +00:00
|
|
|
|
// exclude_all_private option turns on all "private" excludes,
|
|
|
|
|
|
// including private IPs, link-local IPs and loopback IPs
|
|
|
|
|
|
exclude_private_ips: self.exclude_all_private || self.exclude_private_ips,
|
|
|
|
|
|
exclude_link_local_ips: self.exclude_all_private || self.exclude_link_local_ips,
|
|
|
|
|
|
exclude_loopback_ips: self.exclude_all_private || self.exclude_loopback_ips,
|
2023-07-19 17:58:38 +00:00
|
|
|
|
include_mail: self.include_mail,
|
2022-02-09 23:04:48 +00:00
|
|
|
|
};
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
|
|
|
|
|
|
let quirks = Quirks::default();
|
|
|
|
|
|
|
|
|
|
|
|
Ok(Client {
|
|
|
|
|
|
reqwest_client,
|
2022-02-09 23:04:48 +00:00
|
|
|
|
github_client,
|
2023-01-16 18:14:09 +00:00
|
|
|
|
remaps: self.remaps,
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
filter,
|
2022-01-07 00:03:10 +00:00
|
|
|
|
max_retries: self.max_retries,
|
2023-01-16 18:14:09 +00:00
|
|
|
|
retry_wait_time: self.retry_wait_time,
|
|
|
|
|
|
method: self.method,
|
|
|
|
|
|
accepted: self.accepted,
|
2021-09-04 01:21:54 +00:00
|
|
|
|
require_https: self.require_https,
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
quirks,
|
2023-07-31 14:04:00 +00:00
|
|
|
|
include_fragments: self.include_fragments,
|
|
|
|
|
|
fragment_checker: FragmentChecker::new(),
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Handles incoming requests and returns responses.
|
|
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// See [`ClientBuilder`] which contains sane defaults for all configuration
|
|
|
|
|
|
/// options.
|
2022-02-09 23:04:48 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
|
pub struct Client {
|
|
|
|
|
|
/// Underlying `reqwest` client instance that handles the HTTP requests.
|
|
|
|
|
|
reqwest_client: reqwest::Client,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// Optional GitHub client that handles communications with GitHub.
|
2022-02-11 22:43:47 +00:00
|
|
|
|
github_client: Option<Octocrab>,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// Optional remapping rules for URIs matching pattern.
|
2022-05-29 19:41:22 +00:00
|
|
|
|
remaps: Option<Remaps>,
|
|
|
|
|
|
|
2023-07-05 13:05:19 +00:00
|
|
|
|
/// Rules to decided whether each link should be checked or ignored.
|
2022-02-09 23:04:48 +00:00
|
|
|
|
filter: Filter,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Maximum number of retries per request before returning an error.
|
|
|
|
|
|
max_retries: u64,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// Initial wait time between retries of failed requests. This doubles after
|
|
|
|
|
|
/// each failure.
|
2022-02-24 11:24:57 +00:00
|
|
|
|
retry_wait_time: Duration,
|
|
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// HTTP method used for requests, e.g. `GET` or `HEAD`.
|
2022-02-24 11:24:57 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// The same method will be used for all links.
|
2022-02-09 23:04:48 +00:00
|
|
|
|
method: reqwest::Method,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Set of accepted return codes / status codes.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Unmatched return codes/ status codes are deemed as errors.
|
|
|
|
|
|
accepted: Option<HashSet<StatusCode>>,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Requires using HTTPS when it's available.
|
|
|
|
|
|
///
|
2023-02-09 14:32:16 +00:00
|
|
|
|
/// This would treat unencrypted links as errors when HTTPS is available.
|
2022-02-09 23:04:48 +00:00
|
|
|
|
require_https: bool,
|
2022-02-24 11:24:57 +00:00
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Override behaviors for certain known issues with special URIs.
|
|
|
|
|
|
quirks: Quirks,
|
2023-07-31 14:04:00 +00:00
|
|
|
|
|
|
|
|
|
|
/// Enable the checking of fragments in links.
|
|
|
|
|
|
include_fragments: bool,
|
|
|
|
|
|
|
|
|
|
|
|
/// Caches Fragments
|
|
|
|
|
|
fragment_checker: FragmentChecker,
|
2022-02-09 23:04:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
impl Client {
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// Check a single request.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// `request` can be either a [`Request`] or a type that can be converted
|
|
|
|
|
|
/// into it. In any case, it must represent a valid URI.
|
2021-10-07 16:07:18 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// # Errors
|
|
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// Returns an `Err` if:
|
|
|
|
|
|
/// - `request` does not represent a valid URI.
|
|
|
|
|
|
/// - Encrypted connection for a HTTP URL is available but unused. (Only
|
|
|
|
|
|
/// checked when `Client::require_https` is `true`.)
|
|
|
|
|
|
#[allow(clippy::missing_panics_doc)]
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
pub async fn check<T, E>(&self, request: T) -> Result<Response>
|
|
|
|
|
|
where
|
|
|
|
|
|
Request: TryFrom<T, Error = E>,
|
|
|
|
|
|
ErrorKind: From<E>,
|
|
|
|
|
|
{
|
2023-01-16 18:14:09 +00:00
|
|
|
|
let Request {
|
|
|
|
|
|
ref mut uri,
|
2023-06-26 10:06:24 +00:00
|
|
|
|
ref credentials,
|
2023-01-16 18:14:09 +00:00
|
|
|
|
source,
|
|
|
|
|
|
..
|
|
|
|
|
|
} = request.try_into()?;
|
2021-12-16 17:45:52 +00:00
|
|
|
|
|
2023-07-05 13:05:19 +00:00
|
|
|
|
// Allow filtering based on element and attribute
|
|
|
|
|
|
// if !self.filter.is_allowed(uri) {
|
|
|
|
|
|
// return Ok(Response::new(
|
|
|
|
|
|
// uri.clone(),
|
|
|
|
|
|
// Status::Excluded,
|
|
|
|
|
|
// source,
|
|
|
|
|
|
// ));
|
|
|
|
|
|
// }
|
Add optional Rustls support (#1099)
* Add optional Rustls support
This commit adds a non-default feature flag to use Rustls instead of OpenSSL.
My personal motivation is to use Lychee on OpenBSD -current, where the
`openssl` crate frequently fails to link against the unreleased system
LibreSSL. Using the `vendored-openssl` feature helps with compilation, but
segfaults at runtime.
The commit adds three feature flags to the library, binary, benchmark, and all
examples:
- The `native-tls` feature flag toggles the `openssl` crate.
- The `rustls-tls` feature flag toggles the `rustls` crate.
- The `email-check` feature flag toggles the `check-if-email-exists` crate,
which is the only existing functionality currently incompatible with Rustls.
By default, `native-tls` and `email-check` are enabled. Thus, Lychee (bin and
lib) can be used as before unless default features are disabled.
To use the Rustls feature, pass `--no-default-features --features rustls` to
cargo check/build/test/..., e.g.,
$ cargo clippy --workspace --all-targets --no-default-features \ --features
rustls-tls -- --deny warnings
Checking email addresses requires both, `native-tls` and `email-check`, to be
enabled. Otherwise, email addresses are excluded.
The `email-check` feature flag is technically not necessary. I preferred it
over `not(rustls-tls)` because it's clearer and it addresses the AGPL license
issue #594. As far as I understand, a Lychee binary compiled without the
`email-check` feature could be distributed with file-based copyleft for the
MPL-licensed dependencies only. But that's out of scope here.
The benchmark shows a performance regression varying between 2% and 4.4% when
using Rustls instead of OpenSSL on my machine.
PS: The `ring` crate needs to be patched on OpenBSD 7.3 and later until the new
xonly patches have been upstreamed, see the `rust-ring` port.
* Use platform native certificates with Rustls
By default, reqwest uses the webpki-roots crate with Rustls, effectively
bundling Mozilla's root certificates.
This commit uses the rustls-native-certs crate instead to use locally
installed root certificates, to minimize the difference between the
native-tls and rustls-tls features.
* Document feature flags
2023-06-16 00:21:57 +00:00
|
|
|
|
|
2023-07-05 13:05:19 +00:00
|
|
|
|
self.remap(uri)?;
|
|
|
|
|
|
|
|
|
|
|
|
if self.is_excluded(uri) {
|
|
|
|
|
|
return Ok(Response::new(uri.clone(), Status::Excluded, source));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let status = match uri.scheme() {
|
2023-07-31 14:04:00 +00:00
|
|
|
|
_ if uri.is_file() => self.check_file(uri).await,
|
2023-07-05 13:05:19 +00:00
|
|
|
|
_ if uri.is_mail() => self.check_mail(uri).await,
|
|
|
|
|
|
_ => self.check_website(uri, credentials).await?,
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
2022-05-29 19:41:22 +00:00
|
|
|
|
Ok(Response::new(uri.clone(), status, source))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// Remap `uri` using the client-defined remapping rules.
|
2023-07-05 13:05:19 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// # Errors
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Returns an `Err` if the final, remapped `uri` is not a valid URI.
|
|
|
|
|
|
pub fn remap(&self, uri: &mut Uri) -> Result<()> {
|
2023-01-16 18:14:09 +00:00
|
|
|
|
if let Some(ref remaps) = self.remaps {
|
2023-07-05 13:05:19 +00:00
|
|
|
|
uri.url = remaps.remap(&uri.url)?;
|
2022-05-29 19:41:22 +00:00
|
|
|
|
}
|
2023-07-05 13:05:19 +00:00
|
|
|
|
Ok(())
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Returns whether the given `uri` should be ignored from checking.
|
2021-10-07 16:07:18 +00:00
|
|
|
|
#[must_use]
|
2022-01-14 14:25:51 +00:00
|
|
|
|
pub fn is_excluded(&self, uri: &Uri) -> bool {
|
2021-09-06 14:10:48 +00:00
|
|
|
|
self.filter.is_excluded(uri)
|
|
|
|
|
|
}
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
|
2023-07-05 13:05:19 +00:00
|
|
|
|
/// Checks the given URI of a website.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// # Errors
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This returns an `Err` if
|
|
|
|
|
|
/// - The URI is invalid.
|
|
|
|
|
|
/// - The request failed.
|
|
|
|
|
|
/// - The response status code is not accepted.
|
|
|
|
|
|
/// - The URI cannot be converted to HTTPS.
|
|
|
|
|
|
pub async fn check_website(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
uri: &Uri,
|
|
|
|
|
|
credentials: &Option<BasicAuthCredentials>,
|
|
|
|
|
|
) -> Result<Status> {
|
|
|
|
|
|
match self.check_website_inner(uri, credentials).await {
|
|
|
|
|
|
Status::Ok(code) if self.require_https && uri.scheme() == "http" => {
|
|
|
|
|
|
if self
|
|
|
|
|
|
.check_website_inner(&uri.to_https()?, credentials)
|
|
|
|
|
|
.await
|
|
|
|
|
|
.is_success()
|
|
|
|
|
|
{
|
|
|
|
|
|
Ok(Status::Error(ErrorKind::InsecureURL(uri.clone())))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// HTTPS is not available for this URI,
|
|
|
|
|
|
// so the original HTTP URL is fine.
|
|
|
|
|
|
Ok(Status::Ok(code))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
s => Ok(s),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-27 21:22:46 +00:00
|
|
|
|
/// Checks the given URI of a website.
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
2022-03-27 21:22:46 +00:00
|
|
|
|
/// Unsupported schemes will be ignored
|
2023-03-10 21:36:45 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// # Errors
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This returns an `Err` if
|
|
|
|
|
|
/// - The URI is invalid.
|
|
|
|
|
|
/// - The request failed.
|
|
|
|
|
|
/// - The response status code is not accepted.
|
2023-07-05 13:05:19 +00:00
|
|
|
|
pub async fn check_website_inner(
|
2023-06-26 10:06:24 +00:00
|
|
|
|
&self,
|
|
|
|
|
|
uri: &Uri,
|
|
|
|
|
|
credentials: &Option<BasicAuthCredentials>,
|
|
|
|
|
|
) -> Status {
|
2022-03-27 21:22:46 +00:00
|
|
|
|
// Workaround for upstream reqwest panic
|
2023-01-16 18:14:09 +00:00
|
|
|
|
if validate_url(&uri.url) {
|
2022-03-27 21:22:46 +00:00
|
|
|
|
if matches!(uri.scheme(), "http" | "https") {
|
|
|
|
|
|
// This is a truly invalid URI with a known scheme.
|
|
|
|
|
|
// If we pass that to reqwest it would panic.
|
|
|
|
|
|
return Status::Error(ErrorKind::InvalidURI(uri.clone()));
|
|
|
|
|
|
}
|
|
|
|
|
|
// This is merely a URI with a scheme that is not supported by
|
2022-07-17 16:40:45 +00:00
|
|
|
|
// reqwest yet. It would be safe to pass that to reqwest and it
|
|
|
|
|
|
// wouldn't panic, but it's also unnecessary, because it would
|
|
|
|
|
|
// simply return an error.
|
2022-03-27 21:22:46 +00:00
|
|
|
|
return Status::Unsupported(ErrorKind::InvalidURI(uri.clone()));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-06-26 10:06:24 +00:00
|
|
|
|
let status = self.retry_request(uri, credentials).await;
|
2023-03-10 21:36:45 +00:00
|
|
|
|
if status.is_success() {
|
|
|
|
|
|
return status;
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
}
|
2022-01-10 23:40:22 +00:00
|
|
|
|
|
|
|
|
|
|
// Pull out the heavy machinery in case of a failed normal request.
|
2022-02-09 23:04:48 +00:00
|
|
|
|
// This could be a GitHub URL and we ran into the rate limiter.
|
2023-01-16 18:14:09 +00:00
|
|
|
|
// TODO: We should first try to parse the URI as GitHub URI first (Lucius, Jan 2023)
|
2022-06-03 18:13:07 +00:00
|
|
|
|
if let Ok(github_uri) = GithubUri::try_from(uri) {
|
2022-03-03 09:04:55 +00:00
|
|
|
|
let status = self.check_github(github_uri).await;
|
|
|
|
|
|
// Only return Github status in case of success
|
|
|
|
|
|
// Otherwise return the original error, which has more information
|
|
|
|
|
|
if status.is_success() {
|
|
|
|
|
|
return status;
|
|
|
|
|
|
}
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-10 21:36:45 +00:00
|
|
|
|
/// Retry requests up to `max_retries` times
|
|
|
|
|
|
/// with an exponential backoff.
|
2023-06-26 10:06:24 +00:00
|
|
|
|
async fn retry_request(&self, uri: &Uri, credentials: &Option<BasicAuthCredentials>) -> Status {
|
2023-03-10 21:36:45 +00:00
|
|
|
|
let mut retries: u64 = 0;
|
|
|
|
|
|
let mut wait_time = self.retry_wait_time;
|
|
|
|
|
|
|
2023-06-26 10:06:24 +00:00
|
|
|
|
let mut status = self.check_default(uri, credentials).await;
|
2023-03-10 21:36:45 +00:00
|
|
|
|
while retries < self.max_retries {
|
|
|
|
|
|
if status.is_success() || !status.should_retry() {
|
|
|
|
|
|
return status;
|
|
|
|
|
|
}
|
|
|
|
|
|
retries += 1;
|
|
|
|
|
|
tokio::time::sleep(wait_time).await;
|
|
|
|
|
|
wait_time = wait_time.saturating_mul(2);
|
2023-06-26 10:06:24 +00:00
|
|
|
|
status = self.check_default(uri, credentials).await;
|
2023-03-10 21:36:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
status
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Check a `uri` hosted on `GitHub` via the GitHub API.
|
2022-01-12 00:06:44 +00:00
|
|
|
|
///
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// # Caveats
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Files inside private repositories won't get checked and instead would
|
2022-01-12 00:06:44 +00:00
|
|
|
|
/// be reported as valid if the repository itself is reachable through the
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// API.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// A better approach would be to download the file through the API or
|
2022-01-12 00:06:44 +00:00
|
|
|
|
/// clone the repo, but we chose the pragmatic approach.
|
|
|
|
|
|
async fn check_github(&self, uri: GithubUri) -> Status {
|
2023-01-16 18:14:09 +00:00
|
|
|
|
let Some(client) = &self.github_client else { return ErrorKind::MissingGitHubToken.into() };
|
2022-02-19 00:44:00 +00:00
|
|
|
|
let repo = match client.repos(&uri.owner, &uri.repo).get().await {
|
2022-01-12 00:06:44 +00:00
|
|
|
|
Ok(repo) => repo,
|
2022-02-19 00:44:00 +00:00
|
|
|
|
Err(e) => return ErrorKind::GithubRequest(e).into(),
|
2022-01-12 00:06:44 +00:00
|
|
|
|
};
|
2022-02-11 22:43:47 +00:00
|
|
|
|
if let Some(true) = repo.private {
|
2022-01-12 00:06:44 +00:00
|
|
|
|
// The private repo exists. Assume a given endpoint exists as well
|
|
|
|
|
|
// (e.g. `issues` in `github.com/org/private/issues`). This is not
|
|
|
|
|
|
// always the case but simplifies the check.
|
|
|
|
|
|
return Status::Ok(StatusCode::OK);
|
2022-02-19 00:44:00 +00:00
|
|
|
|
} else if let Some(endpoint) = uri.endpoint {
|
2022-01-12 00:06:44 +00:00
|
|
|
|
// The URI returned a non-200 status code from a normal request and
|
|
|
|
|
|
// now we find that this public repo is reachable through the API,
|
|
|
|
|
|
// so that must mean the full URI (which includes the additional
|
|
|
|
|
|
// endpoint) must be invalid.
|
2022-12-19 13:28:10 +00:00
|
|
|
|
return ErrorKind::InvalidGithubUrl(format!("{}/{}/{endpoint}", uri.owner, uri.repo))
|
2022-02-19 00:44:00 +00:00
|
|
|
|
.into();
|
2022-01-12 00:06:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
// Found public repo without endpoint
|
|
|
|
|
|
Status::Ok(StatusCode::OK)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Check a URI using [reqwest](https://github.com/seanmonstar/reqwest).
|
2023-06-26 10:06:24 +00:00
|
|
|
|
async fn check_default(&self, uri: &Uri, credentials: &Option<BasicAuthCredentials>) -> Status {
|
|
|
|
|
|
let request = match credentials {
|
|
|
|
|
|
Some(credentials) => self
|
|
|
|
|
|
.reqwest_client
|
|
|
|
|
|
.request(self.method.clone(), uri.as_str())
|
|
|
|
|
|
.header(AUTHORIZATION, credentials.to_authorization().0.encode())
|
|
|
|
|
|
.build(),
|
|
|
|
|
|
None => self
|
|
|
|
|
|
.reqwest_client
|
|
|
|
|
|
.request(self.method.clone(), uri.as_str())
|
|
|
|
|
|
.build(),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let request = match request {
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
Ok(r) => r,
|
|
|
|
|
|
Err(e) => return e.into(),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let request = self.quirks.apply(request);
|
|
|
|
|
|
|
|
|
|
|
|
match self.reqwest_client.execute(request).await {
|
|
|
|
|
|
Ok(ref response) => Status::new(response, self.accepted.clone()),
|
|
|
|
|
|
Err(e) => e.into(),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Check a `file` URI.
|
2023-07-31 14:04:00 +00:00
|
|
|
|
pub async fn check_file(&self, uri: &Uri) -> Status {
|
|
|
|
|
|
let Ok(path) = uri.url.to_file_path() else {
|
|
|
|
|
|
return ErrorKind::InvalidFilePath(uri.clone()).into();
|
|
|
|
|
|
};
|
|
|
|
|
|
if !path.exists() {
|
|
|
|
|
|
return ErrorKind::InvalidFilePath(uri.clone()).into();
|
|
|
|
|
|
}
|
|
|
|
|
|
if self.include_fragments {
|
|
|
|
|
|
self.check_fragment(&path, uri).await
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Status::Ok(StatusCode::OK)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Checks a `file` URI's fragment.
|
|
|
|
|
|
pub async fn check_fragment(&self, path: &Path, uri: &Uri) -> Status {
|
|
|
|
|
|
match self.fragment_checker.check(path, &uri.url).await {
|
|
|
|
|
|
Ok(true) => Status::Ok(StatusCode::OK),
|
|
|
|
|
|
Ok(false) => ErrorKind::InvalidFragment(uri.clone()).into(),
|
|
|
|
|
|
Err(err) => {
|
|
|
|
|
|
warn!("Skipping fragment check due to the following error: {err}");
|
|
|
|
|
|
Status::Ok(StatusCode::OK)
|
2021-06-30 23:44:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// Check a mail address, or equivalently a `mailto` URI.
|
2022-11-05 22:40:33 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// URIs may contain query parameters (e.g. `contact@example.com?subject="Hello"`),
|
|
|
|
|
|
/// which are ignored by this check. The are not part of the mail address
|
|
|
|
|
|
/// and instead passed to a mail client.
|
Add optional Rustls support (#1099)
* Add optional Rustls support
This commit adds a non-default feature flag to use Rustls instead of OpenSSL.
My personal motivation is to use Lychee on OpenBSD -current, where the
`openssl` crate frequently fails to link against the unreleased system
LibreSSL. Using the `vendored-openssl` feature helps with compilation, but
segfaults at runtime.
The commit adds three feature flags to the library, binary, benchmark, and all
examples:
- The `native-tls` feature flag toggles the `openssl` crate.
- The `rustls-tls` feature flag toggles the `rustls` crate.
- The `email-check` feature flag toggles the `check-if-email-exists` crate,
which is the only existing functionality currently incompatible with Rustls.
By default, `native-tls` and `email-check` are enabled. Thus, Lychee (bin and
lib) can be used as before unless default features are disabled.
To use the Rustls feature, pass `--no-default-features --features rustls` to
cargo check/build/test/..., e.g.,
$ cargo clippy --workspace --all-targets --no-default-features \ --features
rustls-tls -- --deny warnings
Checking email addresses requires both, `native-tls` and `email-check`, to be
enabled. Otherwise, email addresses are excluded.
The `email-check` feature flag is technically not necessary. I preferred it
over `not(rustls-tls)` because it's clearer and it addresses the AGPL license
issue #594. As far as I understand, a Lychee binary compiled without the
`email-check` feature could be distributed with file-based copyleft for the
MPL-licensed dependencies only. But that's out of scope here.
The benchmark shows a performance regression varying between 2% and 4.4% when
using Rustls instead of OpenSSL on my machine.
PS: The `ring` crate needs to be patched on OpenBSD 7.3 and later until the new
xonly patches have been upstreamed, see the `rust-ring` port.
* Use platform native certificates with Rustls
By default, reqwest uses the webpki-roots crate with Rustls, effectively
bundling Mozilla's root certificates.
This commit uses the rustls-native-certs crate instead to use locally
installed root certificates, to minimize the difference between the
native-tls and rustls-tls features.
* Document feature flags
2023-06-16 00:21:57 +00:00
|
|
|
|
#[cfg(all(feature = "email-check", feature = "native-tls"))]
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
pub async fn check_mail(&self, uri: &Uri) -> Status {
|
2022-11-05 22:40:33 +00:00
|
|
|
|
let address = uri.url.path().to_string();
|
|
|
|
|
|
let input = CheckEmailInput::new(address);
|
2022-08-16 10:35:34 +00:00
|
|
|
|
let result = &(check_email(&input).await);
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
|
|
|
|
|
|
if let Reachable::Invalid = result.is_reachable {
|
2022-01-14 21:22:53 +00:00
|
|
|
|
ErrorKind::UnreachableEmailAddress(uri.clone(), mail::error_from_output(result)).into()
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
Status::Ok(StatusCode::OK)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-07-05 13:05:19 +00:00
|
|
|
|
|
2023-08-04 13:11:29 +00:00
|
|
|
|
/// Check a mail address, or equivalently a `mailto` URI.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This implementation simply excludes all email addresses.
|
2023-07-05 13:05:19 +00:00
|
|
|
|
#[cfg(not(all(feature = "email-check", feature = "native-tls")))]
|
2023-08-04 13:11:29 +00:00
|
|
|
|
#[allow(clippy::unused_async)]
|
|
|
|
|
|
pub async fn check_mail(&self, _uri: &Uri) -> Status {
|
2023-07-05 13:05:19 +00:00
|
|
|
|
Status::Excluded
|
|
|
|
|
|
}
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-22 12:15:11 +00:00
|
|
|
|
// Check if the given `Url` would cause `reqwest` to panic.
|
|
|
|
|
|
// This is a workaround for https://github.com/lycheeverse/lychee/issues/539
|
|
|
|
|
|
// and can be removed once https://github.com/seanmonstar/reqwest/pull/1399
|
|
|
|
|
|
// got merged.
|
2022-03-27 21:22:46 +00:00
|
|
|
|
// It is exactly the same check that reqwest runs internally, but unfortunately
|
|
|
|
|
|
// it `unwrap`s (and panics!) instead of returning an error, which we could handle.
|
2023-01-16 18:14:09 +00:00
|
|
|
|
fn validate_url(url: &Url) -> bool {
|
|
|
|
|
|
http::Uri::try_from(url.as_str()).is_err()
|
2022-03-22 12:15:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-10 21:36:45 +00:00
|
|
|
|
/// A shorthand function to check a single URI.
|
2022-02-09 23:04:48 +00:00
|
|
|
|
///
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// This provides the simplest link check utility without having to create a
|
|
|
|
|
|
/// [`Client`]. For more complex scenarios, see documentation of
|
|
|
|
|
|
/// [`ClientBuilder`] instead.
|
2022-01-14 14:25:51 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// # Errors
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Returns an `Err` if:
|
2023-01-16 18:14:09 +00:00
|
|
|
|
/// - The request client cannot be built (see [`ClientBuilder::client`] for
|
|
|
|
|
|
/// failure cases).
|
2022-02-09 23:04:48 +00:00
|
|
|
|
/// - The request cannot be checked (see [`Client::check`] for failure cases).
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
pub async fn check<T, E>(request: T) -> Result<Response>
|
|
|
|
|
|
where
|
|
|
|
|
|
Request: TryFrom<T, Error = E>,
|
|
|
|
|
|
ErrorKind: From<E>,
|
|
|
|
|
|
{
|
2021-04-16 18:25:22 +00:00
|
|
|
|
let client = ClientBuilder::builder().build().client()?;
|
2022-04-25 17:18:50 +00:00
|
|
|
|
client.check(request).await
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2022-06-03 18:13:07 +00:00
|
|
|
|
mod tests {
|
2021-07-04 23:35:36 +00:00
|
|
|
|
use std::{
|
|
|
|
|
|
fs::File,
|
|
|
|
|
|
time::{Duration, Instant},
|
|
|
|
|
|
};
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
|
|
|
|
|
|
use http::{header::HeaderMap, StatusCode};
|
|
|
|
|
|
use reqwest::header;
|
2021-07-04 23:35:36 +00:00
|
|
|
|
use tempfile::tempdir;
|
2023-03-10 14:15:37 +00:00
|
|
|
|
use wiremock::matchers::path;
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
|
|
|
|
|
|
use super::ClientBuilder;
|
2021-09-07 22:21:00 +00:00
|
|
|
|
use crate::{mock_server, test_utils::get_mock_client_response, Uri};
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_nonexistent() {
|
|
|
|
|
|
let mock_server = mock_server!(StatusCode::NOT_FOUND);
|
|
|
|
|
|
let res = get_mock_client_response(mock_server.uri()).await;
|
|
|
|
|
|
|
|
|
|
|
|
assert!(res.status().is_failure());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_nonexistent_with_path() {
|
|
|
|
|
|
let res = get_mock_client_response("http://127.0.0.1/invalid").await;
|
|
|
|
|
|
assert!(res.status().is_failure());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_github() {
|
|
|
|
|
|
let res = get_mock_client_response("https://github.com/lycheeverse/lychee").await;
|
|
|
|
|
|
assert!(res.status().is_success());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
2022-01-12 00:23:58 +00:00
|
|
|
|
async fn test_github_nonexistent_repo() {
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
let res = get_mock_client_response("https://github.com/lycheeverse/not-lychee").await;
|
2022-01-12 00:23:58 +00:00
|
|
|
|
assert!(res.status().is_failure());
|
|
|
|
|
|
}
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
|
2022-01-12 00:23:58 +00:00
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_github_nonexistent_file() {
|
|
|
|
|
|
let res = get_mock_client_response(
|
|
|
|
|
|
"https://github.com/lycheeverse/lychee/blob/master/NON_EXISTENT_FILE.md",
|
|
|
|
|
|
)
|
|
|
|
|
|
.await;
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
assert!(res.status().is_failure());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_youtube() {
|
|
|
|
|
|
// This is applying a quirk. See the quirks module.
|
|
|
|
|
|
let res = get_mock_client_response("https://www.youtube.com/watch?v=NlKuICiT470&list=PLbWDhxwM_45mPVToqaIZNbZeIzFchsKKQ&index=7").await;
|
|
|
|
|
|
assert!(res.status().is_success());
|
|
|
|
|
|
|
|
|
|
|
|
let res = get_mock_client_response("https://www.youtube.com/watch?v=invalidNlKuICiT470&list=PLbWDhxwM_45mPVToqaIZNbZeIzFchsKKQ&index=7").await;
|
|
|
|
|
|
assert!(res.status().is_failure());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_non_github() {
|
|
|
|
|
|
let mock_server = mock_server!(StatusCode::OK);
|
|
|
|
|
|
let res = get_mock_client_response(mock_server.uri()).await;
|
|
|
|
|
|
|
|
|
|
|
|
assert!(res.status().is_success());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_invalid_ssl() {
|
|
|
|
|
|
let res = get_mock_client_response("https://expired.badssl.com/").await;
|
|
|
|
|
|
|
|
|
|
|
|
assert!(res.status().is_failure());
|
|
|
|
|
|
|
|
|
|
|
|
// Same, but ignore certificate error
|
2021-04-16 18:25:22 +00:00
|
|
|
|
let res = ClientBuilder::builder()
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
.allow_insecure(true)
|
|
|
|
|
|
.build()
|
2021-04-16 18:25:22 +00:00
|
|
|
|
.client()
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
.unwrap()
|
|
|
|
|
|
.check("https://expired.badssl.com/")
|
|
|
|
|
|
.await
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
assert!(res.status().is_success());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-04 23:35:36 +00:00
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_file() {
|
|
|
|
|
|
let dir = tempdir().unwrap();
|
|
|
|
|
|
let file = dir.path().join("temp");
|
|
|
|
|
|
File::create(file).unwrap();
|
|
|
|
|
|
let uri = format!("file://{}", dir.path().join("temp").to_str().unwrap());
|
|
|
|
|
|
|
|
|
|
|
|
let res = get_mock_client_response(uri).await;
|
|
|
|
|
|
assert!(res.status().is_success());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_custom_headers() {
|
|
|
|
|
|
// See https://github.com/rust-lang/crates.io/issues/788
|
|
|
|
|
|
let mut custom = HeaderMap::new();
|
|
|
|
|
|
custom.insert(header::ACCEPT, "text/html".parse().unwrap());
|
2021-04-16 18:25:22 +00:00
|
|
|
|
let res = ClientBuilder::builder()
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
.custom_headers(custom)
|
|
|
|
|
|
.build()
|
2021-04-16 18:25:22 +00:00
|
|
|
|
.client()
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
.unwrap()
|
|
|
|
|
|
.check("https://crates.io/crates/lychee")
|
|
|
|
|
|
.await
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
assert!(res.status().is_success());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-07 22:21:00 +00:00
|
|
|
|
#[tokio::test]
|
2023-07-19 17:58:38 +00:00
|
|
|
|
async fn test_exclude_mail_by_default() {
|
2021-09-07 22:21:00 +00:00
|
|
|
|
let client = ClientBuilder::builder()
|
|
|
|
|
|
.exclude_all_private(true)
|
|
|
|
|
|
.build()
|
|
|
|
|
|
.client()
|
|
|
|
|
|
.unwrap();
|
2023-07-19 17:58:38 +00:00
|
|
|
|
assert!(client.is_excluded(&Uri {
|
2022-02-18 09:29:49 +00:00
|
|
|
|
url: "mailto://mail@example.com".try_into().unwrap()
|
2021-09-07 22:21:00 +00:00
|
|
|
|
}));
|
2023-07-19 17:58:38 +00:00
|
|
|
|
}
|
2021-09-07 22:21:00 +00:00
|
|
|
|
|
2023-07-19 17:58:38 +00:00
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_include_mail() {
|
2021-09-07 22:21:00 +00:00
|
|
|
|
let client = ClientBuilder::builder()
|
2023-07-19 17:58:38 +00:00
|
|
|
|
.include_mail(false)
|
2021-09-07 22:21:00 +00:00
|
|
|
|
.exclude_all_private(true)
|
|
|
|
|
|
.build()
|
|
|
|
|
|
.client()
|
|
|
|
|
|
.unwrap();
|
2022-01-14 14:25:51 +00:00
|
|
|
|
assert!(client.is_excluded(&Uri {
|
2022-02-18 09:29:49 +00:00
|
|
|
|
url: "mailto://mail@example.com".try_into().unwrap()
|
2021-09-07 22:21:00 +00:00
|
|
|
|
}));
|
2023-07-19 17:58:38 +00:00
|
|
|
|
|
|
|
|
|
|
let client = ClientBuilder::builder()
|
|
|
|
|
|
.include_mail(true)
|
|
|
|
|
|
.exclude_all_private(true)
|
|
|
|
|
|
.build()
|
|
|
|
|
|
.client()
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
assert!(!client.is_excluded(&Uri {
|
|
|
|
|
|
url: "mailto://mail@example.com".try_into().unwrap()
|
|
|
|
|
|
}));
|
2021-09-07 22:21:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-04 01:21:54 +00:00
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_require_https() {
|
|
|
|
|
|
let client = ClientBuilder::builder().build().client().unwrap();
|
2022-02-18 09:29:49 +00:00
|
|
|
|
let res = client.check("http://example.com").await.unwrap();
|
2021-09-04 01:21:54 +00:00
|
|
|
|
assert!(res.status().is_success());
|
|
|
|
|
|
|
|
|
|
|
|
// Same request will fail if HTTPS is required
|
|
|
|
|
|
let client = ClientBuilder::builder()
|
|
|
|
|
|
.require_https(true)
|
|
|
|
|
|
.build()
|
|
|
|
|
|
.client()
|
|
|
|
|
|
.unwrap();
|
2022-02-18 09:29:49 +00:00
|
|
|
|
let res = client.check("http://example.com").await.unwrap();
|
2021-09-04 01:21:54 +00:00
|
|
|
|
assert!(res.status().is_failure());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_timeout() {
|
|
|
|
|
|
// Note: this checks response timeout, not connect timeout.
|
|
|
|
|
|
// To check connect timeout, we'd have to do something more involved,
|
|
|
|
|
|
// see: https://github.com/LukeMathWalker/wiremock-rs/issues/19
|
|
|
|
|
|
let mock_delay = Duration::from_millis(20);
|
|
|
|
|
|
let checker_timeout = Duration::from_millis(10);
|
|
|
|
|
|
assert!(mock_delay > checker_timeout);
|
|
|
|
|
|
|
|
|
|
|
|
let mock_server = mock_server!(StatusCode::OK, set_delay(mock_delay));
|
|
|
|
|
|
|
2021-04-16 18:25:22 +00:00
|
|
|
|
let client = ClientBuilder::builder()
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
.timeout(checker_timeout)
|
|
|
|
|
|
.build()
|
2021-04-16 18:25:22 +00:00
|
|
|
|
.client()
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
let res = client.check(mock_server.uri()).await.unwrap();
|
|
|
|
|
|
assert!(res.status().is_timeout());
|
|
|
|
|
|
}
|
2022-03-22 12:15:11 +00:00
|
|
|
|
|
2023-03-10 21:36:45 +00:00
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_exponential_backoff() {
|
|
|
|
|
|
let mock_delay = Duration::from_millis(20);
|
|
|
|
|
|
let checker_timeout = Duration::from_millis(10);
|
|
|
|
|
|
assert!(mock_delay > checker_timeout);
|
|
|
|
|
|
|
|
|
|
|
|
let mock_server = mock_server!(StatusCode::OK, set_delay(mock_delay));
|
|
|
|
|
|
|
2023-04-21 20:27:38 +00:00
|
|
|
|
// Perform a warm-up request to ensure the lazy regexes
|
|
|
|
|
|
// in lychee-lib/src/quirks/mod.rs are compiled.
|
|
|
|
|
|
// On some platforms, this can take some time(approx. 110ms),
|
|
|
|
|
|
// which should not be counted in the test.
|
|
|
|
|
|
let warm_up_client = ClientBuilder::builder()
|
|
|
|
|
|
.max_retries(0_u64)
|
|
|
|
|
|
.build()
|
|
|
|
|
|
.client()
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
let _res = warm_up_client.check(mock_server.uri()).await.unwrap();
|
|
|
|
|
|
|
2023-03-10 21:36:45 +00:00
|
|
|
|
let client = ClientBuilder::builder()
|
|
|
|
|
|
.timeout(checker_timeout)
|
|
|
|
|
|
.max_retries(3_u64)
|
|
|
|
|
|
.retry_wait_time(Duration::from_millis(50))
|
|
|
|
|
|
.build()
|
|
|
|
|
|
.client()
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
// Summary:
|
|
|
|
|
|
// 1. First request fails with timeout (after 10ms)
|
|
|
|
|
|
// 2. Retry after 50ms (total 60ms)
|
|
|
|
|
|
// 3. Second request fails with timeout (after 10ms)
|
|
|
|
|
|
// 4. Retry after 100ms (total 160ms)
|
|
|
|
|
|
// 5. Third request fails with timeout (after 10ms)
|
|
|
|
|
|
// 6. Retry after 200ms (total 360ms)
|
|
|
|
|
|
// Total: 360ms
|
|
|
|
|
|
|
2023-05-26 11:32:28 +00:00
|
|
|
|
let start = Instant::now();
|
2023-03-10 21:36:45 +00:00
|
|
|
|
let res = client.check(mock_server.uri()).await.unwrap();
|
|
|
|
|
|
let end = start.elapsed();
|
|
|
|
|
|
|
|
|
|
|
|
assert!(res.status().is_failure());
|
|
|
|
|
|
|
|
|
|
|
|
// on slow connections, this might take a bit longer than nominal
|
|
|
|
|
|
// backed-off timeout (7 secs)
|
Add optional Rustls support (#1099)
* Add optional Rustls support
This commit adds a non-default feature flag to use Rustls instead of OpenSSL.
My personal motivation is to use Lychee on OpenBSD -current, where the
`openssl` crate frequently fails to link against the unreleased system
LibreSSL. Using the `vendored-openssl` feature helps with compilation, but
segfaults at runtime.
The commit adds three feature flags to the library, binary, benchmark, and all
examples:
- The `native-tls` feature flag toggles the `openssl` crate.
- The `rustls-tls` feature flag toggles the `rustls` crate.
- The `email-check` feature flag toggles the `check-if-email-exists` crate,
which is the only existing functionality currently incompatible with Rustls.
By default, `native-tls` and `email-check` are enabled. Thus, Lychee (bin and
lib) can be used as before unless default features are disabled.
To use the Rustls feature, pass `--no-default-features --features rustls` to
cargo check/build/test/..., e.g.,
$ cargo clippy --workspace --all-targets --no-default-features \ --features
rustls-tls -- --deny warnings
Checking email addresses requires both, `native-tls` and `email-check`, to be
enabled. Otherwise, email addresses are excluded.
The `email-check` feature flag is technically not necessary. I preferred it
over `not(rustls-tls)` because it's clearer and it addresses the AGPL license
issue #594. As far as I understand, a Lychee binary compiled without the
`email-check` feature could be distributed with file-based copyleft for the
MPL-licensed dependencies only. But that's out of scope here.
The benchmark shows a performance regression varying between 2% and 4.4% when
using Rustls instead of OpenSSL on my machine.
PS: The `ring` crate needs to be patched on OpenBSD 7.3 and later until the new
xonly patches have been upstreamed, see the `rust-ring` port.
* Use platform native certificates with Rustls
By default, reqwest uses the webpki-roots crate with Rustls, effectively
bundling Mozilla's root certificates.
This commit uses the rustls-native-certs crate instead to use locally
installed root certificates, to minimize the difference between the
native-tls and rustls-tls features.
* Document feature flags
2023-06-16 00:21:57 +00:00
|
|
|
|
assert!((350..=550).contains(&end.as_millis()));
|
2023-03-10 21:36:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-22 12:15:11 +00:00
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_avoid_reqwest_panic() {
|
|
|
|
|
|
let client = ClientBuilder::builder().build().client().unwrap();
|
|
|
|
|
|
// This request will fail, but it won't panic
|
|
|
|
|
|
let res = client.check("http://\"").await.unwrap();
|
|
|
|
|
|
assert!(res.status().is_failure());
|
|
|
|
|
|
}
|
2023-03-10 14:15:37 +00:00
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_max_redirects() {
|
|
|
|
|
|
let mock_server = wiremock::MockServer::start().await;
|
|
|
|
|
|
|
|
|
|
|
|
let ok_uri = format!("{}/ok", &mock_server.uri());
|
|
|
|
|
|
let redirect_uri = format!("{}/redirect", &mock_server.uri());
|
|
|
|
|
|
|
|
|
|
|
|
// Set up permanent redirect loop
|
|
|
|
|
|
let redirect = wiremock::ResponseTemplate::new(StatusCode::PERMANENT_REDIRECT)
|
|
|
|
|
|
.insert_header("Location", ok_uri.as_str());
|
|
|
|
|
|
wiremock::Mock::given(wiremock::matchers::method("GET"))
|
|
|
|
|
|
.and(path("/redirect"))
|
|
|
|
|
|
.respond_with(redirect)
|
|
|
|
|
|
.mount(&mock_server)
|
|
|
|
|
|
.await;
|
|
|
|
|
|
|
|
|
|
|
|
let ok = wiremock::ResponseTemplate::new(StatusCode::OK);
|
|
|
|
|
|
wiremock::Mock::given(wiremock::matchers::method("GET"))
|
|
|
|
|
|
.and(path("/ok"))
|
|
|
|
|
|
.respond_with(ok)
|
|
|
|
|
|
.mount(&mock_server)
|
|
|
|
|
|
.await;
|
|
|
|
|
|
|
|
|
|
|
|
let client = ClientBuilder::builder()
|
2023-06-23 13:49:05 +00:00
|
|
|
|
.max_redirects(0_usize)
|
2023-03-10 14:15:37 +00:00
|
|
|
|
.build()
|
|
|
|
|
|
.client()
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
let res = client.check(redirect_uri.clone()).await.unwrap();
|
|
|
|
|
|
assert!(res.status().is_failure());
|
|
|
|
|
|
|
|
|
|
|
|
let client = ClientBuilder::builder()
|
2023-06-23 13:49:05 +00:00
|
|
|
|
.max_redirects(1_usize)
|
2023-03-10 14:15:37 +00:00
|
|
|
|
.build()
|
|
|
|
|
|
.client()
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
let res = client.check(redirect_uri).await.unwrap();
|
|
|
|
|
|
assert!(res.status().is_success());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_limit_max_redirects() {
|
|
|
|
|
|
let mock_server = wiremock::MockServer::start().await;
|
|
|
|
|
|
|
|
|
|
|
|
// Set up permanent redirect loop
|
|
|
|
|
|
let template = wiremock::ResponseTemplate::new(StatusCode::PERMANENT_REDIRECT)
|
|
|
|
|
|
.insert_header("Location", mock_server.uri().as_str());
|
|
|
|
|
|
wiremock::Mock::given(wiremock::matchers::method("GET"))
|
|
|
|
|
|
.respond_with(template)
|
|
|
|
|
|
.mount(&mock_server)
|
|
|
|
|
|
.await;
|
|
|
|
|
|
|
|
|
|
|
|
let client = ClientBuilder::builder()
|
|
|
|
|
|
.max_redirects(0_usize)
|
|
|
|
|
|
.build()
|
|
|
|
|
|
.client()
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
let res = client.check(mock_server.uri()).await.unwrap();
|
|
|
|
|
|
assert!(res.status().is_failure());
|
|
|
|
|
|
}
|
2023-03-10 21:36:45 +00:00
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
|
async fn test_unsupported_scheme() {
|
|
|
|
|
|
let examples = vec![
|
|
|
|
|
|
"ftp://example.com",
|
|
|
|
|
|
"gopher://example.com",
|
|
|
|
|
|
"slack://example.com",
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
for example in examples {
|
|
|
|
|
|
let client = ClientBuilder::builder().build().client().unwrap();
|
|
|
|
|
|
let res = client.check(example).await.unwrap();
|
|
|
|
|
|
assert!(res.status().is_unsupported());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
Major refactor of codebase (#208)
- The binary component and library component are separated as two
packages in the same workspace.
- `lychee` is the binary component, in `lychee-bin/*`.
- `lychee-lib` is the library component, in `lychee-lib/*`.
- Users can now install only the `lychee-lib`, instead of both
components, that would require fewer dependencies and faster
compilation.
- Dependencies for each component are adjusted and updated. E.g.,
no CLI dependencies for `lychee-lib`.
- CLI tests are only moved to `lychee`, as it has nothing to do
with the library component.
- `Status::Error` is refactored to contain dedicated error enum,
`ErrorKind`.
- The motivation is to delay the formatting of errors to strings.
Note that `e.to_string()` is not necessarily cheap (though
trivial in many cases). The formatting is no delayed until the
error is needed to be displayed to users. So in some cases, if
the error is never used, it means that it won't be formatted at
all.
- Replaced `regex` based matching with one of the following:
- Simple string equality test in the case of 'false positivie'.
- URL parsing based test, in the case of extracting repository and
user name for GitHub links.
- Either cases would be much more efficient than `regex` based
matching. First, there's no need to construct a state machine for
regex. Second, URL is already verified and parsed on its creation,
and extracting its components is fairly cheap. Also, this removes
the dependency on `lazy-static` in `lychee-lib`.
- `types` module now has a sub-directory, and its components are now
separated into their own modules (in that sub-directory).
- `lychee-lib::test_utils` module is only compiled for tests.
- `wiremock` is moved to `dev-dependency` as it's only needed for
`test` modules.
- Dependencies are listed in alphabetical order.
- Imports are organized in the following fashion:
- Imports from `std`
- Imports from 3rd-party crates, and `lychee-lib`.
- Imports from `crate::*` or `super::*`.
- No glob import.
- I followed suggestion from `cargo clippy`, with `clippy::all` and
`clippy:pedantic`.
Co-authored-by: Lucius Hu <lebensterben@users.noreply.github.com>
2021-04-14 23:24:11 +00:00
|
|
|
|
}
|