From b67c3098cff596168278fd0a59e1f06c0de4e971 Mon Sep 17 00:00:00 2001 From: shikai liu Date: Mon, 13 May 2024 10:14:58 +0800 Subject: [PATCH 1/5] Update the version of rcurl. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 03737cd..3e1b781 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rcurl" -version = "0.1.0" +version = "0.0.10" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html From c149dfd1d81602165cb6a3ec118b08d61006b85c Mon Sep 17 00:00:00 2001 From: shikai liu Date: Mon, 13 May 2024 14:42:12 +0800 Subject: [PATCH 2/5] Bug fix. --- src/main.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 22ec91a..812f2eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -190,7 +190,6 @@ async fn main() { .init(); if let Err(e) = do_request(cli).await { println!("{}", e); - error!("{}", e); } } async fn do_request(cli: Cli) -> Result<(), anyhow::Error> { @@ -349,8 +348,8 @@ async fn do_request(cli: Cli) -> Result<(), anyhow::Error> { Ok(()) } }, - _ => { - error!("Request timeout in 5 seconds "); + Err(e) => { + error!("Request timeout in 5 seconds, {}", e); Ok(()) } } From 496a704469f5aec40e2cef9680dbd1972ccf77ff Mon Sep 17 00:00:00 2001 From: shikai liu Date: Mon, 13 May 2024 16:45:34 +0800 Subject: [PATCH 3/5] Bug fix. --- src/main.rs | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 812f2eb..15ae812 100644 --- a/src/main.rs +++ b/src/main.rs @@ -328,29 +328,19 @@ async fn do_request(cli: Cli) -> Result<(), anyhow::Error> { }; fut }; - let timeout_future = timeout(Duration::from_secs(5), request_future).await; - - match timeout_future { - Ok(res_option) => match res_option { - Ok(res) => { - if cli.debug { - let status = res.status(); - println!("< {:?} {}", res.version(), status); - for (key, value) in res.headers().iter() { - println!("< {}: {}", key, value.to_str()?); - } - } - handle_response(cli.file_path_option, res).await?; - return Ok(()); - } - Err(e) => { - error!("{}", e); - Ok(()) + let timeout_future = timeout(Duration::from_secs(5), request_future) + .await + .map_err(|e| anyhow!("Request timeout in 5 seconds, {}", e)); + if let Ok(Ok(res)) = timeout_future { + if cli.debug { + let status = res.status(); + println!("< {:?} {}", res.version(), status); + for (key, value) in res.headers().iter() { + println!("< {}: {}", key, value.to_str()?); } - }, - Err(e) => { - error!("Request timeout in 5 seconds, {}", e); - Ok(()) } + handle_response(cli.file_path_option, res).await?; + return Ok(()); } + Ok(()) } From 4c244ada3c60a6ec554f883575e3f5c9cfc7daf1 Mon Sep 17 00:00:00 2001 From: shikai liu Date: Tue, 14 May 2024 11:26:58 +0800 Subject: [PATCH 4/5] Bug fix. --- src/main.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 15ae812..4d18123 100644 --- a/src/main.rs +++ b/src/main.rs @@ -213,6 +213,7 @@ async fn do_request(cli: Cli) -> Result<(), anyhow::Error> { let verifier = WebPkiVerifier::builder(Arc::new(root_cert_store)) .build() .map_err(|e| anyhow!("{}", e))?; + ClientConfig::builder() .dangerous() .with_custom_certificate_verifier(Arc::new(NoHostnameTlsVerifier { verifier })) @@ -328,19 +329,17 @@ async fn do_request(cli: Cli) -> Result<(), anyhow::Error> { }; fut }; - let timeout_future = timeout(Duration::from_secs(5), request_future) + let res = timeout(Duration::from_secs(5), request_future) .await - .map_err(|e| anyhow!("Request timeout in 5 seconds, {}", e)); - if let Ok(Ok(res)) = timeout_future { - if cli.debug { - let status = res.status(); - println!("< {:?} {}", res.version(), status); - for (key, value) in res.headers().iter() { - println!("< {}: {}", key, value.to_str()?); - } + .map_err(|e| anyhow!("Request timeout in 5 seconds, {}", e))??; + if cli.debug { + let status = res.status(); + println!("< {:?} {}", res.version(), status); + for (key, value) in res.headers().iter() { + println!("< {}: {}", key, value.to_str()?); } - handle_response(cli.file_path_option, res).await?; - return Ok(()); } + handle_response(cli.file_path_option, res).await?; + Ok(()) } From 6e24070b2e15a988d3596fdcff4a6490f1d87bbc Mon Sep 17 00:00:00 2001 From: shikai liu Date: Wed, 15 May 2024 15:12:16 +0800 Subject: [PATCH 5/5] Add the user agent option. --- src/main.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 4d18123..e56625e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -165,6 +165,10 @@ struct Cli { #[arg(short = 'c', long)] certificate_path_option: Option, + /// The User Agent. + #[arg(short = 'A', long)] + user_agent_option: Option, + /// The downloading file path . #[arg(global = true, short = 'o', long, default_missing_value = "none")] file_path_option: Option, @@ -248,9 +252,12 @@ async fn do_request(cli: Cli) -> Result<(), anyhow::Error> { "host", HeaderValue::from_str(uri.host().ok_or(anyhow!("no host"))?)?, ); + let user_agent = cli + .user_agent_option + .unwrap_or(format!("rcur/{}", env!("CARGO_PKG_VERSION").to_string())); request .headers_mut() - .append("User-Agent", HeaderValue::from_str("rcurl/0.0.6")?); + .append("User-Agent", HeaderValue::from_str(&user_agent)?); for x in cli.headers { let split: Vec = x.splitn(2, ':').map(|s| s.to_string()).collect(); if split.len() == 2 {