Skip to content

Commit

Permalink
Add allow statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
0xMimir committed Oct 15, 2023
1 parent df5e85d commit 0789bd2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ pub struct CargoBrokrCli {
/// Test links for localhost
#[arg(long, default_value_t = false)]
pub test_localhost: bool,

/// In addition to regular success status codes, accept some failed status codes
#[arg(long)]
pub allow_statuses: Vec<u16>,
}
15 changes: 13 additions & 2 deletions src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ impl Brokr {
Ok(urls)
}

pub fn find_broken_links(&self, links: Vec<Url>, max_threads: Option<u8>) -> Vec<Url> {
pub fn find_broken_links(
&self,
links: Vec<Url>,
max_threads: Option<u8>,
allowed_statuses: Vec<u16>,
) -> Vec<Url> {
let max_threads = max_threads.unwrap_or(8);

let links = Arc::new(Mutex::new(links));
Expand All @@ -95,13 +100,19 @@ impl Brokr {
let running_threads = running_threads.clone();
let broken_links = broken_links.clone();
let links = links.clone();
let allowed_statuses = allowed_statuses.clone();
thread::spawn(move || {
running_threads.fetch_add(1, Ordering::Relaxed);
let client = Self::create_client();

while let Some(link) = Self::take_last(&links) {
let is_broken = match client.get(link.as_str()).send() {
Ok(response) => response.error_for_status().is_err(),
Ok(response) => {
let status = response.status().as_u16();

!(response.error_for_status().is_ok()
|| allowed_statuses.contains(&status))
}
Err(_) => true,
};

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() -> anyhow::Result<()> {

println!("Found {} links", links.len());

let invalid_links = brokr.find_broken_links(links, config.threads);
let invalid_links = brokr.find_broken_links(links, config.threads, vec![]);

if !invalid_links.is_empty() {
println!("\nFound {} invalid links\n", invalid_links.len());
Expand Down
2 changes: 1 addition & 1 deletion tests/scan-links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn test_scan_links() {
]
.to_vec();

let broken_links = brokr.find_broken_links(links, Some(10));
let broken_links = brokr.find_broken_links(links, Some(10), vec![]);

let broken_links = broken_links
.iter()
Expand Down

0 comments on commit 0789bd2

Please sign in to comment.