diff --git a/Cargo.toml b/Cargo.toml index 2971390..1198e4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,11 +14,12 @@ readme = "README.md" include = ["examples/**/*", "src/**/*", "LICENSE", "README.md", "CHANGELOG.md"] [features] -default = [] -async-std-runtime = ["async-std"] -tokio-runtime = ["tokio"] -gio-runtime = ["gio", "glib"] -async-tls = ["real-async-tls"] +default = ["handshake"] +handshake = ["tungstenite/handshake"] +async-std-runtime = ["async-std", "handshake"] +tokio-runtime = ["tokio", "handshake"] +gio-runtime = ["gio", "glib", "handshake"] +async-tls = ["real-async-tls", "handshake"] async-native-tls = ["async-std-runtime", "real-async-native-tls", "tungstenite/native-tls"] tokio-native-tls = ["tokio-runtime", "real-tokio-native-tls", "real-native-tls", "tungstenite/native-tls"] tokio-rustls-webpki-roots = ["tokio-runtime", "real-tokio-rustls", "webpki-roots", "tungstenite/__rustls-tls"] @@ -36,7 +37,7 @@ futures-io = { version = "0.3", default-features = false, features = ["std"] } pin-project-lite = "0.2" [dependencies.tungstenite] -version = "0.17.0" +version = "0.18" default-features = false [dependencies.async-std] diff --git a/src/handshake.rs b/src/handshake.rs index b4489f4..c9c22ae 100644 --- a/src/handshake.rs +++ b/src/handshake.rs @@ -1,4 +1,6 @@ -use crate::compat::{AllowStd, SetWaker}; +use crate::compat::AllowStd; +#[cfg(feature = "handshake")] +use crate::compat::SetWaker; use crate::WebSocketStream; use futures_io::{AsyncRead, AsyncWrite}; #[allow(unused_imports)] @@ -7,10 +9,15 @@ use std::future::Future; use std::io::{Read, Write}; use std::pin::Pin; use std::task::{Context, Poll}; -use tungstenite::handshake::client::Response; -use tungstenite::handshake::server::Callback; -use tungstenite::handshake::{HandshakeError as Error, HandshakeRole, MidHandshake as WsHandshake}; -use tungstenite::{ClientHandshake, ServerHandshake, WebSocket}; +use tungstenite::WebSocket; +#[cfg(feature = "handshake")] +use tungstenite::{ + handshake::{ + client::Response, server::Callback, HandshakeError as Error, HandshakeRole, + MidHandshake as WsHandshake, + }, + ClientHandshake, ServerHandshake, +}; pub(crate) async fn without_handshake(stream: S, f: F) -> WebSocketStream where @@ -52,19 +59,24 @@ where } } +#[cfg(feature = "handshake")] struct MidHandshake(Option>); +#[cfg(feature = "handshake")] enum StartedHandshake { Done(Role::FinalResult), Mid(WsHandshake), } +#[cfg(feature = "handshake")] struct StartedHandshakeFuture(Option>); +#[cfg(feature = "handshake")] struct StartedHandshakeFutureInner { f: F, stream: S, } +#[cfg(feature = "handshake")] async fn handshake(stream: S, f: F) -> Result> where Role: HandshakeRole + Unpin, @@ -83,6 +95,7 @@ where } } +#[cfg(feature = "handshake")] pub(crate) async fn client_handshake( stream: S, f: F, @@ -101,6 +114,7 @@ where Ok((WebSocketStream::new(s), r)) } +#[cfg(feature = "handshake")] pub(crate) async fn server_handshake( stream: S, f: F, @@ -119,6 +133,7 @@ where Ok(WebSocketStream::new(s)) } +#[cfg(feature = "handshake")] impl Future for StartedHandshakeFuture where Role: HandshakeRole, @@ -143,6 +158,7 @@ where } } +#[cfg(feature = "handshake")] impl Future for MidHandshake where Role: HandshakeRole + Unpin, diff --git a/src/lib.rs b/src/lib.rs index 73458db..f984019 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,14 +69,17 @@ use log::*; use std::pin::Pin; use std::task::{Context, Poll}; +#[cfg(feature = "handshake")] use tungstenite::{ client::IntoClientRequest, - error::Error as WsError, handshake::{ client::{ClientHandshake, Response}, server::{Callback, NoCallback}, HandshakeError, }, +}; +use tungstenite::{ + error::Error as WsError, protocol::{Message, Role, WebSocket, WebSocketConfig}, }; @@ -103,6 +106,7 @@ use tungstenite::protocol::CloseFrame; /// /// This is typically used for clients who have already established, for /// example, a TCP connection to the remote server. +#[cfg(feature = "handshake")] pub async fn client_async<'a, R, S>( request: R, stream: S, @@ -116,6 +120,7 @@ where /// The same as `client_async()` but the one can specify a websocket configuration. /// Please refer to `client_async()` for more details. +#[cfg(feature = "handshake")] pub async fn client_async_with_config<'a, R, S>( request: R, stream: S, @@ -150,6 +155,7 @@ where /// This is typically used after a socket has been accepted from a /// `TcpListener`. That socket is then passed to this function to perform /// the server half of the accepting a client's websocket connection. +#[cfg(feature = "handshake")] pub async fn accept_async(stream: S) -> Result, WsError> where S: AsyncRead + AsyncWrite + Unpin, @@ -159,6 +165,7 @@ where /// The same as `accept_async()` but the one can specify a websocket configuration. /// Please refer to `accept_async()` for more details. +#[cfg(feature = "handshake")] pub async fn accept_async_with_config( stream: S, config: Option, @@ -174,6 +181,7 @@ where /// This function does the same as `accept_async()` but accepts an extra callback /// for header processing. The callback receives headers of the incoming /// requests and is able to add extra headers to the reply. +#[cfg(feature = "handshake")] pub async fn accept_hdr_async(stream: S, callback: C) -> Result, WsError> where S: AsyncRead + AsyncWrite + Unpin, @@ -184,6 +192,7 @@ where /// The same as `accept_hdr_async()` but the one can specify a websocket configuration. /// Please refer to `accept_hdr_async()` for more details. +#[cfg(feature = "handshake")] pub async fn accept_hdr_async_with_config( stream: S, callback: C,