From ab4b8b9d83dc6d50cd457398219f2e8a9acc854d Mon Sep 17 00:00:00 2001 From: DarkDrek Date: Sat, 12 May 2018 00:41:15 +0200 Subject: [PATCH] Add option to set user-agent Fixes #5494 --- src/cargo/ops/registry.rs | 12 ++++++++++-- tests/testsuite/bad_config.rs | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index 2e1f67142b8..5ed9c7f25b3 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -358,8 +358,12 @@ pub fn needs_custom_http_transport(config: &Config) -> CargoResult { let timeout = http_timeout(config)?; let cainfo = config.get_path("http.cainfo")?; let check_revoke = config.get_bool("http.check-revoke")?; + let user_agent = config.get_string("http.user-agent")?; - Ok(proxy_exists || timeout.is_some() || cainfo.is_some() || check_revoke.is_some()) + Ok( + proxy_exists || timeout.is_some() || cainfo.is_some() || check_revoke.is_some() + || user_agent.is_some(), + ) } /// Configure a libcurl http handle with the defaults options for Cargo @@ -371,7 +375,6 @@ pub fn configure_http_handle(config: &Config, handle: &mut Easy) -> CargoResult< handle.connect_timeout(Duration::new(30, 0))?; handle.low_speed_limit(10 /* bytes per second */)?; handle.low_speed_time(Duration::new(30, 0))?; - handle.useragent(&version().to_string())?; if let Some(proxy) = http_proxy(config)? { handle.proxy(&proxy)?; } @@ -385,6 +388,11 @@ pub fn configure_http_handle(config: &Config, handle: &mut Easy) -> CargoResult< handle.connect_timeout(Duration::new(timeout as u64, 0))?; handle.low_speed_time(Duration::new(timeout as u64, 0))?; } + if let Some(user_agent) = config.get_string("http.user-agent")? { + handle.useragent(&user_agent.val)?; + } else { + handle.useragent(&version().to_string())?; + } Ok(()) } diff --git a/tests/testsuite/bad_config.rs b/tests/testsuite/bad_config.rs index 1bdf2f3078a..0986fda731f 100644 --- a/tests/testsuite/bad_config.rs +++ b/tests/testsuite/bad_config.rs @@ -181,6 +181,43 @@ Caused by: ); } +#[test] +fn bad6() { + let p = project("foo") + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.0" + authors = [] + "#, + ) + .file("src/lib.rs", "") + .file( + ".cargo/config", + r#" + [http] + user-agent = true + "#, + ) + .build(); + Package::new("foo", "1.0.0").publish(); + + assert_that( + p.cargo("publish").arg("-v"), + execs().with_status(101).with_stderr( + "\ +error: failed to update registry [..] + +Caused by: + invalid configuration for key `http.user-agent` +expected a string, but found a boolean for `http.user-agent` in [..]config +", + ), + ); +} + #[test] fn bad_cargo_config_jobs() { let p = project("foo")