From 675d7542e9f2fae28512260f39d7a4f86459c0a5 Mon Sep 17 00:00:00 2001 From: Dominik Nakamura Date: Tue, 12 Jan 2021 15:22:53 +0900 Subject: [PATCH] Allows several TLS components to coexist --- src/error.rs | 52 ++++++++++++++++++++------------------------------- src/stream.rs | 8 ++++---- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/src/error.rs b/src/error.rs index c7fe872..8fa8a5c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,24 +5,12 @@ use std::{borrow::Cow, error::Error as ErrorTrait, fmt, io, result, str, string} use crate::protocol::Message; use http::Response; -#[cfg(feature = "use-native-tls")] -pub mod tls { - //! TLS error wrapper module, feature-gated. - pub use native_tls::Error; -} - -#[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] -pub mod tls { - //! TLS error wrapper module, feature-gated. - pub use rustls::TLSError as Error; - pub use webpki::InvalidDNSNameError as DnsError; -} - /// Result type of all Tungstenite library calls. pub type Result = result::Result; /// Possible WebSocket errors #[derive(Debug)] +#[non_exhaustive] pub enum Error { /// WebSocket connection closed normally. This informs you of the close. /// It's not an error as such and nothing wrong happened. @@ -49,13 +37,13 @@ pub enum Error { Io(io::Error), #[cfg(feature = "use-native-tls")] /// TLS error - Tls(tls::Error), - #[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] + TlsNative(native_tls::Error), + #[cfg(feature = "use-rustls")] /// TLS error - Tls(tls::Error), - #[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] + TlsRustls(rustls::TLSError), + #[cfg(feature = "use-rustls")] /// DNS name resolution error. - Dns(tls::DnsError), + Dns(webpki::InvalidDNSNameError), /// - When reading: buffer capacity exhausted. /// - When writing: your message is bigger than the configured max message size /// (64MB by default). @@ -81,10 +69,10 @@ impl fmt::Display for Error { Error::AlreadyClosed => write!(f, "Trying to work with closed connection"), Error::Io(ref err) => write!(f, "IO error: {}", err), #[cfg(feature = "use-native-tls")] - Error::Tls(ref err) => write!(f, "TLS error: {}", err), - #[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] - Error::Tls(ref err) => write!(f, "TLS error: {}", err), - #[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] + Error::TlsNative(ref err) => write!(f, "TLS (native-tls) error: {}", err), + #[cfg(feature = "use-rustls")] + Error::TlsRustls(ref err) => write!(f, "TLS (rustls) error: {}", err), + #[cfg(feature = "use-rustls")] Error::Dns(ref err) => write!(f, "Invalid DNS name: {}", err), Error::Capacity(ref msg) => write!(f, "Space limit exceeded: {}", msg), Error::Protocol(ref msg) => write!(f, "WebSocket protocol error: {}", msg), @@ -154,22 +142,22 @@ impl From for Error { } #[cfg(feature = "use-native-tls")] -impl From for Error { - fn from(err: tls::Error) -> Self { - Error::Tls(err) +impl From for Error { + fn from(err: native_tls::Error) -> Self { + Error::TlsNative(err) } } -#[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] -impl From for Error { - fn from(err: tls::Error) -> Self { - Error::Tls(err) +#[cfg(feature = "use-rustls")] +impl From for Error { + fn from(err: rustls::TLSError) -> Self { + Error::TlsRustls(err) } } -#[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] -impl From for Error { - fn from(err: tls::DnsError) -> Self { +#[cfg(feature = "use-rustls")] +impl From for Error { + fn from(err: webpki::InvalidDNSNameError) -> Self { Error::Dns(err) } } diff --git a/src/stream.rs b/src/stream.rs index 4978b80..09c3d3a 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -10,8 +10,8 @@ use std::net::TcpStream; #[cfg(feature = "use-native-tls")] use native_tls::TlsStream; -#[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] -use rustls::StreamOwned as TlsStream; +#[cfg(feature = "use-rustls")] +use rustls::StreamOwned; /// Stream mode, either plain TCP or TLS. #[derive(Clone, Copy, Debug)] @@ -41,8 +41,8 @@ impl NoDelay for TlsStream { } } -#[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] -impl NoDelay for TlsStream { +#[cfg(feature = "use-rustls")] +impl NoDelay for StreamOwned { fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()> { self.sock.set_nodelay(nodelay) }