1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-27 17:22:57 +01:00

drop unused code

This commit is contained in:
Nikolay Kim 2017-10-08 17:47:41 -07:00
parent 63b78b6461
commit e398694bdb
5 changed files with 18 additions and 201 deletions

View File

@ -27,7 +27,6 @@ http = "0.1"
httparse = "0.1" httparse = "0.1"
slab = "0.4" slab = "0.4"
sha1 = "0.2" sha1 = "0.2"
rand = "0.3"
url = "1.5" url = "1.5"
route-recognizer = "0.1" route-recognizer = "0.1"

View File

@ -4,7 +4,6 @@
extern crate log; extern crate log;
extern crate time; extern crate time;
extern crate bytes; extern crate bytes;
extern crate rand;
extern crate sha1; extern crate sha1;
extern crate url; extern crate url;
#[macro_use] #[macro_use]

View File

@ -296,7 +296,8 @@ impl WsWriter {
pub fn ping<A>(ctx: &mut HttpContext<A>, message: String) pub fn ping<A>(ctx: &mut HttpContext<A>, message: String)
where A: Actor<Context=HttpContext<A>> + Route where A: Actor<Context=HttpContext<A>> + Route
{ {
let mut frame = wsframe::Frame::ping(Vec::from(message.as_str())); let mut frame = wsframe::Frame::message(
Vec::from(message.as_str()), OpCode::Ping, true);
let mut buf = Vec::new(); let mut buf = Vec::new();
frame.format(&mut buf).unwrap(); frame.format(&mut buf).unwrap();
@ -309,7 +310,8 @@ impl WsWriter {
pub fn pong<A>(ctx: &mut HttpContext<A>, message: String) pub fn pong<A>(ctx: &mut HttpContext<A>, message: String)
where A: Actor<Context=HttpContext<A>> + Route where A: Actor<Context=HttpContext<A>> + Route
{ {
let mut frame = wsframe::Frame::pong(Vec::from(message.as_str())); let mut frame = wsframe::Frame::message(
Vec::from(message.as_str()), OpCode::Pong, true);
let mut buf = Vec::new(); let mut buf = Vec::new();
frame.format(&mut buf).unwrap(); frame.format(&mut buf).unwrap();

View File

@ -1,11 +1,6 @@
#![allow(dead_code, unused_variables)] use std::{fmt, mem};
use std::fmt;
use std::mem::transmute;
use std::io::{Write, Error, ErrorKind}; use std::io::{Write, Error, ErrorKind};
use std::default::Default;
use std::iter::FromIterator; use std::iter::FromIterator;
use rand;
use bytes::BytesMut; use bytes::BytesMut;
use wsproto::{OpCode, CloseCode}; use wsproto::{OpCode, CloseCode};
@ -18,11 +13,6 @@ fn apply_mask(buf: &mut [u8], mask: &[u8; 4]) {
} }
} }
#[inline]
fn generate_mask() -> [u8; 4] {
unsafe { transmute(rand::random::<u32>()) }
}
/// A struct representing a `WebSocket` frame. /// A struct representing a `WebSocket` frame.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct Frame { pub(crate) struct Frame {
@ -47,7 +37,7 @@ impl Frame {
#[inline] #[inline]
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
let mut header_length = 2; let mut header_length = 2;
let payload_len = self.payload().len(); let payload_len = self.payload.len();
if payload_len > 125 { if payload_len > 125 {
if payload_len <= u16::max_value() as usize { if payload_len <= u16::max_value() as usize {
header_length += 2; header_length += 2;
@ -56,143 +46,13 @@ impl Frame {
} }
} }
if self.is_masked() { if self.mask.is_some() {
header_length += 4; header_length += 4;
} }
header_length + payload_len header_length + payload_len
} }
/// Test whether the frame is a final frame.
#[inline]
pub fn is_final(&self) -> bool {
self.finished
}
/// Test whether the first reserved bit is set.
#[inline]
pub fn has_rsv1(&self) -> bool {
self.rsv1
}
/// Test whether the second reserved bit is set.
#[inline]
pub fn has_rsv2(&self) -> bool {
self.rsv2
}
/// Test whether the third reserved bit is set.
#[inline]
pub fn has_rsv3(&self) -> bool {
self.rsv3
}
/// Get the OpCode of the frame.
#[inline]
pub fn opcode(&self) -> OpCode {
self.opcode
}
/// Test whether this is a control frame.
#[inline]
pub fn is_control(&self) -> bool {
self.opcode.is_control()
}
/// Get a reference to the frame's payload.
#[inline]
pub fn payload(&self) -> &Vec<u8> {
&self.payload
}
// Test whether the frame is masked.
#[doc(hidden)]
#[inline]
pub fn is_masked(&self) -> bool {
self.mask.is_some()
}
// Get an optional reference to the frame's mask.
#[doc(hidden)]
#[allow(dead_code)]
#[inline]
pub fn mask(&self) -> Option<&[u8; 4]> {
self.mask.as_ref()
}
/// Make this frame a final frame.
#[allow(dead_code)]
#[inline]
pub fn set_final(&mut self, is_final: bool) -> &mut Frame {
self.finished = is_final;
self
}
/// Set the first reserved bit.
#[inline]
pub fn set_rsv1(&mut self, has_rsv1: bool) -> &mut Frame {
self.rsv1 = has_rsv1;
self
}
/// Set the second reserved bit.
#[inline]
pub fn set_rsv2(&mut self, has_rsv2: bool) -> &mut Frame {
self.rsv2 = has_rsv2;
self
}
/// Set the third reserved bit.
#[inline]
pub fn set_rsv3(&mut self, has_rsv3: bool) -> &mut Frame {
self.rsv3 = has_rsv3;
self
}
/// Set the OpCode.
#[allow(dead_code)]
#[inline]
pub fn set_opcode(&mut self, opcode: OpCode) -> &mut Frame {
self.opcode = opcode;
self
}
/// Edit the frame's payload.
#[allow(dead_code)]
#[inline]
pub fn payload_mut(&mut self) -> &mut Vec<u8> {
&mut self.payload
}
// Generate a new mask for this frame.
//
// This method simply generates and stores the mask. It does not change the payload data.
// Instead, the payload data will be masked with the generated mask when the frame is sent
// to the other endpoint.
#[doc(hidden)]
#[inline]
pub fn set_mask(&mut self) -> &mut Frame {
self.mask = Some(generate_mask());
self
}
// This method unmasks the payload and should only be called on frames that are actually
// masked. In other words, those frames that have just been received from a client endpoint.
#[doc(hidden)]
#[inline]
pub fn remove_mask(&mut self) -> &mut Frame {
self.mask.and_then(|mask| {
Some(apply_mask(&mut self.payload, &mask))
});
self.mask = None;
self
}
/// Consume the frame into its payload.
pub fn into_data(self) -> Vec<u8> {
self.payload
}
/// Create a new data frame. /// Create a new data frame.
#[inline] #[inline]
pub fn message(data: Vec<u8>, code: OpCode, finished: bool) -> Frame { pub fn message(data: Vec<u8>, code: OpCode, finished: bool) -> Frame {
@ -209,32 +69,12 @@ impl Frame {
} }
} }
/// Create a new Pong control frame.
#[inline]
pub fn pong(data: Vec<u8>) -> Frame {
Frame {
opcode: OpCode::Pong,
payload: data,
.. Frame::default()
}
}
/// Create a new Ping control frame.
#[inline]
pub fn ping(data: Vec<u8>) -> Frame {
Frame {
opcode: OpCode::Ping,
payload: data,
.. Frame::default()
}
}
/// Create a new Close control frame. /// Create a new Close control frame.
#[inline] #[inline]
pub fn close(code: CloseCode, reason: &str) -> Frame { pub fn close(code: CloseCode, reason: &str) -> Frame {
let raw: [u8; 2] = unsafe { let raw: [u8; 2] = unsafe {
let u: u16 = code.into(); let u: u16 = code.into();
transmute(u.to_be()) mem::transmute(u.to_be())
}; };
let payload = if let CloseCode::Empty = code { let payload = if let CloseCode::Empty = code {
@ -298,7 +138,7 @@ impl Frame {
idx += 2; idx += 2;
length = u64::from(unsafe{ length = u64::from(unsafe{
let mut wide: u16 = transmute(length_bytes); let mut wide: u16 = mem::transmute(length_bytes);
wide = u16::from_be(wide); wide = u16::from_be(wide);
wide}); wide});
header_length += 2; header_length += 2;
@ -311,7 +151,7 @@ impl Frame {
size -= 8; size -= 8;
idx += 2; idx += 2;
unsafe { length = transmute(length_bytes); } unsafe { length = mem::transmute(length_bytes); }
length = u64::from_be(length); length = u64::from_be(length);
header_length += 8; header_length += 8;
} }
@ -393,23 +233,23 @@ impl Frame {
{ {
let mut one = 0u8; let mut one = 0u8;
let code: u8 = self.opcode.into(); let code: u8 = self.opcode.into();
if self.is_final() { if self.finished {
one |= 0x80; one |= 0x80;
} }
if self.has_rsv1() { if self.rsv1 {
one |= 0x40; one |= 0x40;
} }
if self.has_rsv2() { if self.rsv2 {
one |= 0x20; one |= 0x20;
} }
if self.has_rsv3() { if self.rsv3 {
one |= 0x10; one |= 0x10;
} }
one |= code; one |= code;
let mut two = 0u8; let mut two = 0u8;
if self.is_masked() { if self.mask.is_some() {
two |= 0x80; two |= 0x80;
} }
@ -421,7 +261,7 @@ impl Frame {
two |= 126; two |= 126;
let length_bytes: [u8; 2] = unsafe { let length_bytes: [u8; 2] = unsafe {
let short = self.payload.len() as u16; let short = self.payload.len() as u16;
transmute(short.to_be()) mem::transmute(short.to_be())
}; };
let headers = [one, two, length_bytes[0], length_bytes[1]]; let headers = [one, two, length_bytes[0], length_bytes[1]];
try!(w.write_all(&headers)); try!(w.write_all(&headers));
@ -429,7 +269,7 @@ impl Frame {
two |= 127; two |= 127;
let length_bytes: [u8; 8] = unsafe { let length_bytes: [u8; 8] = unsafe {
let long = self.payload.len() as u64; let long = self.payload.len() as u64;
transmute(long.to_be()) mem::transmute(long.to_be())
}; };
let headers = [ let headers = [
one, one,
@ -446,7 +286,7 @@ impl Frame {
try!(w.write_all(&headers)); try!(w.write_all(&headers));
} }
if self.is_masked() { if self.mask.is_some() {
let mask = self.mask.take().unwrap(); let mask = self.mask.take().unwrap();
apply_mask(&mut self.payload, &mask); apply_mask(&mut self.payload, &mask);
try!(w.write_all(&mask)); try!(w.write_all(&mask));

View File

@ -1,9 +1,5 @@
#![allow(dead_code, unused_variables)]
use std::fmt; use std::fmt;
use std::convert::{Into, From}; use std::convert::{Into, From};
use std::mem::transmute;
use rand;
use sha1; use sha1;
@ -27,18 +23,6 @@ pub(crate) enum OpCode {
Bad, Bad,
} }
impl OpCode {
/// Test whether the opcode indicates a control frame.
pub fn is_control(&self) -> bool {
match *self {
Text | Binary | Continue => false,
_ => true,
}
}
}
impl fmt::Display for OpCode { impl fmt::Display for OpCode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
@ -209,13 +193,6 @@ static WS_GUID: &'static str = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
static BASE64: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static BASE64: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
fn generate_key() -> String {
let key: [u8; 16] = unsafe {
transmute(rand::random::<(u64, u64)>())
};
encode_base64(&key)
}
// TODO: hash is always same size, we dont need String // TODO: hash is always same size, we dont need String
pub(crate) fn hash_key(key: &[u8]) -> String { pub(crate) fn hash_key(key: &[u8]) -> String {
let mut hasher = sha1::Sha1::new(); let mut hasher = sha1::Sha1::new();