Merge pull request #114 from snshn/custom-network-timeout-option

Add option for custom network request timeout
This commit is contained in:
Sunshine 2020-02-10 21:13:17 -05:00 committed by GitHub
commit f720fe0176
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 2 deletions

View file

@ -42,10 +42,11 @@ If compared to saving websites with `wget -mpk`, this tool embeds all assets as
- `-k`: Accept invalid X.509 (TLS) certificates - `-k`: Accept invalid X.509 (TLS) certificates
- `-o`: Write output to file - `-o`: Write output to file
- `-s`: Silent mode - `-s`: Silent mode
- `-t`: Set custom network request timeout (in seconds)
- `-u`: Specify custom User-Agent - `-u`: Specify custom User-Agent
## HTTPS and HTTP proxies ## HTTPS and HTTP proxies
Please set `https_proxy`, `http_proxy` and `no_proxy` environment variables. Please set `https_proxy`, `http_proxy`, and `no_proxy` environment variables.
## Contributing ## Contributing
Please open an issue if something is wrong, that helps make this project better. Please open an issue if something is wrong, that helps make this project better.

View file

@ -11,9 +11,11 @@ pub struct AppArgs {
pub isolate: bool, pub isolate: bool,
pub output: String, pub output: String,
pub silent: bool, pub silent: bool,
pub timeout: u64,
pub user_agent: String, pub user_agent: String,
} }
const DEFAULT_NETWORK_TIMEOUT: u64 = 120;
const DEFAULT_USER_AGENT: &str = const DEFAULT_USER_AGENT: &str =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:66.0) Gecko/20100101 Firefox/66.0"; "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:66.0) Gecko/20100101 Firefox/66.0";
@ -39,6 +41,7 @@ impl AppArgs {
.args_from_usage("-k, --insecure 'Accept invalid X.509 (TLS) certificates'") .args_from_usage("-k, --insecure 'Accept invalid X.509 (TLS) certificates'")
.args_from_usage("-o, --output=[document.html] 'Write output to <file>'") .args_from_usage("-o, --output=[document.html] 'Write output to <file>'")
.args_from_usage("-s, --silent 'Suppress verbosity'") .args_from_usage("-s, --silent 'Suppress verbosity'")
.args_from_usage("-t, --timeout=[60] 'Specify custom timeout for network requests'")
.args_from_usage("-u, --user-agent=[Iceweasel] 'Custom User-Agent string'") .args_from_usage("-u, --user-agent=[Iceweasel] 'Custom User-Agent string'")
// .args_from_usage("-v, --include-video 'Embed video sources'") // .args_from_usage("-v, --include-video 'Embed video sources'")
.get_matches(); .get_matches();
@ -55,6 +58,11 @@ impl AppArgs {
app_args.insecure = app.is_present("insecure"); app_args.insecure = app.is_present("insecure");
app_args.isolate = app.is_present("isolate"); app_args.isolate = app.is_present("isolate");
app_args.silent = app.is_present("silent"); app_args.silent = app.is_present("silent");
app_args.timeout = app
.value_of("timeout")
.unwrap_or(&DEFAULT_NETWORK_TIMEOUT.to_string())
.parse::<u64>()
.unwrap();
app_args.output = app.value_of("output").unwrap_or("").to_string(); app_args.output = app.value_of("output").unwrap_or("").to_string();
app_args.user_agent = app app_args.user_agent = app
.value_of("user-agent") .value_of("user-agent")

View file

@ -65,8 +65,13 @@ fn main() {
HeaderValue::from_str(&app_args.user_agent).expect("Invalid User-Agent header specified"), HeaderValue::from_str(&app_args.user_agent).expect("Invalid User-Agent header specified"),
); );
let timeout: u64 = if app_args.timeout > 0 {
app_args.timeout
} else {
std::u64::MAX / 4
};
let client = Client::builder() let client = Client::builder()
.timeout(Duration::from_secs(10)) .timeout(Duration::from_secs(timeout))
.danger_accept_invalid_certs(app_args.insecure) .danger_accept_invalid_certs(app_args.insecure)
.default_headers(header_map) .default_headers(header_map)
.build() .build()