mirror of https://github.com/fafhrd91/actix-web
ws/frame: Introduce `FramedBinary` type
This type is introduced to avoid confusion between the `.binary()` and `.write_raw()` methods on WebSocket contexts
This commit is contained in:
parent
f53abed584
commit
6fc084a07c
|
@ -27,7 +27,7 @@ use client::{
|
|||
Pipeline, SendRequest, SendRequestError,
|
||||
};
|
||||
|
||||
use super::frame::Frame;
|
||||
use super::frame::{Frame, FramedBinary};
|
||||
use super::proto::{CloseReason, OpCode};
|
||||
use super::{Message, ProtocolError, WsWriter};
|
||||
|
||||
|
@ -529,10 +529,10 @@ pub struct ClientWriter {
|
|||
impl ClientWriter {
|
||||
/// Write payload
|
||||
#[inline]
|
||||
fn write(&mut self, mut data: Binary) {
|
||||
fn write(&mut self, mut data: FramedBinary) {
|
||||
let inner = self.inner.borrow_mut();
|
||||
if !inner.closed {
|
||||
let _ = inner.tx.unbounded_send(data.take());
|
||||
let _ = inner.tx.unbounded_send(data.0.take());
|
||||
} else {
|
||||
warn!("Trying to write to disconnected response");
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ use context::{ActorHttpContext, Drain, Frame as ContextFrame};
|
|||
use error::{Error, ErrorInternalServerError, PayloadError};
|
||||
use httprequest::HttpRequest;
|
||||
|
||||
use ws::frame::Frame;
|
||||
use ws::frame::{Frame, FramedBinary};
|
||||
use ws::proto::{CloseReason, OpCode};
|
||||
use ws::{Message, ProtocolError, WsStream, WsWriter};
|
||||
|
||||
|
@ -138,13 +138,13 @@ where
|
|||
/// data you should prefer the `text()` or `binary()` convenience functions
|
||||
/// that handle the framing for you.
|
||||
#[inline]
|
||||
pub fn write_raw(&mut self, data: Binary) {
|
||||
pub fn write_raw(&mut self, data: FramedBinary) {
|
||||
if !self.disconnected {
|
||||
if self.stream.is_none() {
|
||||
self.stream = Some(SmallVec::new());
|
||||
}
|
||||
let stream = self.stream.as_mut().unwrap();
|
||||
stream.push(ContextFrame::Chunk(Some(data)));
|
||||
stream.push(ContextFrame::Chunk(Some(data.0)));
|
||||
} else {
|
||||
warn!("Trying to write to disconnected response");
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ impl Frame {
|
|||
|
||||
/// Create a new Close control frame.
|
||||
#[inline]
|
||||
pub fn close(reason: Option<CloseReason>, genmask: bool) -> Binary {
|
||||
pub fn close(reason: Option<CloseReason>, genmask: bool) -> FramedBinary {
|
||||
let payload = match reason {
|
||||
None => Vec::new(),
|
||||
Some(reason) => {
|
||||
|
@ -295,7 +295,7 @@ impl Frame {
|
|||
/// Generate binary representation
|
||||
pub fn message<B: Into<Binary>>(
|
||||
data: B, code: OpCode, finished: bool, genmask: bool,
|
||||
) -> Binary {
|
||||
) -> FramedBinary {
|
||||
let payload = data.into();
|
||||
let one: u8 = if finished {
|
||||
0x80 | Into::<u8>::into(code)
|
||||
|
@ -325,7 +325,7 @@ impl Frame {
|
|||
buf
|
||||
};
|
||||
|
||||
if genmask {
|
||||
let binary = if genmask {
|
||||
let mask = rand::random::<u32>();
|
||||
buf.put_u32_le(mask);
|
||||
buf.extend_from_slice(payload.as_ref());
|
||||
|
@ -335,7 +335,9 @@ impl Frame {
|
|||
} else {
|
||||
buf.put_slice(payload.as_ref());
|
||||
buf.into()
|
||||
}
|
||||
};
|
||||
|
||||
FramedBinary(binary)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -372,6 +374,10 @@ impl fmt::Display for Frame {
|
|||
}
|
||||
}
|
||||
|
||||
/// A `Binary` representing a `WebSocket` message with framing.
|
||||
#[derive(Debug)]
|
||||
pub struct FramedBinary(pub Binary);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -502,7 +508,7 @@ mod tests {
|
|||
|
||||
let mut v = vec![137u8, 4u8];
|
||||
v.extend(b"data");
|
||||
assert_eq!(frame, v.into());
|
||||
assert_eq!(frame.0, v.into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -511,7 +517,7 @@ mod tests {
|
|||
|
||||
let mut v = vec![138u8, 4u8];
|
||||
v.extend(b"data");
|
||||
assert_eq!(frame, v.into());
|
||||
assert_eq!(frame.0, v.into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -521,12 +527,12 @@ mod tests {
|
|||
|
||||
let mut v = vec![136u8, 6u8, 3u8, 232u8];
|
||||
v.extend(b"data");
|
||||
assert_eq!(frame, v.into());
|
||||
assert_eq!(frame.0, v.into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_close_frame() {
|
||||
let frame = Frame::close(None, false);
|
||||
assert_eq!(frame, vec![0x88, 0x00].into());
|
||||
assert_eq!(frame.0, vec![0x88, 0x00].into());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue