Make handshake dependencies optional.

pull/114/head
Icelk 2 years ago committed by Sebastian Dröge
parent 952681ed06
commit 1dd92fc13a
  1. 13
      Cargo.toml
  2. 26
      src/handshake.rs
  3. 11
      src/lib.rs

@ -14,11 +14,12 @@ readme = "README.md"
include = ["examples/**/*", "src/**/*", "LICENSE", "README.md", "CHANGELOG.md"] include = ["examples/**/*", "src/**/*", "LICENSE", "README.md", "CHANGELOG.md"]
[features] [features]
default = [] default = ["handshake"]
async-std-runtime = ["async-std"] handshake = ["tungstenite/handshake"]
tokio-runtime = ["tokio"] async-std-runtime = ["async-std", "handshake"]
gio-runtime = ["gio", "glib"] tokio-runtime = ["tokio", "handshake"]
async-tls = ["real-async-tls"] gio-runtime = ["gio", "glib", "handshake"]
async-tls = ["real-async-tls", "handshake"]
async-native-tls = ["async-std-runtime", "real-async-native-tls", "tungstenite/native-tls"] 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-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"] 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" pin-project-lite = "0.2"
[dependencies.tungstenite] [dependencies.tungstenite]
version = "0.17.0" version = "0.18"
default-features = false default-features = false
[dependencies.async-std] [dependencies.async-std]

@ -1,4 +1,6 @@
use crate::compat::{AllowStd, SetWaker}; use crate::compat::AllowStd;
#[cfg(feature = "handshake")]
use crate::compat::SetWaker;
use crate::WebSocketStream; use crate::WebSocketStream;
use futures_io::{AsyncRead, AsyncWrite}; use futures_io::{AsyncRead, AsyncWrite};
#[allow(unused_imports)] #[allow(unused_imports)]
@ -7,10 +9,15 @@ use std::future::Future;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::pin::Pin; use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use tungstenite::handshake::client::Response; use tungstenite::WebSocket;
use tungstenite::handshake::server::Callback; #[cfg(feature = "handshake")]
use tungstenite::handshake::{HandshakeError as Error, HandshakeRole, MidHandshake as WsHandshake}; use tungstenite::{
use tungstenite::{ClientHandshake, ServerHandshake, WebSocket}; handshake::{
client::Response, server::Callback, HandshakeError as Error, HandshakeRole,
MidHandshake as WsHandshake,
},
ClientHandshake, ServerHandshake,
};
pub(crate) async fn without_handshake<F, S>(stream: S, f: F) -> WebSocketStream<S> pub(crate) async fn without_handshake<F, S>(stream: S, f: F) -> WebSocketStream<S>
where where
@ -52,19 +59,24 @@ where
} }
} }
#[cfg(feature = "handshake")]
struct MidHandshake<Role: HandshakeRole>(Option<WsHandshake<Role>>); struct MidHandshake<Role: HandshakeRole>(Option<WsHandshake<Role>>);
#[cfg(feature = "handshake")]
enum StartedHandshake<Role: HandshakeRole> { enum StartedHandshake<Role: HandshakeRole> {
Done(Role::FinalResult), Done(Role::FinalResult),
Mid(WsHandshake<Role>), Mid(WsHandshake<Role>),
} }
#[cfg(feature = "handshake")]
struct StartedHandshakeFuture<F, S>(Option<StartedHandshakeFutureInner<F, S>>); struct StartedHandshakeFuture<F, S>(Option<StartedHandshakeFutureInner<F, S>>);
#[cfg(feature = "handshake")]
struct StartedHandshakeFutureInner<F, S> { struct StartedHandshakeFutureInner<F, S> {
f: F, f: F,
stream: S, stream: S,
} }
#[cfg(feature = "handshake")]
async fn handshake<Role, F, S>(stream: S, f: F) -> Result<Role::FinalResult, Error<Role>> async fn handshake<Role, F, S>(stream: S, f: F) -> Result<Role::FinalResult, Error<Role>>
where where
Role: HandshakeRole + Unpin, Role: HandshakeRole + Unpin,
@ -83,6 +95,7 @@ where
} }
} }
#[cfg(feature = "handshake")]
pub(crate) async fn client_handshake<F, S>( pub(crate) async fn client_handshake<F, S>(
stream: S, stream: S,
f: F, f: F,
@ -101,6 +114,7 @@ where
Ok((WebSocketStream::new(s), r)) Ok((WebSocketStream::new(s), r))
} }
#[cfg(feature = "handshake")]
pub(crate) async fn server_handshake<C, F, S>( pub(crate) async fn server_handshake<C, F, S>(
stream: S, stream: S,
f: F, f: F,
@ -119,6 +133,7 @@ where
Ok(WebSocketStream::new(s)) Ok(WebSocketStream::new(s))
} }
#[cfg(feature = "handshake")]
impl<Role, F, S> Future for StartedHandshakeFuture<F, S> impl<Role, F, S> Future for StartedHandshakeFuture<F, S>
where where
Role: HandshakeRole, Role: HandshakeRole,
@ -143,6 +158,7 @@ where
} }
} }
#[cfg(feature = "handshake")]
impl<Role> Future for MidHandshake<Role> impl<Role> Future for MidHandshake<Role>
where where
Role: HandshakeRole + Unpin, Role: HandshakeRole + Unpin,

@ -69,14 +69,17 @@ use log::*;
use std::pin::Pin; use std::pin::Pin;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
#[cfg(feature = "handshake")]
use tungstenite::{ use tungstenite::{
client::IntoClientRequest, client::IntoClientRequest,
error::Error as WsError,
handshake::{ handshake::{
client::{ClientHandshake, Response}, client::{ClientHandshake, Response},
server::{Callback, NoCallback}, server::{Callback, NoCallback},
HandshakeError, HandshakeError,
}, },
};
use tungstenite::{
error::Error as WsError,
protocol::{Message, Role, WebSocket, WebSocketConfig}, protocol::{Message, Role, WebSocket, WebSocketConfig},
}; };
@ -103,6 +106,7 @@ use tungstenite::protocol::CloseFrame;
/// ///
/// This is typically used for clients who have already established, for /// This is typically used for clients who have already established, for
/// example, a TCP connection to the remote server. /// example, a TCP connection to the remote server.
#[cfg(feature = "handshake")]
pub async fn client_async<'a, R, S>( pub async fn client_async<'a, R, S>(
request: R, request: R,
stream: S, stream: S,
@ -116,6 +120,7 @@ where
/// The same as `client_async()` but the one can specify a websocket configuration. /// The same as `client_async()` but the one can specify a websocket configuration.
/// Please refer to `client_async()` for more details. /// Please refer to `client_async()` for more details.
#[cfg(feature = "handshake")]
pub async fn client_async_with_config<'a, R, S>( pub async fn client_async_with_config<'a, R, S>(
request: R, request: R,
stream: S, stream: S,
@ -150,6 +155,7 @@ where
/// This is typically used after a socket has been accepted from a /// This is typically used after a socket has been accepted from a
/// `TcpListener`. That socket is then passed to this function to perform /// `TcpListener`. That socket is then passed to this function to perform
/// the server half of the accepting a client's websocket connection. /// the server half of the accepting a client's websocket connection.
#[cfg(feature = "handshake")]
pub async fn accept_async<S>(stream: S) -> Result<WebSocketStream<S>, WsError> pub async fn accept_async<S>(stream: S) -> Result<WebSocketStream<S>, WsError>
where where
S: AsyncRead + AsyncWrite + Unpin, S: AsyncRead + AsyncWrite + Unpin,
@ -159,6 +165,7 @@ where
/// The same as `accept_async()` but the one can specify a websocket configuration. /// The same as `accept_async()` but the one can specify a websocket configuration.
/// Please refer to `accept_async()` for more details. /// Please refer to `accept_async()` for more details.
#[cfg(feature = "handshake")]
pub async fn accept_async_with_config<S>( pub async fn accept_async_with_config<S>(
stream: S, stream: S,
config: Option<WebSocketConfig>, config: Option<WebSocketConfig>,
@ -174,6 +181,7 @@ where
/// This function does the same as `accept_async()` but accepts an extra callback /// This function does the same as `accept_async()` but accepts an extra callback
/// for header processing. The callback receives headers of the incoming /// for header processing. The callback receives headers of the incoming
/// requests and is able to add extra headers to the reply. /// requests and is able to add extra headers to the reply.
#[cfg(feature = "handshake")]
pub async fn accept_hdr_async<S, C>(stream: S, callback: C) -> Result<WebSocketStream<S>, WsError> pub async fn accept_hdr_async<S, C>(stream: S, callback: C) -> Result<WebSocketStream<S>, WsError>
where where
S: AsyncRead + AsyncWrite + Unpin, S: AsyncRead + AsyncWrite + Unpin,
@ -184,6 +192,7 @@ where
/// The same as `accept_hdr_async()` but the one can specify a websocket configuration. /// The same as `accept_hdr_async()` but the one can specify a websocket configuration.
/// Please refer to `accept_hdr_async()` for more details. /// Please refer to `accept_hdr_async()` for more details.
#[cfg(feature = "handshake")]
pub async fn accept_hdr_async_with_config<S, C>( pub async fn accept_hdr_async_with_config<S, C>(
stream: S, stream: S,
callback: C, callback: C,

Loading…
Cancel
Save