From 27c65f3d029346c9660a10bdca6ff3fba575b989 Mon Sep 17 00:00:00 2001 From: csh <458761603@qq.com> Date: Thu, 21 Dec 2023 18:10:10 +0800 Subject: [PATCH] Support build rustls into wasi Signed-off-by: csh <458761603@qq.com> --- Cargo.toml | 14 +++++++++++--- src/conn/mod.rs | 10 ++++++---- src/conn/pool/mod.rs | 15 ++++++++++++--- src/io/mod.rs | 3 +-- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 421b8585..ce8e5a67 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ keywords = ["mysql", "database", "asynchronous", "async", "wasi"] license = "MIT/Apache-2.0" name = "mysql_async_wasi" readme = "README.md" -repository = "https://github.com/blackbeam/mysql_async" +repository = "https://github.com/WasmEdge/mysql_async_wasi" version = "0.33.0" exclude = ["test/*"] edition = "2018" @@ -58,10 +58,16 @@ wasmedge_rustls_api = { version = "0.1.0", optional = true, features = [ "tokio_async", ] } -[dependencies.tokio-rustls] +[target.'cfg(not(target_os="wasi"))'.dependencies.tokio-rustls] version = "0.24.0" optional = true +[target.'cfg(target_os="wasi")'.dependencies.tokio-rustls-wasi] +git = "https://github.com/second-state/tokio-rustls.git" +branch = "v0.24.1" +optional = true + + [target.'cfg(not(target_os="wasi"))'.dependencies.tokio-native-tls] version = "0.3.0" optional = true @@ -106,7 +112,7 @@ tokio_wasi = { version = "1", features = [ wasmedge_wasi_socket = "0.5" [features] -default = ["flate2/zlib", "common", "derive", "native-tls-tls", "binlog"] +default = ["flate2/zlib", "common", "derive", "binlog"] default-rustls = [ "flate2/rust_backend", "common", @@ -121,10 +127,12 @@ common = [ "mysql_common/frunk", ] minimal = ["flate2/zlib"] +wasmedge-tls = ["wasmedge_rustls_api"] native-tls-tls = ["native-tls", "tokio-native-tls"] rustls-tls = [ "rustls", "tokio-rustls", + "tokio-rustls-wasi", "webpki", "webpki-roots", "rustls-pemfile", diff --git a/src/conn/mod.rs b/src/conn/mod.rs index ec961eef..33c2c057 100644 --- a/src/conn/mod.rs +++ b/src/conn/mod.rs @@ -16,7 +16,7 @@ use mysql_common::{ packets::{ AuthPlugin, AuthSwitchRequest, CommonOkPacket, ErrPacket, HandshakePacket, HandshakeResponse, OkPacket, OkPacketDeserializer, OldAuthSwitchRequest, OldEofPacket, - ResultSetTerminator, SslRequest, + ResultSetTerminator, }, proto::MySerialize, row::Row, @@ -528,7 +528,7 @@ impl Conn { Ok(()) } - #[cfg(any(not(target_os = "wasi"), feature = "wasmedge-tls"))] + async fn switch_to_ssl_if_needed(&mut self) -> Result<()> { if self .inner @@ -929,7 +929,6 @@ impl Conn { conn.inner.stream = Some(stream); conn.setup_stream()?; conn.handle_handshake().await?; - #[cfg(any(not(target_os = "wasi"), feature = "wasmedge-tls"))] conn.switch_to_ssl_if_needed().await?; conn.do_handshake_response().await?; conn.continue_auth().await?; @@ -1452,7 +1451,10 @@ mod test { #[test] fn should_not_panic_if_dropped_without_tokio_runtime() { let fut = Conn::new(get_opts()); - let runtime = tokio::runtime::Runtime::new().unwrap(); + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); runtime.block_on(async { fut.await.unwrap(); }); diff --git a/src/conn/pool/mod.rs b/src/conn/pool/mod.rs index df3ac251..02b49fe9 100644 --- a/src/conn/pool/mod.rs +++ b/src/conn/pool/mod.rs @@ -485,7 +485,10 @@ mod test { } } - let runtime = tokio::runtime::Runtime::new().unwrap(); + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); let database = Database { pool: Pool::new(get_opts()), }; @@ -854,7 +857,10 @@ mod test { fn should_not_panic_on_unclean_shutdown() { // run more than once to trigger different drop orders for _ in 0..10 { - let rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); let (tx, rx) = tokio::sync::oneshot::channel(); rt.block_on(async move { let pool = Pool::new(get_opts()); @@ -874,7 +880,10 @@ mod test { fn should_perform_clean_shutdown() { // run more than once to trigger different drop orders for _ in 0..10 { - let rt = tokio::runtime::Runtime::new().unwrap(); + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); let (tx, rx) = tokio::sync::oneshot::channel(); let jh = rt.spawn(async move { let pool = Pool::new(get_opts()); diff --git a/src/io/mod.rs b/src/io/mod.rs index da11114f..29f099e3 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -205,7 +205,7 @@ impl Endpoint { not(feature = "rustls"), not(feature = "wasmedge-tls") ))] - pub async fn _make_secure( + pub async fn make_secure( &mut self, _domain: String, _ssl_opts: crate::SslOpts, @@ -435,7 +435,6 @@ impl Stream { pub(crate) fn set_tcp_nodelay(&self, val: bool) -> io::Result<()> { self.codec.as_ref().unwrap().get_ref().set_tcp_nodelay(val) } - #[cfg(any(not(target_os = "wasi"), feature = "wasmedge-tls"))] pub(crate) async fn make_secure( &mut self, domain: String,