diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index cc7c885e7..72c78303f 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -23,7 +23,8 @@ name = "actix_http" path = "src/lib.rs" [features] -default = [] +default = ["invalid-ping-payload"] +invalid-ping-payload = [] # openssl ssl = ["openssl", "actix-connect/ssl"] diff --git a/actix-http/src/ws/codec.rs b/actix-http/src/ws/codec.rs index 72055f7ba..71c9ab377 100644 --- a/actix-http/src/ws/codec.rs +++ b/actix-http/src/ws/codec.rs @@ -13,8 +13,14 @@ pub enum Message { /// Binary message Binary(Bytes), /// Ping message + #[cfg(feature = "invalid-ping-payload")] + Ping(String), + #[cfg(not(feature = "invalid-ping-payload"))] Ping(Bytes), /// Pong message + #[cfg(feature = "invalid-ping-payload")] + Pong(String), + #[cfg(not(feature = "invalid-ping-payload"))] Pong(Bytes), /// Close message with optional reason Close(Option), @@ -30,8 +36,14 @@ pub enum Frame { /// Binary frame Binary(Option), /// Ping message + #[cfg(feature = "invalid-ping-payload")] + Ping(String), + #[cfg(not(feature = "invalid-ping-payload"))] Ping(Bytes), /// Pong message + #[cfg(feature = "invalid-ping-payload")] + Pong(String), + #[cfg(not(feature = "invalid-ping-payload"))] Pong(Bytes), /// Close message with optional reason Close(Option), @@ -190,6 +202,7 @@ impl Decoder for Codec { Ok(Some(Frame::Close(None))) } } + #[cfg(not(feature = "invalid-ping-payload"))] OpCode::Ping => { if let Some(pl) = payload { Ok(Some(Frame::Ping(pl.into()))) @@ -197,6 +210,7 @@ impl Decoder for Codec { Ok(Some(Frame::Ping(Bytes::new()))) } } + #[cfg(not(feature = "invalid-ping-payload"))] OpCode::Pong => { if let Some(pl) = payload { Ok(Some(Frame::Pong(pl.into()))) @@ -204,6 +218,22 @@ impl Decoder for Codec { Ok(Some(Frame::Pong(Bytes::new()))) } } + #[cfg(feature = "invalid-ping-payload")] + OpCode::Ping => { + if let Some(ref pl) = payload { + Ok(Some(Frame::Ping(String::from_utf8_lossy(pl).into()))) + } else { + Ok(Some(Frame::Ping(String::new()))) + } + } + #[cfg(feature = "invalid-ping-payload")] + OpCode::Pong => { + if let Some(ref pl) = payload { + Ok(Some(Frame::Pong(String::from_utf8_lossy(pl).into()))) + } else { + Ok(Some(Frame::Pong(String::new()))) + } + } OpCode::Binary => Ok(Some(Frame::Binary(payload))), OpCode::Text => { Ok(Some(Frame::Text(payload))) diff --git a/actix-web-actors/Cargo.toml b/actix-web-actors/Cargo.toml index 356109da5..72063193c 100644 --- a/actix-web-actors/Cargo.toml +++ b/actix-web-actors/Cargo.toml @@ -17,6 +17,10 @@ edition = "2018" name = "actix_web_actors" path = "src/lib.rs" +[features] +default = ["invalid-ping-payload"] +invalid-ping-payload = ["actix-http/invalid-ping-payload"] + [dependencies] actix = "0.8.3" actix-web = "1.0.3" diff --git a/actix-web-actors/src/ws.rs b/actix-web-actors/src/ws.rs index e0b7877f7..7946827f5 100644 --- a/actix-web-actors/src/ws.rs +++ b/actix-web-actors/src/ws.rs @@ -345,15 +345,31 @@ where /// Send ping frame #[inline] + #[cfg(not(feature = "invalid-ping-payload"))] pub fn ping>(&mut self, message: B) { self.write_raw(Message::Ping(message.into())); } + /// Send ping frame + #[inline] + #[cfg(feature = "invalid-ping-payload")] + pub fn ping>(&mut self, message: &str) { + self.write_raw(Message::Ping(message.to_string())); + } + /// Send pong frame #[inline] + #[cfg(not(feature = "invalid-ping-payload"))] pub fn pong>(&mut self, message: B) { self.write_raw(Message::Pong(message.into())); } + + /// Send pong frame + #[inline] + #[cfg(feature = "invalid-ping-payload")] + pub fn pong(&mut self, message: &str) { + self.write_raw(Message::Pong(message.to_string())); + } /// Send close frame #[inline]