diff --git a/src/ws/client.rs b/src/ws/client.rs index 989220474..f1b30e20b 100644 --- a/src/ws/client.rs +++ b/src/ws/client.rs @@ -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"); } diff --git a/src/ws/context.rs b/src/ws/context.rs index c10dea95a..1dab15713 100644 --- a/src/ws/context.rs +++ b/src/ws/context.rs @@ -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"); } diff --git a/src/ws/frame.rs b/src/ws/frame.rs index 006d322f6..964170a86 100644 --- a/src/ws/frame.rs +++ b/src/ws/frame.rs @@ -28,7 +28,7 @@ impl Frame { /// Create a new Close control frame. #[inline] - pub fn close(reason: Option, genmask: bool) -> Binary { + pub fn close(reason: Option, genmask: bool) -> FramedBinary { let payload = match reason { None => Vec::new(), Some(reason) => { @@ -295,7 +295,7 @@ impl Frame { /// Generate binary representation pub fn message>( data: B, code: OpCode, finished: bool, genmask: bool, - ) -> Binary { + ) -> FramedBinary { let payload = data.into(); let one: u8 = if finished { 0x80 | Into::::into(code) @@ -325,7 +325,7 @@ impl Frame { buf }; - if genmask { + let binary = if genmask { let mask = rand::random::(); 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()); } }