diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2599e39725484fa20530647d404f8d344ffb5d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..191768bca1d693e05ebc3b780cc73a910e76c862 --- /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 58dc042ef614de5f103f56cf9017ae8e5b1da593..38efbabc16f501b15dd9d387019c2061d7509a18 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 51f396c9f8aa7eed17f2cc99a55d47d0ee8ad080..c395011b4eaacb87a13b11aa0c8e71fcfe209b98 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 e280172004885a645c5f03001d91e6c2203ea54b..42656742c3c91f4bf57225aa3baf23209a1e7f67 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