diff --git a/ylong_http/Cargo.toml b/ylong_http/Cargo.toml index ae15713e16c90522515b28e2a05799619ab2c19c..53438ce2d2c1d915b8b70c28ac899b23910c7011 100644 --- a/ylong_http/Cargo.toml +++ b/ylong_http/Cargo.toml @@ -11,7 +11,7 @@ keywords = ["ylong", "http"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["http1_1"] +default = ["http1_1", "tokio_base"] full = [ "http1_1", "http2", @@ -23,8 +23,22 @@ http2 = [] # Uses HTTP/2. http3 = [] # Uses HTTP/3. huffman = [] # Uses Huffman encoding in `Hpack` and `Qpack`. +tokio_base = ["tokio"] # Uses asynchronous components of `tokio` +ylong_base = ["ylong_runtime"] # Uses asynchronous components of `tokio` + [dependencies] -tokio = { version = "1.20.1", features = ["io-util"] } +tokio = { version = "1.20.1", features = ["io-util"], optional = true } +ylong_runtime = { path = "../runtime/ylong_runtime", optional = true } [dev-dependencies] -tokio = { version = "1.20.1", features = ["io-util", "rt-multi-thread", "macros"] } \ No newline at end of file +tokio = { version = "1.20.1", features = ["io-util", "rt-multi-thread", "macros"] } + +[[example]] +name = "mimebody_multi" +path = "./examples/mimebody_multi.rs" +required-features = ["tokio_base"] + +[[example]] +name = "mimebody_multi_then_async_data" +path = "./examples/mimebody_multi_then_async_data.rs" +required-features = ["tokio_base"] \ No newline at end of file diff --git a/ylong_http/src/body/chunk.rs b/ylong_http/src/body/chunk.rs index 8e229ae3c3eadd6b2f178ce38f76bb59eae7e14c..dfbc47c8f4b647532658b03f7acb6cce145ee4c7 100644 --- a/ylong_http/src/body/chunk.rs +++ b/ylong_http/src/body/chunk.rs @@ -18,6 +18,7 @@ use super::{async_impl, sync_impl}; use crate::body::origin::FromAsyncBody; use crate::error::{ErrorKind, HttpError}; use crate::headers::{Header, HeaderName, HeaderValue, Headers}; +use crate::{AsyncRead, AsyncReadExt, ReadBuf}; use core::convert::Infallible; use core::ops::{Deref, DerefMut}; use core::pin::Pin; @@ -28,7 +29,6 @@ use std::collections::HashMap; use std::convert::{TryFrom, TryInto}; use std::future::Future; use std::io::{Error, Read}; -use tokio::io::{AsyncRead, AsyncReadExt, ReadBuf}; /// A chunk body is used to encode body to send message by chunk in `HTTP/1.1` format. /// @@ -1555,6 +1555,7 @@ mod ut_chunk { /// 1. Creates a `ChunkBody` by calling `ChunkBody::from_bytes`. /// 2. Encodes chunk body by calling `async_impl::Body::data` /// 3. Checks if the test result is correct. + #[cfg(feature = "tokio_base")] #[tokio::test] async fn ut_asnyc_chunk_body_encode_0() { let content = data_message(); @@ -1578,6 +1579,7 @@ mod ut_chunk { /// 1. Creates a `ChunkBody` by calling `ChunkBody::from_async_reader`. /// 2. Encodes chunk body by calling `async_impl::Body::data` /// 3. Checks if the test result is correct. + #[cfg(feature = "tokio_base")] #[tokio::test] async fn ut_asnyc_chunk_body_encode_1() { let content = data_message(); diff --git a/ylong_http/src/body/empty.rs b/ylong_http/src/body/empty.rs index 2c5694c5bf62dea91a15f1170d9b652ad1dc0442..c6201edf5d58ffefae8f4d17fada2330c33400c4 100644 --- a/ylong_http/src/body/empty.rs +++ b/ylong_http/src/body/empty.rs @@ -134,6 +134,7 @@ mod ut_empty { /// # Brief /// 1. Creates an `EmptyBody`. /// 2. Calls its `async_impl::Body::data` method and then checks the results. + #[cfg(feature = "tokio_base")] #[tokio::test] async fn ut_empty_body_async_impl_data() { use crate::body::async_impl::Body; diff --git a/ylong_http/src/body/mime/common/mix.rs b/ylong_http/src/body/mime/common/mix.rs index 579c7bbb7ff1917775f2017f3d27f4e20502dc9f..c4a58475e452164a6d114b2d9c7547a984540034 100644 --- a/ylong_http/src/body/mime/common/mix.rs +++ b/ylong_http/src/body/mime/common/mix.rs @@ -18,13 +18,13 @@ use crate::body::{ mime::common::{data_copy, SizeResult, TokenStatus}, sync_impl, }; +use crate::{AsyncRead, ReadBuf}; use core::{ fmt::Debug, pin::Pin, task::{Context, Poll}, }; use std::io::Read; -use tokio::io::{AsyncRead, ReadBuf}; // Uses Box so that it can be put into a list(like vec) with different T. pub(crate) enum MixFrom<'a> { @@ -414,6 +414,7 @@ mod ut_mix { /// 1. Creates a `MixFrom` from asynchronous read content by `MixFrom::set_async_reader`. /// 2. Encodes by asynchronous encoding. /// 3. Checks whether the result is correct. + #[cfg(feature = "tokio_base")] #[tokio::test] async fn ut_mix_set_async_reader() { mix_encode_compare!( @@ -431,6 +432,7 @@ mod ut_mix { /// 1. Creates a `MixFrom` from synchronous read content by `MixFrom::set_reader`. /// 2. Encodes by asynchronous encoding. /// 3. Checks whether the result is correct. + #[cfg(feature = "tokio_base")] #[tokio::test] async fn ut_mix_set_reader_then_async_data() { mix_encode_compare!( @@ -448,6 +450,7 @@ mod ut_mix { /// 1. Creates a `MixFrom` from synchronous read content by `MixFrom::set_bytes`. /// 2. Encodes by asynchronous encoding. /// 3. Checks whether the result is correct. + #[cfg(feature = "tokio_base")] #[tokio::test] async fn ut_mix_set_bytes_then_async_data() { mix_encode_compare!( @@ -465,6 +468,7 @@ mod ut_mix { /// 1. Creates a `MixFrom` from synchronous read content by `MixFrom::set_owned`. /// 2. Encodes by asynchronous encoding. /// 3. Checks whether the result is correct. + #[cfg(feature = "tokio_base")] #[tokio::test] async fn ut_mix_set_owned_then_async_data() { mix_encode_compare!( diff --git a/ylong_http/src/body/mime/common/part.rs b/ylong_http/src/body/mime/common/part.rs index 9240f4512c1671cdd2cb4692a238bfb386e88808..138173f4daebc3a0d57b872e4179e897d52121b4 100644 --- a/ylong_http/src/body/mime/common/part.rs +++ b/ylong_http/src/body/mime/common/part.rs @@ -14,6 +14,7 @@ */ use crate::headers::{HeaderName, HeaderValue}; +use crate::AsyncRead; use crate::{ body::mime::{MixFrom, CR, LF}, error::HttpError, @@ -21,7 +22,6 @@ use crate::{ }; use core::{convert::TryFrom, mem::take}; use std::io::Read; -use tokio::io::AsyncRead; /// `MimePart` is a body part of a Composite MIME body which is defined in [`RFC2046`]: \ /// The body must then contain one or more body parts, each preceded by a boundary diff --git a/ylong_http/src/body/mime/encode/multi.rs b/ylong_http/src/body/mime/encode/multi.rs index 817e3018860fefab509b06014410018672c1b193..9bf63b660ff91f96f02211f23708fd7a1ecbdefb 100644 --- a/ylong_http/src/body/mime/encode/multi.rs +++ b/ylong_http/src/body/mime/encode/multi.rs @@ -543,6 +543,7 @@ mod ut_mime_multi_encoder { /// 3. Creates a main `MimeMulti`, adds parts. /// 4. Builds a `MimeMultiEncoder` by `from_multi` and encodes asynchronously. /// 5. Checks whether the result is correct. + #[cfg(feature = "tokio_base")] #[tokio::test] async fn ut_mime_multi_encoder_data_many_parts_nesting_then_async_data() { let multi1 = MimeMulti::builder() diff --git a/ylong_http/src/body/mime/encode/part.rs b/ylong_http/src/body/mime/encode/part.rs index 936bfde8d43d61b547aba30457de784b96bc0a39..811efdeff52e9d0441d95c8077120daf482b7b41 100644 --- a/ylong_http/src/body/mime/encode/part.rs +++ b/ylong_http/src/body/mime/encode/part.rs @@ -24,13 +24,13 @@ use crate::{ }, error::{ErrorKind, HttpError}, }; +use crate::{AsyncRead, ReadBuf}; use core::{ convert::TryFrom, pin::Pin, task::{Context, Poll}, }; use std::io::Read; -use tokio::io::{AsyncRead, ReadBuf}; /// `MimePartEncoder` can get a [`MimePart`] to encode into data that can be transmitted. /// @@ -389,6 +389,7 @@ mod ut_mime_part_encoder { /// 2. Sets body by `body_from_owned`. /// 3. Builds a `MimePartEncoder` by `from_part` and encodes asynchronously. /// 4. Checks whether the result is correct. + #[cfg(feature = "tokio_base")] #[tokio::test] async fn ut_mime_part_encoder_body_from_owned_then_async_data() { part_encode_compare! ( @@ -409,6 +410,7 @@ mod ut_mime_part_encoder { /// 2. Sets body by `body_from_bytes`. /// 3. Builds a `MimePartEncoder` by `from_part` and encodes asynchronously. /// 4. Checks whether the result is correct. + #[cfg(feature = "tokio_base")] #[tokio::test] async fn ut_mime_part_encoder_body_from_bytes_then_async_data() { part_encode_compare! ( @@ -429,6 +431,7 @@ mod ut_mime_part_encoder { /// 2. Sets headers and sets body. /// 3. Builds a `MimePartEncoder` by `from_part` and encodes asynchronously. /// 4. Checks whether the result is correct. + #[cfg(feature = "tokio_base")] #[tokio::test] async fn ut_mime_part_encoder_common_then_async_data() { part_encode_compare! ( diff --git a/ylong_http/src/body/mime/mod.rs b/ylong_http/src/body/mime/mod.rs index 2c5d0dcf64852d21047211a1b8f31cf9b682f6ce..f72d40c9e6a85aa9e54476b5239d607d500f71ee 100644 --- a/ylong_http/src/body/mime/mod.rs +++ b/ylong_http/src/body/mime/mod.rs @@ -39,11 +39,11 @@ pub(crate) use decode::MimePartDecoder; // TODO: Adapter, remove this later. use crate::body::async_impl::{poll_read, Body}; +use crate::{AsyncRead, ReadBuf}; use std::io::Cursor; use std::pin::Pin; use std::task::{Context, Poll}; use std::vec::IntoIter; -use tokio::io::{AsyncRead, ReadBuf}; /// A structure that helps you build a `multipart/form-data` message. /// @@ -388,17 +388,6 @@ impl Part { /// Sets a stream body of this `Part`. /// /// The body message will be set to the body part. - /// - /// # Examples - /// - /// ``` - /// # use tokio::io::AsyncRead; - /// # use ylong_http::body::Part; - /// - /// # fn set_stream_body(stream: R) { - /// let part = Part::new().stream(stream); - /// # } - /// ``` pub fn stream(mut self, body: T) -> Self { self.body = Some(MultiPartState::stream(Box::pin(body))); self diff --git a/ylong_http/src/body/mod.rs b/ylong_http/src/body/mod.rs index c34199469a24d05472dc4c3f92cfdca20f7a4d11..1117e7ea3f6ee972ca5e6c62f64fcf669bbfadcc 100644 --- a/ylong_http/src/body/mod.rs +++ b/ylong_http/src/body/mod.rs @@ -174,11 +174,11 @@ pub mod sync_impl { /// Asynchronous `Body` trait definition. pub mod async_impl { use crate::headers::Headers; + use crate::{AsyncRead, ReadBuf}; use core::future::Future; use core::pin::Pin; use core::task::{Context, Poll}; use std::error::Error; - use tokio::io::{AsyncRead, ReadBuf}; /// The `async_impl::Body` trait allows for reading body data asynchronously. /// @@ -408,9 +408,9 @@ pub mod async_impl { // Type definitions of the origin of the body data. pub(crate) mod origin { + use crate::AsyncRead; use core::ops::{Deref, DerefMut}; use std::io::Read; - use tokio::io::AsyncRead; /// A type that represents the body data is from memory. pub struct FromBytes<'a> { @@ -539,6 +539,7 @@ mod ut_mod { /// 1. Creates a `async_impl::Body` object. /// 2. Gets its mutable reference. /// 3. Calls its `async_impl::Body::data` method and then checks the results. + #[cfg(feature = "tokio_base")] #[tokio::test] async fn ut_asyn_body_mut_asyn_body_data() { use crate::body::async_impl::Body; diff --git a/ylong_http/src/body/text.rs b/ylong_http/src/body/text.rs index b10dc56ce41679e3a02c09651a71f7fc32c02bfd..facecbaf1b12269017fb1bcde2ec61ac1dbdb87f 100644 --- a/ylong_http/src/body/text.rs +++ b/ylong_http/src/body/text.rs @@ -16,11 +16,11 @@ use super::origin::{FromAsyncReader, FromBytes, FromReader}; use super::{async_impl, sync_impl}; use crate::body::origin::FromAsyncBody; +use crate::{AsyncRead, ReadBuf}; use core::cmp::min; use core::pin::Pin; use core::task::{Context, Poll}; use std::io::{Error, Read}; -use tokio::io::{AsyncRead, ReadBuf}; /// `TextBody` is used to represent the body of plain text type. /// @@ -542,6 +542,7 @@ mod ut_text { /// # Brief /// 1. Creates a `TextBody>`. /// 2. Calls its `async_impl::Body::data` method and then checks the results. + #[cfg(feature = "tokio_base")] #[tokio::test] async fn ut_text_body_from_bytes_asyn_data() { use crate::body::async_impl::Body; @@ -594,6 +595,7 @@ mod ut_text { /// # Brief /// 1. Creates a `TextBody>`. /// 2. Calls its `async_impl::Body::data` method and then checks the results. + #[cfg(feature = "tokio_base")] #[tokio::test] async fn ut_text_body_from_async_reader_asyn_data() { use crate::body::async_impl::Body; diff --git a/ylong_http/src/lib.rs b/ylong_http/src/lib.rs index 45b3d8dc123c554272a7153e380121db780bedaa..3351b4ad97ad43bf93eed1b17e2864beee1cebd5 100644 --- a/ylong_http/src/lib.rs +++ b/ylong_http/src/lib.rs @@ -46,3 +46,9 @@ pub mod version; #[cfg(test)] pub(crate) mod test_util; + +#[cfg(feature = "tokio_base")] +pub(crate) use tokio::io::{AsyncRead, AsyncReadExt, ReadBuf}; + +#[cfg(feature = "ylong_base")] +pub(crate) use ylong_runtime::io::{AsyncRead, AsyncReadExt, ReadBuf}; diff --git a/ylong_http_client/Cargo.toml b/ylong_http_client/Cargo.toml index 25efa590b93df8c79966d78fea6048e49f0c7ac5..121ac47e5c38a7dab8363a1dbd7f1dc259b437a9 100644 --- a/ylong_http_client/Cargo.toml +++ b/ylong_http_client/Cargo.toml @@ -12,8 +12,9 @@ keywords = ["ylong", "http", "client"] [dependencies] ylong_http = { path = "../ylong_http", version = "1.9.0", features = ["full"] } -tokio = { version = "1.20.1", features = ["io-util", "net", "rt", "rt-multi-thread", "macros", "sync", "time"] } libc = { version = "0.2.134", optional = true } +tokio = { version = "1.20.1", features = ["io-util", "net", "rt", "rt-multi-thread", "macros", "sync", "time"], optional = true } +ylong_runtime = { path = "../runtime/ylong_runtime", features = ["net", "sync", "fs", "macros", "time"], optional = true } [dev-dependencies] hyper = { version = "0.14.23", features = ["http1", "tcp", "server"] } @@ -23,6 +24,7 @@ tokio-openssl = { version = "0.6.3" } [features] default = [] full = [ + "tokio_base", "sync", "async", "http1_1", @@ -34,8 +36,9 @@ async = [] # Uses async interf http1_1 = ["ylong_http/http1_1"] # Uses HTTP/1.1. http2 = ["ylong_http/http2"] # Uses HTTP/2. http3 = [] # Uses HTTP/3. -tokio_rt = ["tokio/rt", "tokio/rt-multi-thread"] # Uses tokio runtime. -# ylong_rt = ["ylong_sched"] # Uses ylong runtime. + +tokio_base = ["tokio"] # Uses tokio runtime. +ylong_base = ["ylong_runtime"] # Uses ylong runtime. tls_default = ["c_openssl_3_0"] __tls = [] # Not open to user, only mark to use tls for developer. @@ -46,55 +49,54 @@ c_openssl_3_0 = ["__c_openssl"] # Uses TLS by FFI o [[example]] name = "async_http" path = "examples/async_http.rs" -required-features = ["async", "http1_1"] +required-features = ["async", "http1_1", "tokio_base"] [[example]] -name = "sync_http" -path = "examples/sync_http.rs" -required-features = ["sync", "http1_1"] +name = "async_http2" +path = "examples/async_http2.rs" +required-features = ["async", "http2", "tokio_base"] [[example]] -name = "async_redirect_http" -path = "examples/async_redirect_http.rs" -required-features = ["async", "http1_1"] +name = "async_http2_multi" +path = "examples/async_http2_multi.rs" +required-features = ["async", "http2", "tokio_base"] [[example]] -name = "sync_redirect_http" -path = "examples/sync_redirect_http.rs" -required-features = ["sync", "http1_1"] +name = "async_https_outside" +path = "./examples/async_https_outside.rs" +required-features = ["async", "http1_1", "tls_default", "tokio_base"] [[example]] name = "async_proxy_http" path = "examples/async_proxy_http.rs" +required-features = ["async", "http1_1", "tokio_base"] + +[[example]] +name = "async_redirect_http" +path = "examples/async_redirect_http.rs" required-features = ["async", "http1_1"] [[example]] -name = "sync_proxy_http" -path = "examples/sync_proxy_http.rs" +name = "sync_http" +path = "examples/sync_http.rs" required-features = ["sync", "http1_1"] [[example]] -name = "sync_http2" -path = "./examples/async_http2.rs" -required-features = ["async", "http2"] +name = "sync_redirect_http" +path = "examples/sync_redirect_http.rs" +required-features = ["sync", "http1_1"] [[example]] -name = "sync_http2_multi" -path = "./examples/async_http2_multi.rs" -required-features = ["async", "http2"] +name = "sync_proxy_http" +path = "examples/sync_proxy_http.rs" +required-features = ["sync", "http1_1"] [[test]] name = "sdv_client" path = "./tests/sdv_client.rs" -required-features = ["sync", "async", "http1_1"] +required-features = ["sync", "async", "http1_1", "tokio_base"] [[test]] name = "sdv_https_c_ssl" path = "./tests/sdv_https_c_ssl.rs" -required-features = ["sync", "async", "http1_1", "c_openssl_1_1"] - -[[example]] -name = "async_https_outside" -path = "./examples/async_https_outside.rs" -required-features = ["async", "http1_1", "tls_default"] - +required-features = ["sync", "async", "http1_1", "c_openssl_1_1", "tokio_base"] diff --git a/ylong_http_client/src/async_impl/client.rs b/ylong_http_client/src/async_impl/client.rs index 59b289bfd8560169df3720e34714d7dc8a7eb672..01cb69531d0aba0aa20fe39adfd4f7b1657b260b 100644 --- a/ylong_http_client/src/async_impl/client.rs +++ b/ylong_http_client/src/async_impl/client.rs @@ -19,6 +19,7 @@ use crate::util::normalizer::{RequestFormatter, UriFormatter}; use crate::util::proxy::Proxies; use crate::util::redirect::TriggerKind; use crate::util::{ClientConfig, ConnectorConfig, HttpConfig, HttpVersion, Redirect}; +use crate::{sleep, timeout}; use crate::{ErrorKind, HttpClientError, Proxy, Request, Timeout, Uri}; use ylong_http::body::{ChunkBody, TextBody}; use ylong_http::response::Response; @@ -225,8 +226,8 @@ impl Client { #[cfg(feature = "http2")] HttpVersion::Http2PriorKnowledge => self.http2_request(uri, request).await, HttpVersion::Http11 => { - let conn = if let Some(timeout) = self.client_config.connect_timeout.inner() { - match tokio::time::timeout(timeout, self.inner.connect_to(uri)).await { + let conn = if let Some(dur) = self.client_config.connect_timeout.inner() { + match timeout(dur, self.inner.connect_to(uri)).await { Err(_elapsed) => { return Err(HttpClientError::new_with_message( ErrorKind::Timeout, @@ -243,7 +244,7 @@ impl Client { let mut retryable = Retryable::default(); if let Some(timeout) = self.client_config.request_timeout.inner() { TimeoutFuture { - timeout: Some(Box::pin(tokio::time::sleep(timeout))), + timeout: Some(Box::pin(sleep(timeout))), future: Box::pin(conn::request(conn, request, &mut retryable)), } .await diff --git a/ylong_http_client/src/async_impl/conn/http1.rs b/ylong_http_client/src/async_impl/conn/http1.rs index 3ce43d1da59763416e93533560ae7fbcfab3e0d4..c1814164d54ff648b337a34a9209377130bc34ec 100644 --- a/ylong_http_client/src/async_impl/conn/http1.rs +++ b/ylong_http_client/src/async_impl/conn/http1.rs @@ -18,9 +18,9 @@ use crate::error::{ErrorKind, HttpClientError}; use crate::util::dispatcher::http1::Http1Conn; use crate::util::normalizer::BodyLengthParser; use crate::Request; +use crate::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf}; use std::pin::Pin; use std::task::{Context, Poll}; -use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf}; use ylong_http::h1::{RequestEncoder, ResponseDecoder}; use ylong_http::response::Response; diff --git a/ylong_http_client/src/async_impl/conn/http2.rs b/ylong_http_client/src/async_impl/conn/http2.rs index 49f9b763cfe6cc8f7948da8158a5e3c00d91044b..1368c29521ec85338315bb0e82877dd1b8377060 100644 --- a/ylong_http_client/src/async_impl/conn/http2.rs +++ b/ylong_http_client/src/async_impl/conn/http2.rs @@ -18,11 +18,11 @@ use crate::async_impl::conn::HttpBody; use crate::async_impl::StreamData; use crate::error::{ErrorKind, HttpClientError}; use crate::util::dispatcher::http2::Http2Conn; +use crate::{AsyncRead, AsyncWrite, ReadBuf}; use std::cmp::min; use std::future::Future; use std::pin::Pin; use std::task::{Context, Poll}; -use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; use ylong_http::body::async_impl::Body; use ylong_http::error::HttpError; use ylong_http::h2; @@ -451,6 +451,7 @@ mod ut_http2 { ); } + #[cfg(feature = "tokio_base")] #[test] fn ut_http2_build_data_frame() { let mut request = build_request!( diff --git a/ylong_http_client/src/async_impl/conn/mod.rs b/ylong_http_client/src/async_impl/conn/mod.rs index 33f094a4c9c4391ba7516301427107fa40975a85..1c3146aef287e7a40ffc92cc148f1a45e1449bac 100644 --- a/ylong_http_client/src/async_impl/conn/mod.rs +++ b/ylong_http_client/src/async_impl/conn/mod.rs @@ -16,7 +16,7 @@ use crate::async_impl::HttpBody; use crate::error::HttpClientError; use crate::util::dispatcher::Conn; -use tokio::io::{AsyncRead, AsyncWrite}; +use crate::{AsyncRead, AsyncWrite}; use ylong_http::body::async_impl::Body; use crate::async_impl::client::Retryable; diff --git a/ylong_http_client/src/async_impl/connector.rs b/ylong_http_client/src/async_impl/connector.rs index 3f3031abc90353cd5ab0ec5a62a0170d25a86221..51f396c9f8aa7eed17f2cc99a55d47d0ee8ad080 100644 --- a/ylong_http_client/src/async_impl/connector.rs +++ b/ylong_http_client/src/async_impl/connector.rs @@ -14,8 +14,8 @@ */ use crate::util::ConnectorConfig; +use crate::{AsyncRead, AsyncWrite}; use core::future::Future; -use tokio::io::{AsyncRead, AsyncWrite}; use ylong_http::request::uri::Uri; /// `Connector` trait used by `async_impl::Client`. `Connector` provides @@ -56,9 +56,9 @@ impl Default for HttpConnector { #[cfg(not(feature = "__tls"))] pub(crate) mod no_tls { use crate::async_impl::Connector; + use crate::TcpStream; use core::{future::Future, pin::Pin}; use std::io::Error; - use tokio::net::TcpStream; use ylong_http::request::uri::Uri; impl Connector for super::HttpConnector { @@ -91,10 +91,9 @@ pub(crate) mod c_ssl { async_impl::{AsyncSslStream, Connector, MixStream}, ErrorKind, HttpClientError, }; + use crate::{AsyncReadExt, AsyncWriteExt, TcpStream}; use core::{future::Future, pin::Pin}; use std::io::Write; - use tokio::io::{AsyncReadExt, AsyncWriteExt}; - use tokio::net::TcpStream; use ylong_http::request::uri::{Scheme, Uri}; impl Connector for super::HttpConnector { diff --git a/ylong_http_client/src/async_impl/http_body.rs b/ylong_http_client/src/async_impl/http_body.rs index 7ce64fb2fed85b93daecf6512528f947b0cc31da..89c6cb7cc9f01cbe878a812d61220375bf27ec6a 100644 --- a/ylong_http_client/src/async_impl/http_body.rs +++ b/ylong_http_client/src/async_impl/http_body.rs @@ -16,12 +16,11 @@ use super::{Body, StreamData}; use crate::error::{ErrorKind, HttpClientError}; +use crate::{AsyncRead, ReadBuf, Sleep}; use core::pin::Pin; use core::task::{Context, Poll}; use std::future::Future; use std::io::{Cursor, Read}; -use tokio::io::{AsyncRead, ReadBuf}; -use tokio::time::Sleep; use ylong_http::body::TextBodyDecoder; use ylong_http::headers::Headers; diff --git a/ylong_http_client/src/async_impl/pool.rs b/ylong_http_client/src/async_impl/pool.rs index 9cf520a385a1660f7d716b6aeb94a021b9ba61e9..898d4002041e2f79d607539d39766703a50129c2 100644 --- a/ylong_http_client/src/async_impl/pool.rs +++ b/ylong_http_client/src/async_impl/pool.rs @@ -17,12 +17,12 @@ use crate::async_impl::Connector; use crate::error::HttpClientError; use crate::util::dispatcher::{Conn, ConnDispatcher, Dispatcher}; use crate::util::pool::{Pool, PoolKey}; +use crate::{AsyncRead, AsyncWrite}; use crate::{ErrorKind, HttpConfig, HttpVersion, Uri}; use std::error::Error; use std::future::Future; use std::mem::take; use std::sync::{Arc, Mutex}; -use tokio::io::{AsyncRead, AsyncWrite}; pub(crate) struct ConnPool { pool: Pool>, @@ -56,7 +56,7 @@ pub(crate) struct Conns { list: Arc>>>, #[cfg(feature = "http2")] - h2_occupation: Arc>, + h2_occupation: Arc>, } impl Conns { @@ -65,7 +65,7 @@ impl Conns { list: Arc::new(Mutex::new(Vec::new())), #[cfg(feature = "http2")] - h2_occupation: Arc::new(tokio::sync::Mutex::new(())), + h2_occupation: Arc::new(crate::AsyncMutex::new(())), } } } diff --git a/ylong_http_client/src/async_impl/ssl_stream/c_ssl_stream.rs b/ylong_http_client/src/async_impl/ssl_stream/c_ssl_stream.rs index 10a94bc645629e4eb5d413874fb0acb84af21598..4de830d51818edd36d0e1c0262344ba68b2df156 100644 --- a/ylong_http_client/src/async_impl/ssl_stream/c_ssl_stream.rs +++ b/ylong_http_client/src/async_impl/ssl_stream/c_ssl_stream.rs @@ -18,6 +18,7 @@ use crate::util::c_openssl::{ error::ErrorStack, ssl::{self, ShutdownResult, Ssl, SslErrorCode}, }; +use crate::{AsyncRead, AsyncWrite, ReadBuf}; use core::{ future, pin::Pin, @@ -25,7 +26,6 @@ use core::{ task::{Context, Poll}, }; use std::io::{self, Read, Write}; -use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; /// An asynchronous version of [`openssl::ssl::SslStream`]. #[derive(Debug)] diff --git a/ylong_http_client/src/async_impl/ssl_stream/mix.rs b/ylong_http_client/src/async_impl/ssl_stream/mix.rs index b6d0e36f291906b2634ca4d2869e7c4e38d02d4c..bdc3f2e30a08b2e9868efdf70b0ca0fcbf4f6d03 100644 --- a/ylong_http_client/src/async_impl/ssl_stream/mix.rs +++ b/ylong_http_client/src/async_impl/ssl_stream/mix.rs @@ -14,11 +14,11 @@ */ use crate::async_impl::AsyncSslStream; +use crate::{AsyncRead, AsyncWrite, ReadBuf}; use core::{ pin::Pin, task::{Context, Poll}, }; -use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; /// A stream which may be wrapped with TLS. pub enum MixStream { diff --git a/ylong_http_client/src/async_impl/ssl_stream/wrapper.rs b/ylong_http_client/src/async_impl/ssl_stream/wrapper.rs index bd4c6e6ad1795e9b21373e3954fca6345d58bae9..8fbedde5aa427ef06191871c5d75155e45c81cd2 100644 --- a/ylong_http_client/src/async_impl/ssl_stream/wrapper.rs +++ b/ylong_http_client/src/async_impl/ssl_stream/wrapper.rs @@ -13,13 +13,13 @@ * limitations under the License. */ +use crate::{AsyncRead, AsyncWrite, ReadBuf}; use core::{ fmt::Debug, pin::Pin, task::{Context, Poll}, }; use std::io::{self, Read, Write}; -use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; #[derive(Debug)] pub(crate) struct Wrapper { diff --git a/ylong_http_client/src/async_impl/timeout.rs b/ylong_http_client/src/async_impl/timeout.rs index 6487a18fcc852ec06490c34cf8f1a1c74fd47df8..2a62553a5a2671d930aa7ab7145d0090e4999816 100644 --- a/ylong_http_client/src/async_impl/timeout.rs +++ b/ylong_http_client/src/async_impl/timeout.rs @@ -14,11 +14,11 @@ */ use crate::async_impl::HttpBody; +use crate::Sleep; use crate::{ErrorKind, HttpClientError}; use std::future::Future; use std::pin::Pin; use std::task::{Context, Poll}; -use tokio::time::Sleep; use ylong_http::response::Response; pub(crate) struct TimeoutFuture { diff --git a/ylong_http_client/src/async_impl/uploader/builder.rs b/ylong_http_client/src/async_impl/uploader/builder.rs index a778b9d6564dc4a54f2e6a28e37ec29d00e1ad77..ffa58fbd1d36e03731f854d6bcc4a43239fd0da8 100644 --- a/ylong_http_client/src/async_impl/uploader/builder.rs +++ b/ylong_http_client/src/async_impl/uploader/builder.rs @@ -15,7 +15,7 @@ use super::{Console, UploadConfig, UploadOperator, Uploader}; use crate::async_impl::MultiPart; -use tokio::io::AsyncRead; +use crate::AsyncRead; /// A builder that can create a `Uploader`. /// @@ -110,7 +110,6 @@ impl UploaderBuilder> { /// ``` /// # use std::pin::Pin; /// # use std::task::{Context, Poll}; - /// use tokio::io::ReadBuf; /// # use ylong_http_client::async_impl::{UploaderBuilder, Uploader, UploadOperator}; /// # use ylong_http_client::{HttpClientError, Response}; /// diff --git a/ylong_http_client/src/async_impl/uploader/mod.rs b/ylong_http_client/src/async_impl/uploader/mod.rs index 410a7c1f666eac776480e4a938dba0cf297946e9..c3a7397ee2df419f85b64e67222f69d8152f8957 100644 --- a/ylong_http_client/src/async_impl/uploader/mod.rs +++ b/ylong_http_client/src/async_impl/uploader/mod.rs @@ -19,10 +19,10 @@ mod operator; pub use builder::{UploaderBuilder, WantsReader}; pub use operator::{Console, UploadOperator}; +use crate::{AsyncRead, ReadBuf}; use crate::{ErrorKind, HttpClientError}; use std::pin::Pin; use std::task::{Context, Poll}; -use tokio::io::{AsyncRead, ReadBuf}; use ylong_http::body::async_impl::Body; /// An uploader that can help you upload the request body. @@ -59,7 +59,6 @@ use ylong_http::body::async_impl::Body; /// ```no_run /// # use std::pin::Pin; /// # use std::task::{Context, Poll}; -/// # use tokio::io::ReadBuf; /// # use ylong_http_client::async_impl::{Uploader, UploadOperator}; /// # use ylong_http_client::{Response, SpeedLimit, Timeout}; /// # use ylong_http_client::HttpClientError; diff --git a/ylong_http_client/src/async_impl/uploader/operator.rs b/ylong_http_client/src/async_impl/uploader/operator.rs index 6fd22ea48053792f2e775d9c619d25c3d1a7c711..c7cbb2d2a9e22eaadea2a445317216c33c40b12d 100644 --- a/ylong_http_client/src/async_impl/uploader/operator.rs +++ b/ylong_http_client/src/async_impl/uploader/operator.rs @@ -29,7 +29,6 @@ use std::task::{Context, Poll}; /// ``` /// # use std::pin::Pin; /// # use std::task::{Context, Poll}; -/// # use tokio::io::ReadBuf; /// # use ylong_http_client::async_impl::UploadOperator; /// # use ylong_http_client::HttpClientError; /// diff --git a/ylong_http_client/src/lib.rs b/ylong_http_client/src/lib.rs index 56faf341c45242ce8339a351745273050aa693ca..1b1adfb02caf31f10072e4945163b0aba48cbadb 100644 --- a/ylong_http_client/src/lib.rs +++ b/ylong_http_client/src/lib.rs @@ -60,3 +60,29 @@ pub mod util; any(feature = "http1_1", feature = "http2"), ))] pub use util::*; + +#[cfg(feature = "tokio_base")] +pub(crate) use tokio::{ + io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf}, + net::TcpStream, + time::{sleep, timeout, Sleep}, +}; + +#[cfg(all(feature = "tokio_base", feature = "http2"))] +pub(crate) use tokio::sync::{ + mpsc::{error::TryRecvError, unbounded_channel, UnboundedReceiver, UnboundedSender}, + Mutex as AsyncMutex, MutexGuard, +}; + +#[cfg(feature = "ylong_base")] +pub(crate) use ylong_runtime::{ + io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf}, + net::TcpStream, + time::{sleep, timeout, Sleep}, +}; + +#[cfg(all(feature = "ylong_base", feature = "http2"))] +pub(crate) use ylong_runtime::sync::{ + mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}, + Mutex as AsyncMutex, MutexGuard, RecvError as TryRecvError, +}; diff --git a/ylong_http_client/src/util/dispatcher.rs b/ylong_http_client/src/util/dispatcher.rs index 59018a253f276246b1aea79f390bba71c8a80c0a..097f5d6c665204ec3a779e4596d6f6e732e7e9ea 100644 --- a/ylong_http_client/src/util/dispatcher.rs +++ b/ylong_http_client/src/util/dispatcher.rs @@ -151,6 +151,10 @@ pub(crate) mod http2 { use crate::error::HttpClientError; use crate::util::H2Config; use crate::ErrorKind; + use crate::MutexGuard; + use crate::TryRecvError; + use crate::{unbounded_channel, UnboundedReceiver, UnboundedSender}; + use crate::{AsyncMutex, AsyncRead, AsyncWrite, ReadBuf}; use std::collections::{HashMap, VecDeque}; use std::future::Future; use std::mem::take; @@ -158,10 +162,6 @@ pub(crate) mod http2 { use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use std::sync::{Arc, Mutex}; use std::task::{Context, Poll, Waker}; - use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; - use tokio::sync::mpsc::error::TryRecvError; - use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; - use tokio::sync::MutexGuard; use ylong_http::error::HttpError; use ylong_http::h2; use ylong_http::h2::Payload::Settings; @@ -215,7 +215,7 @@ pub(crate) mod http2 { // Indicates that the dispatcher is occupied. At this time, a user coroutine is already acting as the dispatcher. pub(crate) occupied: AtomicU32, pub(crate) dispatcher_invalid: AtomicBool, - pub(crate) manager: tokio::sync::Mutex>, + pub(crate) manager: AsyncMutex>, pub(crate) stream_waker: Mutex, } @@ -306,7 +306,7 @@ pub(crate) mod http2 { // 0 means io is not occupied occupied: AtomicU32::new(0), dispatcher_invalid: AtomicBool::new(false), - manager: tokio::sync::Mutex::new(manager), + manager: AsyncMutex::new(manager), stream_waker: Mutex::new(StreamWaker::new()), } } @@ -544,7 +544,7 @@ pub(crate) mod http2 { }; // For each stream to send the frame to the controller - let (tx, rx) = tokio::sync::mpsc::unbounded_channel::(); + let (tx, rx) = unbounded_channel::(); let stream_controller = Arc::new(StreamController::new(inner, rx, connection_frames)); @@ -607,7 +607,7 @@ pub(crate) mod http2 { frame: Frame, ) -> Result<(), HttpClientError> { if self.stream_info.receiver.is_none() { - let (tx, rx) = tokio::sync::mpsc::unbounded_channel::(); + let (tx, rx) = unbounded_channel::(); self.stream_info.receiver.set_receiver(rx); self.sender.send((Some((self.id, tx)), frame)).map_err(|_| { HttpClientError::new_with_cause(