From 4c595e0431d5efae6140efb1cc710a7e1cdb6e11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BB=BA=E8=BE=9B?= Date: Thu, 29 Jun 2023 10:44:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20SslStream=20Wrapper=20?= =?UTF-8?q?=E5=86=85=E9=83=A8=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张建辛 --- ylong_http_client/examples/async_https_outside.rs | 2 +- .../src/async_impl/ssl_stream/c_ssl_stream.rs | 15 +++++++++++---- .../src/async_impl/ssl_stream/mod.rs | 5 ++--- .../src/async_impl/ssl_stream/wrapper.rs | 8 ++++++-- ylong_http_client/src/util/c_openssl/bio.rs | 2 ++ ylong_http_client/src/util/c_openssl/ffi/bio.rs | 1 - 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/ylong_http_client/examples/async_https_outside.rs b/ylong_http_client/examples/async_https_outside.rs index 38efbab..a6fc90e 100644 --- a/ylong_http_client/examples/async_https_outside.rs +++ b/ylong_http_client/examples/async_https_outside.rs @@ -36,7 +36,7 @@ fn main() { } async fn req() { - let v = include_bytes!("./test.pem"); + let v = "some certs".as_bytes(); let cert = Certificate::from_pem(v); // Creates a `async_impl::Client` let client = Client::builder() 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 4de830d..18a9d03 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 @@ -22,7 +22,7 @@ use crate::{AsyncRead, AsyncWrite, ReadBuf}; use core::{ future, pin::Pin, - slice, + ptr, slice, task::{Context, Poll}, }; use std::io::{self, Read, Write}; @@ -41,9 +41,9 @@ impl AsyncSslStream { let this = unsafe { self.get_unchecked_mut() }; // sets context, SslStream to R, reset 0. - this.0.get_mut().context = ctx as *mut _ as usize; + this.0.get_mut().context = ctx as *mut _ as *mut (); let r = f(&mut this.0); - this.0.get_mut().context = 0; + this.0.get_mut().context = ptr::null_mut(); r } @@ -61,7 +61,14 @@ where /// Like [`SslStream::new`](ssl::SslStream::new). pub(crate) fn new(ssl: Ssl, stream: S) -> Result { // This corresponds to `SSL_set_bio`. - ssl::SslStream::new_base(ssl, Wrapper { stream, context: 0 }).map(AsyncSslStream) + ssl::SslStream::new_base( + ssl, + Wrapper { + stream, + context: ptr::null_mut(), + }, + ) + .map(AsyncSslStream) } /// Like [`SslStream::connect`](ssl::SslStream::connect). diff --git a/ylong_http_client/src/async_impl/ssl_stream/mod.rs b/ylong_http_client/src/async_impl/ssl_stream/mod.rs index b9f3b91..3e73292 100644 --- a/ylong_http_client/src/async_impl/ssl_stream/mod.rs +++ b/ylong_http_client/src/async_impl/ssl_stream/mod.rs @@ -13,14 +13,13 @@ * limitations under the License. */ +#[cfg(feature = "__c_openssl")] +mod c_ssl_stream; mod mix; mod wrapper; -#[cfg(feature = "__c_openssl")] -mod c_ssl_stream; #[cfg(feature = "__c_openssl")] pub use c_ssl_stream::AsyncSslStream; - pub use mix::MixStream; pub(crate) use wrapper::{check_io_to_poll, Wrapper}; 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 8fbedde..4aff137 100644 --- a/ylong_http_client/src/async_impl/ssl_stream/wrapper.rs +++ b/ylong_http_client/src/async_impl/ssl_stream/wrapper.rs @@ -24,7 +24,7 @@ use std::io::{self, Read, Write}; #[derive(Debug)] pub(crate) struct Wrapper { pub(crate) stream: S, - pub(crate) context: usize, // + pub(crate) context: *mut (), // Context of stream. } impl Wrapper { @@ -34,7 +34,7 @@ impl Wrapper { /// Must be called with `context` set to a valid pointer to a live `Context` object, /// and the wrapper must be pinned in memory. unsafe fn inner(&mut self) -> (Pin<&mut S>, &mut Context<'_>) { - debug_assert_ne!(self.context, 0); + debug_assert!(!self.context.is_null()); let stream = Pin::new_unchecked(&mut self.stream); let context = &mut *(self.context as *mut _); (stream, context) @@ -76,6 +76,10 @@ where } } +// *mut () is not impl Send or Sync. +unsafe impl Send for Wrapper {} +unsafe impl Sync for Wrapper {} + /// Checks `io::Result`. pub(crate) fn check_io_to_poll(r: io::Result) -> Poll> { match r { diff --git a/ylong_http_client/src/util/c_openssl/bio.rs b/ylong_http_client/src/util/c_openssl/bio.rs index 428779e..a0456bb 100644 --- a/ylong_http_client/src/util/c_openssl/bio.rs +++ b/ylong_http_client/src/util/c_openssl/bio.rs @@ -81,6 +81,7 @@ const BIO_FLAGS_RWS: c_int = BIO_FLAGS_READ | BIO_FLAGS_WRITE | BIO_FLAGS_IO_SPE #[derive(Debug)] pub struct BioMethodInner(*mut BIO_METHOD); + impl BioMethodInner { fn new() -> Result { unsafe { @@ -114,6 +115,7 @@ impl Drop for BioMethodInner { #[derive(Debug)] pub struct BioMethod(BioMethodInner); + impl BioMethod { fn new() -> Result { let method = BioMethodInner::new::()?; diff --git a/ylong_http_client/src/util/c_openssl/ffi/bio.rs b/ylong_http_client/src/util/c_openssl/ffi/bio.rs index 7f54a53..b42d1b5 100644 --- a/ylong_http_client/src/util/c_openssl/ffi/bio.rs +++ b/ylong_http_client/src/util/c_openssl/ffi/bio.rs @@ -38,7 +38,6 @@ extern "C" { pub(crate) fn BIO_clear_flags(b: *mut BIO, flags: c_int); } -#[derive(Debug)] pub(crate) enum BIO_METHOD {} // for `BIO_METHOD` -- Gitee