1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2025-04-05 01:29:36 +00:00

Make correct ping handeling a flag

This commit is contained in:
Heinz Gies 2019-09-10 17:20:56 +02:00 committed by Heinz N. Gies
parent c5dfead7f9
commit 81230a7cb1
4 changed files with 52 additions and 1 deletions

View file

@ -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"]

View file

@ -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<CloseReason>),
@ -30,8 +36,14 @@ pub enum Frame {
/// Binary frame
Binary(Option<BytesMut>),
/// 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<CloseReason>),
@ -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)))

View file

@ -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"

View file

@ -345,15 +345,31 @@ where
/// Send ping frame
#[inline]
#[cfg(not(feature = "invalid-ping-payload"))]
pub fn ping<B: Into<Bytes>>(&mut self, message: B) {
self.write_raw(Message::Ping(message.into()));
}
/// Send ping frame
#[inline]
#[cfg(feature = "invalid-ping-payload")]
pub fn ping<B: Into<Bytes>>(&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<B: Into<Bytes>>(&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]