make it possible to specify an empty user-agent string

pull/235/head
Sunshine 3 years ago
parent 028187a31e
commit 4fa2eda983
No known key found for this signature in database
GPG Key ID: B80CA68703CD8AB1

@ -114,10 +114,12 @@ fn main() {
// Initialize client // Initialize client
let mut cache = HashMap::new(); let mut cache = HashMap::new();
let mut header_map = HeaderMap::new(); let mut header_map = HeaderMap::new();
header_map.insert( if let Some(user_agent) = &options.user_agent {
USER_AGENT, header_map.insert(
HeaderValue::from_str(&options.user_agent).expect("Invalid User-Agent header specified"), USER_AGENT,
); HeaderValue::from_str(&user_agent).expect("Invalid User-Agent header specified"),
);
}
let timeout: u64 = if options.timeout > 0 { let timeout: u64 = if options.timeout > 0 {
options.timeout options.timeout
} else { } else {

@ -16,7 +16,7 @@ pub struct Options {
pub output: String, pub output: String,
pub silent: bool, pub silent: bool,
pub timeout: u64, pub timeout: u64,
pub user_agent: String, pub user_agent: Option<String>,
pub no_video: bool, pub no_video: bool,
pub target: String, pub target: String,
} }
@ -38,7 +38,7 @@ impl Options {
pub fn from_args() -> Options { pub fn from_args() -> Options {
let app = App::new(env!("CARGO_PKG_NAME")) let app = App::new(env!("CARGO_PKG_NAME"))
.version(crate_version!()) .version(crate_version!())
.author(crate_authors!("\n")) .author(format!("\n{}", crate_authors!("\n")).as_str())
.about(format!("{}\n{}", ASCII, crate_description!()).as_str()) .about(format!("{}\n{}", ASCII, crate_description!()).as_str())
.args_from_usage("-a, --no-audio 'Removes audio sources'") .args_from_usage("-a, --no-audio 'Removes audio sources'")
.args_from_usage("-b, --base-url=[http://localhost/] 'Sets custom base URL'") .args_from_usage("-b, --base-url=[http://localhost/] 'Sets custom base URL'")
@ -61,7 +61,7 @@ impl Options {
.required(true) .required(true)
.takes_value(true) .takes_value(true)
.index(1) .index(1)
.help("URL or file path"), .help("URL or file path, use - for stdin"),
) )
.get_matches(); .get_matches();
let mut options: Options = Options::default(); let mut options: Options = Options::default();
@ -91,10 +91,11 @@ impl Options {
.unwrap_or(&DEFAULT_NETWORK_TIMEOUT.to_string()) .unwrap_or(&DEFAULT_NETWORK_TIMEOUT.to_string())
.parse::<u64>() .parse::<u64>()
.unwrap(); .unwrap();
options.user_agent = app if let Some(user_agent) = app.value_of("user-agent") {
.value_of("user-agent") options.user_agent = Some(str!(user_agent));
.unwrap_or(DEFAULT_USER_AGENT) } else {
.to_string(); options.user_agent = Some(DEFAULT_USER_AGENT.to_string());
}
options.no_video = app.is_present("no-video"); options.no_video = app.is_present("no-video");
options options

@ -13,8 +13,8 @@ mod passing {
fn defaults() { fn defaults() {
let options: Options = Options::default(); let options: Options = Options::default();
assert_eq!(options.target, str!());
assert_eq!(options.no_audio, false); assert_eq!(options.no_audio, false);
assert_eq!(options.base_url, None);
assert_eq!(options.no_css, false); assert_eq!(options.no_css, false);
assert_eq!(options.no_frames, false); assert_eq!(options.no_frames, false);
assert_eq!(options.no_fonts, false); assert_eq!(options.no_fonts, false);
@ -26,7 +26,9 @@ mod passing {
assert_eq!(options.output, str!()); assert_eq!(options.output, str!());
assert_eq!(options.silent, false); assert_eq!(options.silent, false);
assert_eq!(options.timeout, 0); assert_eq!(options.timeout, 0);
assert_eq!(options.user_agent, ""); assert_eq!(options.user_agent, None);
assert_eq!(options.no_video, false); assert_eq!(options.no_video, false);
assert_eq!(options.target, str!());
} }
} }

Loading…
Cancel
Save