From 4fa2eda983b9de7033ed958e8344bbc733ec866c Mon Sep 17 00:00:00 2001 From: Sunshine Date: Thu, 31 Dec 2020 12:17:17 -1000 Subject: [PATCH] make it possible to specify an empty user-agent string --- src/main.rs | 10 ++++++---- src/opts.rs | 15 ++++++++------- src/tests/opts.rs | 6 ++++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6b7efae..0f1e243 100644 --- a/src/main.rs +++ b/src/main.rs @@ -114,10 +114,12 @@ fn main() { // Initialize client let mut cache = HashMap::new(); let mut header_map = HeaderMap::new(); - header_map.insert( - USER_AGENT, - HeaderValue::from_str(&options.user_agent).expect("Invalid User-Agent header specified"), - ); + if let Some(user_agent) = &options.user_agent { + header_map.insert( + USER_AGENT, + HeaderValue::from_str(&user_agent).expect("Invalid User-Agent header specified"), + ); + } let timeout: u64 = if options.timeout > 0 { options.timeout } else { diff --git a/src/opts.rs b/src/opts.rs index cb35539..84ee88c 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -16,7 +16,7 @@ pub struct Options { pub output: String, pub silent: bool, pub timeout: u64, - pub user_agent: String, + pub user_agent: Option, pub no_video: bool, pub target: String, } @@ -38,7 +38,7 @@ impl Options { pub fn from_args() -> Options { let app = App::new(env!("CARGO_PKG_NAME")) .version(crate_version!()) - .author(crate_authors!("\n")) + .author(format!("\n{}", crate_authors!("\n")).as_str()) .about(format!("{}\n{}", ASCII, crate_description!()).as_str()) .args_from_usage("-a, --no-audio 'Removes audio sources'") .args_from_usage("-b, --base-url=[http://localhost/] 'Sets custom base URL'") @@ -61,7 +61,7 @@ impl Options { .required(true) .takes_value(true) .index(1) - .help("URL or file path"), + .help("URL or file path, use - for stdin"), ) .get_matches(); let mut options: Options = Options::default(); @@ -91,10 +91,11 @@ impl Options { .unwrap_or(&DEFAULT_NETWORK_TIMEOUT.to_string()) .parse::() .unwrap(); - options.user_agent = app - .value_of("user-agent") - .unwrap_or(DEFAULT_USER_AGENT) - .to_string(); + if let Some(user_agent) = app.value_of("user-agent") { + options.user_agent = Some(str!(user_agent)); + } else { + options.user_agent = Some(DEFAULT_USER_AGENT.to_string()); + } options.no_video = app.is_present("no-video"); options diff --git a/src/tests/opts.rs b/src/tests/opts.rs index c00f4f3..ba93e93 100644 --- a/src/tests/opts.rs +++ b/src/tests/opts.rs @@ -13,8 +13,8 @@ mod passing { fn defaults() { let options: Options = Options::default(); - assert_eq!(options.target, str!()); assert_eq!(options.no_audio, false); + assert_eq!(options.base_url, None); assert_eq!(options.no_css, false); assert_eq!(options.no_frames, false); assert_eq!(options.no_fonts, false); @@ -26,7 +26,9 @@ mod passing { assert_eq!(options.output, str!()); assert_eq!(options.silent, false); 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.target, str!()); } }