From b68624f2f3da5751efc9861775ca2277a82fee50 Mon Sep 17 00:00:00 2001 From: rhysd Date: Mon, 20 Jan 2020 17:02:08 +0900 Subject: [PATCH 1/3] support HTTP and HTTPS proxies (fix #103) --- README.md | 4 ++++ src/main.rs | 44 ++++++++++++++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 776e4f3..077961c 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,10 @@ If compared to saving websites with `wget -mpk`, this tool embeds all assets as - `-s`: Silent mode - `-u`: Specify custom User-Agent +## HTTPS and HTTP proxies + +Please set `$https_proxy` and `$http_proxy` environment variables. + ## Related projects - `Pagesaver`: https://github.com/distributed-mind/pagesaver - `SingleFile`: https://github.com/gildas-lormeau/SingleFile diff --git a/src/main.rs b/src/main.rs index a805133..47a7937 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,9 @@ use monolith::http::retrieve_asset; use monolith::utils::is_valid_url; use reqwest::blocking::Client; use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT}; +use reqwest::Proxy; use std::collections::HashMap; +use std::env; use std::fs::File; use std::io::{self, Error, Write}; use std::process; @@ -44,6 +46,33 @@ impl Output { } } +fn create_http_client(args: &AppArgs) -> Result { + let mut header_map = HeaderMap::new(); + header_map.insert( + USER_AGENT, + HeaderValue::from_str(&args.user_agent).expect("Invalid User-Agent header specified"), + ); + + let mut builder = Client::builder() + .timeout(Duration::from_secs(10)) + .danger_accept_invalid_certs(args.insecure) + .default_headers(header_map); + + if let Ok(var) = env::var("https_proxy").or_else(|_| env::var("HTTPS_PROXY")) { + let proxy = Proxy::https(&var) + .expect("Could not set HTTPS proxy. Please check $https_proxy env var"); + builder = builder.proxy(proxy); + } + + if let Ok(var) = env::var("http_proxy").or_else(|_| env::var("HTTP_PROXY")) { + let proxy = + Proxy::http(&var).expect("Could not set HTTP proxy. Please check $http_proxy env var"); + builder = builder.proxy(proxy); + } + + builder.build() +} + fn main() { let app_args = AppArgs::get(); @@ -56,21 +85,8 @@ fn main() { } let mut output = Output::new(&app_args.output).expect("Could not prepare output"); - - // Initialize client let mut cache = HashMap::new(); - let mut header_map = HeaderMap::new(); - header_map.insert( - USER_AGENT, - HeaderValue::from_str(&app_args.user_agent).expect("Invalid User-Agent header specified"), - ); - - let client = Client::builder() - .timeout(Duration::from_secs(10)) - .danger_accept_invalid_certs(app_args.insecure) - .default_headers(header_map) - .build() - .expect("Failed to initialize HTTP client"); + let client = create_http_client(&app_args).expect("Failed to initialize HTTP client"); // Retrieve root document let (data, final_url) = retrieve_asset( From d47482fcd9208b1f5b54b9ed9afc2af7af287251 Mon Sep 17 00:00:00 2001 From: rhysd Date: Mon, 20 Jan 2020 17:17:24 +0900 Subject: [PATCH 2/3] fix crash at setting empty values to HTTP proxies with this patch `https_proxy=` and `http_proxy=` will work well. --- src/main.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 47a7937..1403b73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,15 +59,19 @@ fn create_http_client(args: &AppArgs) -> Result { .default_headers(header_map); if let Ok(var) = env::var("https_proxy").or_else(|_| env::var("HTTPS_PROXY")) { - let proxy = Proxy::https(&var) - .expect("Could not set HTTPS proxy. Please check $https_proxy env var"); - builder = builder.proxy(proxy); + if !var.is_empty() { + let proxy = Proxy::https(&var) + .expect("Could not set HTTPS proxy. Please check $https_proxy env var"); + builder = builder.proxy(proxy); + } } if let Ok(var) = env::var("http_proxy").or_else(|_| env::var("HTTP_PROXY")) { - let proxy = - Proxy::http(&var).expect("Could not set HTTP proxy. Please check $http_proxy env var"); - builder = builder.proxy(proxy); + if !var.is_empty() { + let proxy = Proxy::http(&var) + .expect("Could not set HTTP proxy. Please check $http_proxy env var"); + builder = builder.proxy(proxy); + } } builder.build() From 0420854ed6437e499eb899406d8adc7d2921ff56 Mon Sep 17 00:00:00 2001 From: Linda_pp Date: Mon, 20 Jan 2020 23:11:14 +0900 Subject: [PATCH 3/3] remove '$' from environment variable names in README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 077961c..8ac4fe0 100644 --- a/README.md +++ b/README.md @@ -46,8 +46,7 @@ If compared to saving websites with `wget -mpk`, this tool embeds all assets as - `-u`: Specify custom User-Agent ## HTTPS and HTTP proxies - -Please set `$https_proxy` and `$http_proxy` environment variables. +Please set `https_proxy` and `http_proxy` environment variables. ## Related projects - `Pagesaver`: https://github.com/distributed-mind/pagesaver