2
0
mirror of https://github.com/Y2Z/monolith synced 2024-11-15 06:12:52 +00:00

Merge pull request #235 from snshn/allow-empty-user-agent-string

Make it possible to specify an empty user-agent string
This commit is contained in:
Sunshine 2020-12-31 13:02:35 -10:00 committed by GitHub
commit 57883b84b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 13 deletions

View File

@ -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 {

View File

@ -16,7 +16,7 @@ pub struct Options {
pub output: String,
pub silent: bool,
pub timeout: u64,
pub user_agent: String,
pub user_agent: Option<String>,
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::<u64>()
.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

View File

@ -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!());
}
}