link_checker: Set Accept header

As mentioned in #381, crates.io 404's any request without an Accept:
text/html header.  It 200's any request with one, but at least
false-successes don't prevent checking any other links.

This also makes it easier to add a custom User-Agent if desired.

rustfmt and fix a clippy nit (unnecessary return) while I'm here.
This commit is contained in:
Thomas Hurst 2018-08-25 16:50:01 +01:00
parent 6d875db4c7
commit 6a5ace62fc

View file

@ -2,11 +2,11 @@ extern crate reqwest;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
use reqwest::header::{qitem, Accept, Headers};
use reqwest::{mime, StatusCode};
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use reqwest::StatusCode;
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct LinkResult { pub struct LinkResult {
@ -54,19 +54,30 @@ pub fn check_url(url: &str) -> LinkResult {
} }
} }
let mut headers = Headers::new();
headers.set(Accept(vec![qitem(mime::TEXT_HTML), qitem(mime::STAR_STAR)]));
let client = reqwest::Client::new();
// Need to actually do the link checking // Need to actually do the link checking
let res = match reqwest::get(url) { let res = match client.get(url).headers(headers).send() {
Ok(response) => LinkResult { code: Some(response.status()), error: None }, Ok(response) => LinkResult {
Err(e) => LinkResult { code: None, error: Some(e.description().to_string()) }, code: Some(response.status()),
error: None,
},
Err(e) => LinkResult {
code: None,
error: Some(e.description().to_string()),
},
}; };
LINKS.write().unwrap().insert(url.to_string(), res.clone()); LINKS.write().unwrap().insert(url.to_string(), res.clone());
return res; res
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{LINKS, check_url}; use super::{check_url, LINKS};
#[test] #[test]
fn can_validate_ok_links() { fn can_validate_ok_links() {