From 438fb1579e8227809d2882a9238b4cc7beaeae02 Mon Sep 17 00:00:00 2001 From: hu-kai45 Date: Tue, 20 Jun 2023 16:52:19 +0800 Subject: [PATCH] =?UTF-8?q?tokio=20=E5=92=8C=20ylong=5Fruntime=20=E9=9A=94?= =?UTF-8?q?=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hu-kai45 --- ylong_http/Cargo.toml | 20 ++++++- ylong_http/src/body/chunk.rs | 4 +- ylong_http/src/body/empty.rs | 1 + ylong_http/src/body/mime/common/mix.rs | 6 +- ylong_http/src/body/mime/common/part.rs | 2 +- ylong_http/src/body/mime/encode/multi.rs | 1 + ylong_http/src/body/mime/encode/part.rs | 5 +- ylong_http/src/body/mime/mod.rs | 13 +--- ylong_http/src/body/mod.rs | 5 +- ylong_http/src/body/text.rs | 4 +- ylong_http/src/lib.rs | 6 ++ ylong_http_client/Cargo.toml | 60 ++++++++++--------- ylong_http_client/src/async_impl/client.rs | 7 ++- .../src/async_impl/conn/http1.rs | 2 +- .../src/async_impl/conn/http2.rs | 3 +- ylong_http_client/src/async_impl/conn/mod.rs | 2 +- ylong_http_client/src/async_impl/connector.rs | 7 +-- ylong_http_client/src/async_impl/http_body.rs | 3 +- ylong_http_client/src/async_impl/pool.rs | 6 +- .../src/async_impl/ssl_stream/c_ssl_stream.rs | 2 +- .../src/async_impl/ssl_stream/mix.rs | 2 +- .../src/async_impl/ssl_stream/wrapper.rs | 2 +- ylong_http_client/src/async_impl/timeout.rs | 2 +- .../src/async_impl/uploader/builder.rs | 3 +- .../src/async_impl/uploader/mod.rs | 3 +- .../src/async_impl/uploader/operator.rs | 1 - ylong_http_client/src/lib.rs | 26 ++++++++ ylong_http_client/src/util/dispatcher.rs | 16 ++--- 28 files changed, 131 insertions(+), 83 deletions(-) diff --git a/ylong_http/Cargo.toml b/ylong_http/Cargo.toml index ae15713..53438ce 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 8e229ae..dfbc47c 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 2c5694c..c6201ed 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 579c7bb..c4a5847 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 9240f45..138173f 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 817e301..9bf63b6 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 936bfde..811efde 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 2c5d0dc..f72d40c 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 c341994..1117e7e 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 b10dc56..facecba 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 45b3d8d..3351b4a 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 25efa59..121ac47 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 59b289b..01cb695 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 3ce43d1..c181416 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 49f9b76..1368c29 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 33f094a..1c3146a 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 3f3031a..51f396c 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 7ce64fb..89c6cb7 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 9cf520a..898d400 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 10a94bc..4de830d 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 b6d0e36..bdc3f2e 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 bd4c6e6..8fbedde 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 6487a18..2a62553 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 a778b9d..ffa58fb 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 410a7c1..c3a7397 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 6fd22ea..c7cbb2d 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 56faf34..1b1adfb 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 59018a2..097f5d6 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( -- Gitee