Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ab4b8b9

Browse files
committedMay 11, 2018
Add option to set user-agent
Fixes #5494
1 parent 2f7cf4a commit ab4b8b9

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed
 

‎src/cargo/ops/registry.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,12 @@ pub fn needs_custom_http_transport(config: &Config) -> CargoResult<bool> {
358358
let timeout = http_timeout(config)?;
359359
let cainfo = config.get_path("http.cainfo")?;
360360
let check_revoke = config.get_bool("http.check-revoke")?;
361+
let user_agent = config.get_string("http.user-agent")?;
361362

362-
Ok(proxy_exists || timeout.is_some() || cainfo.is_some() || check_revoke.is_some())
363+
Ok(
364+
proxy_exists || timeout.is_some() || cainfo.is_some() || check_revoke.is_some()
365+
|| user_agent.is_some(),
366+
)
363367
}
364368

365369
/// 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<
371375
handle.connect_timeout(Duration::new(30, 0))?;
372376
handle.low_speed_limit(10 /* bytes per second */)?;
373377
handle.low_speed_time(Duration::new(30, 0))?;
374-
handle.useragent(&version().to_string())?;
375378
if let Some(proxy) = http_proxy(config)? {
376379
handle.proxy(&proxy)?;
377380
}
@@ -385,6 +388,11 @@ pub fn configure_http_handle(config: &Config, handle: &mut Easy) -> CargoResult<
385388
handle.connect_timeout(Duration::new(timeout as u64, 0))?;
386389
handle.low_speed_time(Duration::new(timeout as u64, 0))?;
387390
}
391+
if let Some(user_agent) = config.get_string("http.user-agent")? {
392+
handle.useragent(&user_agent.val)?;
393+
} else {
394+
handle.useragent(&version().to_string())?;
395+
}
388396
Ok(())
389397
}
390398

‎tests/testsuite/bad_config.rs

+37
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,43 @@ Caused by:
181181
);
182182
}
183183

184+
#[test]
185+
fn bad6() {
186+
let p = project("foo")
187+
.file(
188+
"Cargo.toml",
189+
r#"
190+
[package]
191+
name = "foo"
192+
version = "0.0.0"
193+
authors = []
194+
"#,
195+
)
196+
.file("src/lib.rs", "")
197+
.file(
198+
".cargo/config",
199+
r#"
200+
[http]
201+
user-agent = true
202+
"#,
203+
)
204+
.build();
205+
Package::new("foo", "1.0.0").publish();
206+
207+
assert_that(
208+
p.cargo("publish").arg("-v"),
209+
execs().with_status(101).with_stderr(
210+
"\
211+
error: failed to update registry [..]
212+
213+
Caused by:
214+
invalid configuration for key `http.user-agent`
215+
expected a string, but found a boolean for `http.user-agent` in [..]config
216+
",
217+
),
218+
);
219+
}
220+
184221
#[test]
185222
fn bad_cargo_config_jobs() {
186223
let p = project("foo")

0 commit comments

Comments
 (0)
Please sign in to comment.