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:
Tobias Bieniek 2018-07-19 08:51:54 +02:00
parent f53abed584
commit 6fc084a07c
3 changed files with 20 additions and 14 deletions

View File

@ -27,7 +27,7 @@ use client::{
Pipeline, SendRequest, SendRequestError, Pipeline, SendRequest, SendRequestError,
}; };
use super::frame::Frame; use super::frame::{Frame, FramedBinary};
use super::proto::{CloseReason, OpCode}; use super::proto::{CloseReason, OpCode};
use super::{Message, ProtocolError, WsWriter}; use super::{Message, ProtocolError, WsWriter};
@ -529,10 +529,10 @@ pub struct ClientWriter {
impl ClientWriter { impl ClientWriter {
/// Write payload /// Write payload
#[inline] #[inline]
fn write(&mut self, mut data: Binary) { fn write(&mut self, mut data: FramedBinary) {
let inner = self.inner.borrow_mut(); let inner = self.inner.borrow_mut();
if !inner.closed { if !inner.closed {
let _ = inner.tx.unbounded_send(data.take()); let _ = inner.tx.unbounded_send(data.0.take());
} else { } else {
warn!("Trying to write to disconnected response"); warn!("Trying to write to disconnected response");
} }

View File

@ -20,7 +20,7 @@ use context::{ActorHttpContext, Drain, Frame as ContextFrame};
use error::{Error, ErrorInternalServerError, PayloadError}; use error::{Error, ErrorInternalServerError, PayloadError};
use httprequest::HttpRequest; use httprequest::HttpRequest;
use ws::frame::Frame; use ws::frame::{Frame, FramedBinary};
use ws::proto::{CloseReason, OpCode}; use ws::proto::{CloseReason, OpCode};
use ws::{Message, ProtocolError, WsStream, WsWriter}; use ws::{Message, ProtocolError, WsStream, WsWriter};
@ -138,13 +138,13 @@ where
/// data you should prefer the `text()` or `binary()` convenience functions /// data you should prefer the `text()` or `binary()` convenience functions
/// that handle the framing for you. /// that handle the framing for you.
#[inline] #[inline]
pub fn write_raw(&mut self, data: Binary) { pub fn write_raw(&mut self, data: FramedBinary) {
if !self.disconnected { if !self.disconnected {
if self.stream.is_none() { if self.stream.is_none() {
self.stream = Some(SmallVec::new()); self.stream = Some(SmallVec::new());
} }
let stream = self.stream.as_mut().unwrap(); let stream = self.stream.as_mut().unwrap();
stream.push(ContextFrame::Chunk(Some(data))); stream.push(ContextFrame::Chunk(Some(data.0)));
} else { } else {
warn!("Trying to write to disconnected response"); warn!("Trying to write to disconnected response");
} }

View File

@ -28,7 +28,7 @@ impl Frame {
/// Create a new Close control frame. /// Create a new Close control frame.
#[inline] #[inline]
pub fn close(reason: Option<CloseReason>, genmask: bool) -> Binary { pub fn close(reason: Option<CloseReason>, genmask: bool) -> FramedBinary {
let payload = match reason { let payload = match reason {
None => Vec::new(), None => Vec::new(),
Some(reason) => { Some(reason) => {
@ -295,7 +295,7 @@ impl Frame {
/// Generate binary representation /// Generate binary representation
pub fn message<B: Into<Binary>>( pub fn message<B: Into<Binary>>(
data: B, code: OpCode, finished: bool, genmask: bool, data: B, code: OpCode, finished: bool, genmask: bool,
) -> Binary { ) -> FramedBinary {
let payload = data.into(); let payload = data.into();
let one: u8 = if finished { let one: u8 = if finished {
0x80 | Into::<u8>::into(code) 0x80 | Into::<u8>::into(code)
@ -325,7 +325,7 @@ impl Frame {
buf buf
}; };
if genmask { let binary = if genmask {
let mask = rand::random::<u32>(); let mask = rand::random::<u32>();
buf.put_u32_le(mask); buf.put_u32_le(mask);
buf.extend_from_slice(payload.as_ref()); buf.extend_from_slice(payload.as_ref());
@ -335,7 +335,9 @@ impl Frame {
} else { } else {
buf.put_slice(payload.as_ref()); buf.put_slice(payload.as_ref());
buf.into() 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -502,7 +508,7 @@ mod tests {
let mut v = vec![137u8, 4u8]; let mut v = vec![137u8, 4u8];
v.extend(b"data"); v.extend(b"data");
assert_eq!(frame, v.into()); assert_eq!(frame.0, v.into());
} }
#[test] #[test]
@ -511,7 +517,7 @@ mod tests {
let mut v = vec![138u8, 4u8]; let mut v = vec![138u8, 4u8];
v.extend(b"data"); v.extend(b"data");
assert_eq!(frame, v.into()); assert_eq!(frame.0, v.into());
} }
#[test] #[test]
@ -521,12 +527,12 @@ mod tests {
let mut v = vec![136u8, 6u8, 3u8, 232u8]; let mut v = vec![136u8, 6u8, 3u8, 232u8];
v.extend(b"data"); v.extend(b"data");
assert_eq!(frame, v.into()); assert_eq!(frame.0, v.into());
} }
#[test] #[test]
fn test_empty_close_frame() { fn test_empty_close_frame() {
let frame = Frame::close(None, false); let frame = Frame::close(None, false);
assert_eq!(frame, vec![0x88, 0x00].into()); assert_eq!(frame.0, vec![0x88, 0x00].into());
} }
} }