Allows several TLS components to coexist

pull/166/head
Dominik Nakamura 4 years ago
parent de808a3a01
commit 675d7542e9
No known key found for this signature in database
GPG Key ID: E4C6A749B2491910
  1. 52
      src/error.rs
  2. 8
      src/stream.rs

@ -5,24 +5,12 @@ use std::{borrow::Cow, error::Error as ErrorTrait, fmt, io, result, str, string}
use crate::protocol::Message; use crate::protocol::Message;
use http::Response; 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. /// Result type of all Tungstenite library calls.
pub type Result<T> = result::Result<T, Error>; pub type Result<T> = result::Result<T, Error>;
/// Possible WebSocket errors /// Possible WebSocket errors
#[derive(Debug)] #[derive(Debug)]
#[non_exhaustive]
pub enum Error { pub enum Error {
/// WebSocket connection closed normally. This informs you of the close. /// WebSocket connection closed normally. This informs you of the close.
/// It's not an error as such and nothing wrong happened. /// It's not an error as such and nothing wrong happened.
@ -49,13 +37,13 @@ pub enum Error {
Io(io::Error), Io(io::Error),
#[cfg(feature = "use-native-tls")] #[cfg(feature = "use-native-tls")]
/// TLS error /// TLS error
Tls(tls::Error), TlsNative(native_tls::Error),
#[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] #[cfg(feature = "use-rustls")]
/// TLS error /// TLS error
Tls(tls::Error), TlsRustls(rustls::TLSError),
#[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] #[cfg(feature = "use-rustls")]
/// DNS name resolution error. /// DNS name resolution error.
Dns(tls::DnsError), Dns(webpki::InvalidDNSNameError),
/// - When reading: buffer capacity exhausted. /// - When reading: buffer capacity exhausted.
/// - When writing: your message is bigger than the configured max message size /// - When writing: your message is bigger than the configured max message size
/// (64MB by default). /// (64MB by default).
@ -81,10 +69,10 @@ impl fmt::Display for Error {
Error::AlreadyClosed => write!(f, "Trying to work with closed connection"), Error::AlreadyClosed => write!(f, "Trying to work with closed connection"),
Error::Io(ref err) => write!(f, "IO error: {}", err), Error::Io(ref err) => write!(f, "IO error: {}", err),
#[cfg(feature = "use-native-tls")] #[cfg(feature = "use-native-tls")]
Error::Tls(ref err) => write!(f, "TLS error: {}", err), Error::TlsNative(ref err) => write!(f, "TLS (native-tls) error: {}", err),
#[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] #[cfg(feature = "use-rustls")]
Error::Tls(ref err) => write!(f, "TLS error: {}", err), Error::TlsRustls(ref err) => write!(f, "TLS (rustls) error: {}", err),
#[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] #[cfg(feature = "use-rustls")]
Error::Dns(ref err) => write!(f, "Invalid DNS name: {}", err), Error::Dns(ref err) => write!(f, "Invalid DNS name: {}", err),
Error::Capacity(ref msg) => write!(f, "Space limit exceeded: {}", msg), Error::Capacity(ref msg) => write!(f, "Space limit exceeded: {}", msg),
Error::Protocol(ref msg) => write!(f, "WebSocket protocol error: {}", msg), Error::Protocol(ref msg) => write!(f, "WebSocket protocol error: {}", msg),
@ -154,22 +142,22 @@ impl From<http::Error> for Error {
} }
#[cfg(feature = "use-native-tls")] #[cfg(feature = "use-native-tls")]
impl From<tls::Error> for Error { impl From<native_tls::Error> for Error {
fn from(err: tls::Error) -> Self { fn from(err: native_tls::Error) -> Self {
Error::Tls(err) Error::TlsNative(err)
} }
} }
#[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] #[cfg(feature = "use-rustls")]
impl From<tls::Error> for Error { impl From<rustls::TLSError> for Error {
fn from(err: tls::Error) -> Self { fn from(err: rustls::TLSError) -> Self {
Error::Tls(err) Error::TlsRustls(err)
} }
} }
#[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] #[cfg(feature = "use-rustls")]
impl From<tls::DnsError> for Error { impl From<webpki::InvalidDNSNameError> for Error {
fn from(err: tls::DnsError) -> Self { fn from(err: webpki::InvalidDNSNameError) -> Self {
Error::Dns(err) Error::Dns(err)
} }
} }

@ -10,8 +10,8 @@ use std::net::TcpStream;
#[cfg(feature = "use-native-tls")] #[cfg(feature = "use-native-tls")]
use native_tls::TlsStream; use native_tls::TlsStream;
#[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] #[cfg(feature = "use-rustls")]
use rustls::StreamOwned as TlsStream; use rustls::StreamOwned;
/// Stream mode, either plain TCP or TLS. /// Stream mode, either plain TCP or TLS.
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
@ -41,8 +41,8 @@ impl<S: Read + Write + NoDelay> NoDelay for TlsStream<S> {
} }
} }
#[cfg(all(feature = "use-rustls", not(feature = "use-native-tls")))] #[cfg(feature = "use-rustls")]
impl<S: rustls::Session, T: Read + Write + NoDelay> NoDelay for TlsStream<S, T> { impl<S: rustls::Session, T: Read + Write + NoDelay> NoDelay for StreamOwned<S, T> {
fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()> { fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()> {
self.sock.set_nodelay(nodelay) self.sock.set_nodelay(nodelay)
} }

Loading…
Cancel
Save