From 2a1d46bf0fd809e18056291fff4b39787ddbfe78 Mon Sep 17 00:00:00 2001 From: Tanner Rogalsky Date: Thu, 12 Dec 2024 09:55:44 -0500 Subject: [PATCH 1/2] Bump tokio-tungenstenite to 0.24 --- Cargo.toml | 4 ++-- src/error.rs | 20 ++++++++++++++++++++ src/native.rs | 17 +++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) 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..a6fabcc 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,9 @@ 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}")] + SecWebSocketSubProtocolError(SubProtocolError), /// Garbage data encountered after client request. #[error("Junk after client request")] JunkAfterRequest, 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 { From bcad3b0d4b5fc997bc8f77d95dd944cc0789ffea Mon Sep 17 00:00:00 2001 From: Tanner Rogalsky Date: Thu, 12 Dec 2024 10:13:21 -0500 Subject: [PATCH 2/2] Fix clippy warnings --- src/error.rs | 1 + src/message.rs | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/error.rs b/src/error.rs index a6fabcc..2f98866 100644 --- a/src/error.rs +++ b/src/error.rs @@ -188,6 +188,7 @@ pub enum ProtocolError { 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")] 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) }