Skip to content

feat: detect website fragments #1675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lychee-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ secrecy = "0.10.3"
serde = { version = "1.0.219", features = ["derive"] }
serde_with = "3.12.0"
shellexpand = "3.1.0"
tempfile = "3.19.1"
thiserror = "2.0.12"
tokio = { version = "1.44.1", features = ["full"] }
toml = "0.8.20"
Expand All @@ -65,7 +66,6 @@ features = ["runtime-tokio"]

[dev-dependencies]
doc-comment = "0.3.3"
tempfile = "3.19.1"
wiremock = "0.6.3"
serde_json = "1.0.140"
rstest = "0.25.0"
Expand Down
48 changes: 43 additions & 5 deletions lychee-lib/src/checker/website.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use crate::{
quirks::Quirks,
retry::RetryExt,
types::uri::github::GithubUri,
utils::fragment_checker::FragmentChecker,
BasicAuthCredentials, ErrorKind, Status, Uri,
};
use async_trait::async_trait;
use http::StatusCode;
use http::{Method, StatusCode};
use octocrab::Octocrab;
use reqwest::Request;
use std::{collections::HashSet, time::Duration};
use reqwest::{Request, Response};
use std::{collections::HashSet, io::Write, time::Duration};
use tempfile::NamedTempFile;

#[derive(Debug, Clone)]
pub(crate) struct WebsiteChecker {
Expand Down Expand Up @@ -41,11 +43,19 @@ pub(crate) struct WebsiteChecker {
///
/// This would treat unencrypted links as errors when HTTPS is available.
require_https: bool,

/// Whether to check the existence of fragments in the response HTML files.
///
/// Will be disabled if the request method is `HEAD`.
include_fragments: bool,

/// Utility for performing fragment checks in HTML files.
fragment_checker: FragmentChecker,
}

impl WebsiteChecker {
#[allow(clippy::too_many_arguments)]
pub(crate) const fn new(
pub(crate) fn new(
method: reqwest::Method,
retry_wait_time: Duration,
max_retries: u64,
Expand All @@ -54,6 +64,7 @@ impl WebsiteChecker {
github_client: Option<Octocrab>,
require_https: bool,
plugin_request_chain: RequestChain,
include_fragments: bool,
) -> Self {
Self {
method,
Expand All @@ -64,6 +75,8 @@ impl WebsiteChecker {
retry_wait_time,
accepted,
require_https,
include_fragments,
fragment_checker: FragmentChecker::new(),
}
}

Expand All @@ -87,12 +100,37 @@ impl WebsiteChecker {

/// Check a URI using [reqwest](https://github.com/seanmonstar/reqwest).
async fn check_default(&self, request: Request) -> Status {
let method = request.method().clone();
match self.reqwest_client.execute(request).await {
Ok(ref response) => Status::new(response, self.accepted.clone()),
Ok(response) => {
let mut status = Status::new(&response, self.accepted.clone());
if self.include_fragments && status.is_success() && method == Method::GET {
status = self.check_html_fragment(status, response).await;
}
status
}
Err(e) => e.into(),
}
}

async fn check_html_fragment(&self, status: Status, response: Response) -> Status {
let url = response.url().clone();
match response.text().await {
Ok(text) => {
let mut file = NamedTempFile::with_suffix(".html").unwrap();
if let Err(e) = file.write_all(text.as_bytes()) {
return Status::Error(ErrorKind::ReadFileInput(e, file.path().to_path_buf()));
}
return match self.fragment_checker.check(file.path(), &url).await {
Ok(true) => status,
Ok(false) => Status::Error(ErrorKind::InvalidFragment(url.clone().into())),
Err(e) => Status::Error(e),
};
}
Err(e) => Status::Error(ErrorKind::ReadResponseBody(e)),
}
}

/// Checks the given URI of a website.
///
/// # Errors
Expand Down
1 change: 1 addition & 0 deletions lychee-lib/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ impl ClientBuilder {
github_client,
self.require_https,
self.plugin_request_chain,
self.include_fragments,
);

Ok(Client {
Expand Down
Loading