diff --git a/Cargo.toml b/Cargo.toml index a3ee0d0..bb219de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,10 +39,10 @@ futures-util = { version = "0.3", default-features = false, features = [ [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -tokio-tungstenite = "0.21" +tokio-tungstenite = "0.24" tokio = { version = "1.36", default-features = false, features = ["net"] } native-tls = { version = "0.2", default-features = false, optional = true } -rustls = { version = "0.22", default-features = false, optional = true } +rustls = { version = "0.23", default-features = false, optional = true } [target.'cfg(target_arch = "wasm32")'.dependencies] diff --git a/src/error.rs b/src/error.rs index 997a15f..2f98866 100644 --- a/src/error.rs +++ b/src/error.rs @@ -145,6 +145,23 @@ pub enum CapacityError { }, } +/// Indicates the specific type/cause of a subprotocol header error. +#[derive(Error, Clone, PartialEq, Eq, Debug, Copy)] +pub enum SubProtocolError { + /// The server sent a subprotocol to a client handshake request but none was requested + #[error("Server sent a subprotocol but none was requested")] + ServerSentSubProtocolNoneRequested, + + /// The server sent an invalid subprotocol to a client handhshake request + #[error("Server sent an invalid subprotocol")] + InvalidSubProtocol, + + /// The server sent no subprotocol to a client handshake request that requested one or more + /// subprotocols + #[error("Server sent no subprotocol")] + NoSubProtocol, +} + /// Indicates the specific type/cause of a protocol error. #[derive(Error, Debug, PartialEq, Eq, Clone)] pub enum ProtocolError { @@ -169,6 +186,10 @@ pub enum ProtocolError { /// The `Sec-WebSocket-Accept` header is either not present or does not specify the correct key value. #[error("Key mismatch in \"Sec-WebSocket-Accept\" header")] SecWebSocketAcceptKeyMismatch, + /// The `Sec-WebSocket-Protocol` header was invalid + #[error("SubProtocol error: {0}")] + #[allow(clippy::enum_variant_names)] + SecWebSocketSubProtocolError(SubProtocolError), /// Garbage data encountered after client request. #[error("Junk after client request")] JunkAfterRequest, diff --git a/src/message.rs b/src/message.rs index 905d754..2f1d163 100644 --- a/src/message.rs +++ b/src/message.rs @@ -77,6 +77,7 @@ impl Message { } /// Attempt to consume the WebSocket message and convert it to a String. + #[allow(clippy::result_large_err)] pub fn into_text(self) -> Result { match self { Message::Text(string) => Ok(string), @@ -88,6 +89,7 @@ impl Message { /// Attempt to get a &str from the WebSocket message, /// this will try to convert binary data to utf8. + #[allow(clippy::result_large_err)] pub fn to_text(&self) -> Result<&str, crate::Error> { match *self { Message::Text(ref string) => Ok(string), @@ -155,7 +157,7 @@ pub struct CloseFrame<'t> { pub reason: std::borrow::Cow<'t, str>, } -impl<'t> CloseFrame<'t> { +impl CloseFrame<'_> { /// Convert into a owned string. pub fn into_owned(self) -> CloseFrame<'static> { CloseFrame { @@ -165,7 +167,7 @@ impl<'t> CloseFrame<'t> { } } -impl<'t> std::fmt::Display for CloseFrame<'t> { +impl std::fmt::Display for CloseFrame<'_> { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "{} ({})", self.reason, self.code) } diff --git a/src/native.rs b/src/native.rs index ee9883b..8a8d504 100644 --- a/src/native.rs +++ b/src/native.rs @@ -240,6 +240,9 @@ impl From for crate::error::ProtocolError { ProtocolError::InvalidCloseSequence => { crate::error::ProtocolError::InvalidCloseSequence } + ProtocolError::SecWebSocketSubProtocolError(sub_protocol_error) => { + crate::error::ProtocolError::SecWebSocketSubProtocolError(sub_protocol_error.into()) + } } } } @@ -258,6 +261,20 @@ impl From for crate::error::TlsError { } } +impl From for crate::error::SubProtocolError { + fn from(error: SubProtocolError) -> Self { + match error { + SubProtocolError::ServerSentSubProtocolNoneRequested => { + crate::error::SubProtocolError::ServerSentSubProtocolNoneRequested + } + SubProtocolError::InvalidSubProtocol => { + crate::error::SubProtocolError::InvalidSubProtocol + } + SubProtocolError::NoSubProtocol => crate::error::SubProtocolError::NoSubProtocol, + } + } +} + impl From for crate::error::Data { fn from(data: Data) -> Self { match data {