From 6e432bcb7e26fa83a6d071a923a61c055763b6a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BB=BA=E8=BE=9B?= Date: Tue, 20 Jun 2023 17:27:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=96=E9=83=A8=20toml?= =?UTF-8?q?=EF=BC=8C=20=E4=BF=AE=E5=A4=8D=20sync=20connect=20=E6=8A=A5?= =?UTF-8?q?=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张建辛 --- .gitignore | 2 + Cargo.toml | 7 +++ .../examples/async_https_outside.rs | 44 +++++++++++++++---- ylong_http_client/src/async_impl/connector.rs | 4 +- ylong_http_client/src/sync_impl/connector.rs | 14 +++--- 5 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 .gitignore create mode 100644 Cargo.toml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d2599e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..191768b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[workspace] +resolver = "2" +members = [ + # web + "ylong_http", + "ylong_http_client", +] diff --git a/ylong_http_client/examples/async_https_outside.rs b/ylong_http_client/examples/async_https_outside.rs index 58dc042..38efbab 100644 --- a/ylong_http_client/examples/async_https_outside.rs +++ b/ylong_http_client/examples/async_https_outside.rs @@ -15,28 +15,54 @@ use ylong_http_client::async_impl::{Client, Downloader}; use ylong_http_client::util::Redirect; -use ylong_http_client::{Certificate, Request}; +use ylong_http_client::{Certificate, Request, TlsVersion}; -#[tokio::main] -async fn main() { +fn main() { + let rt = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .expect("Tokio runtime build err."); + let mut v = vec![]; + for _i in 0..1 { + let handle = rt.spawn(req()); + v.push(handle); + } + + rt.block_on(async move { + for h in v { + let _ = h.await; + } + }); +} + +async fn req() { let v = include_bytes!("./test.pem"); let cert = Certificate::from_pem(v); // Creates a `async_impl::Client` let client = Client::builder() .redirect(Redirect::default()) + .tls_built_in_root_certs(true) // default true + .max_tls_version(TlsVersion::TLS_1_2) + .min_tls_version(TlsVersion::TLS_1_2) .add_root_certificate(cert.unwrap()) .build() .unwrap(); // Creates a `Request`. - let request = Request::get("https://www.huawei.com") - .body("".as_bytes()) - .unwrap(); + let request = Request::get( + "https://www.baidu.com", + // "https://app.prntscr.com/build/setup-lightshot.exe", + ) + .body("".as_bytes()) + .unwrap(); // Sends request and receives a `Response`. - let response = client.request(request).await; - assert!(response.is_ok()); + let response = client.request(request).await.unwrap(); + // assert!(response.is_ok()); + + println!("{}", response.status().as_u16()); + println!("{}", response.headers()); // Reads the body of `Response` by using `BodyReader`. - let _ = Downloader::console(response.unwrap()).download().await; + let _ = Downloader::console(response).download().await; } diff --git a/ylong_http_client/src/async_impl/connector.rs b/ylong_http_client/src/async_impl/connector.rs index 51f396c..c395011 100644 --- a/ylong_http_client/src/async_impl/connector.rs +++ b/ylong_http_client/src/async_impl/connector.rs @@ -85,8 +85,8 @@ pub(crate) mod no_tls { } } -#[cfg(feature = "__c_openssl")] -pub(crate) mod c_ssl { +#[cfg(feature = "__tls")] +pub(crate) mod tls_conn { use crate::{ async_impl::{AsyncSslStream, Connector, MixStream}, ErrorKind, HttpClientError, diff --git a/ylong_http_client/src/sync_impl/connector.rs b/ylong_http_client/src/sync_impl/connector.rs index e280172..4265674 100644 --- a/ylong_http_client/src/sync_impl/connector.rs +++ b/ylong_http_client/src/sync_impl/connector.rs @@ -71,14 +71,16 @@ pub mod no_tls { } } -#[cfg(feature = "__c_openssl")] -pub mod c_ssl { +#[cfg(feature = "__tls")] +pub mod tls_conn { use crate::{ sync_impl::{Connector, MixStream}, ErrorKind, HttpClientError, }; - use std::io::{Read, Write}; - use std::net::TcpStream; + use std::{ + io::{Read, Write}, + net::TcpStream, + }; use ylong_http::request::uri::{Scheme, Uri}; impl Connector for super::HttpConnector { @@ -124,12 +126,12 @@ pub mod c_ssl { tcp_stream }; - let mut tls_ssl = config.ssl().map_err(|e| { + let mut tls_ssl = self.config.tls.ssl().map_err(|e| { HttpClientError::new_with_cause(ErrorKind::Connect, Some(e)) })?; tls_ssl.set_sni_verify(&host_name).map_err(|e| { - HttpClientError::new_with_cause(ErrorKind::Connect, Some(e)); + HttpClientError::new_with_cause(ErrorKind::Connect, Some(e)) })?; let stream = self -- Gitee