Skip to content

Commit 661bd9c

Browse files
committed
Set default user agent for external requests
Many servers will return errors (e.g. 400/403) to requests that do not set a User-Agent header. This results in issues in both the link_checker and load_data components. With the link_checker these are false positive dead links. In load_data, remote data fails to be fetched. To mitigate this issue, this sets a default User-Agent of $CARGO_PKG_NAME/$CARGO_PKG_VERSION Note that the root cause of this regression from zola v0.9.0 is that reqwest 0.10 changed their default behavior and no longer sets a User-Agent by default: seanmonstar/reqwest#751 Fixes #950.
1 parent 15a3ab1 commit 661bd9c

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

components/link_checker/src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ pub fn check_url(url: &str, config: &LinkChecker) -> LinkResult {
5858
headers.insert(ACCEPT, "text/html".parse().unwrap());
5959
headers.append(ACCEPT, "*/*".parse().unwrap());
6060

61-
let client = Client::new();
61+
let client = Client::builder()
62+
.user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")))
63+
.build()
64+
.expect("reqwest client build");
6265

6366
let check_anchor = !config.skip_anchor_prefixes.iter().any(|prefix| url.starts_with(prefix));
6467

@@ -185,6 +188,22 @@ mod tests {
185188
assert!(res.error.is_none());
186189
}
187190

191+
#[test]
192+
fn set_default_user_agent() {
193+
let user_agent = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
194+
let _m1 = mock("GET", "/C4Szbfnvj6M0LoPk")
195+
.match_header("User-Agent", user_agent)
196+
.with_status(200)
197+
.with_body("Test")
198+
.create();
199+
200+
let url = format!("{}{}", mockito::server_url(), "/C4Szbfnvj6M0LoPk");
201+
let res = check_url(&url, &LinkChecker::default());
202+
assert!(res.is_valid());
203+
assert!(res.code.is_some());
204+
assert!(res.error.is_none());
205+
}
206+
188207
#[test]
189208
fn can_fail_301_to_404_links() {
190209
let _m1 = mock("GET", "/cav9vibhsc")

components/templates/src/global_fns/load_data.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,12 @@ pub struct LoadData {
178178
}
179179
impl LoadData {
180180
pub fn new(base_path: PathBuf) -> Self {
181-
let client = Arc::new(Mutex::new(Client::builder().build().expect("reqwest client build")));
181+
let client = Arc::new(Mutex::new(
182+
Client::builder()
183+
.user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")))
184+
.build()
185+
.expect("reqwest client build"),
186+
));
182187
let result_cache = Arc::new(Mutex::new(HashMap::new()));
183188
Self { base_path, client, result_cache }
184189
}
@@ -443,6 +448,31 @@ mod tests {
443448
);
444449
}
445450

451+
#[test]
452+
fn set_default_user_agent() {
453+
let user_agent = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
454+
let _m = mock("GET", "/chu8aizahBiy")
455+
.match_header("User-Agent", user_agent)
456+
.with_header("content-type", "application/json")
457+
.with_body(
458+
r#"{
459+
"test": {
460+
"foo": "bar"
461+
}
462+
}
463+
"#,
464+
)
465+
.create();
466+
467+
let url = format!("{}{}", mockito::server_url(), "/chu8aizahBiy");
468+
let static_fn = LoadData::new(PathBuf::new());
469+
let mut args = HashMap::new();
470+
args.insert("url".to_string(), to_value(&url).unwrap());
471+
args.insert("format".to_string(), to_value("json").unwrap());
472+
let result = static_fn.call(&args).unwrap();
473+
assert_eq!(result.get("test").unwrap().get("foo").unwrap(), &to_value("bar").unwrap());
474+
}
475+
446476
#[test]
447477
fn can_load_toml() {
448478
let static_fn = LoadData::new(PathBuf::from("../utils/test-files"));

0 commit comments

Comments
 (0)