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

Remove byteorder-dependency

This commit is contained in:
Lukas Lueg 2019-08-06 18:32:22 +02:00
parent cf1a60cb3a
commit 0b9e692298
3 changed files with 12 additions and 14 deletions

View File

@ -57,7 +57,6 @@ actix-threadpool = "0.1.1"
base64 = "0.10" base64 = "0.10"
bitflags = "1.0" bitflags = "1.0"
bytes = "0.4" bytes = "0.4"
byteorder = "1.2"
copyless = "0.1.4" copyless = "0.1.4"
derive_more = "0.15.0" derive_more = "0.15.0"
either = "1.5.2" either = "1.5.2"

View File

@ -1,4 +1,5 @@
use byteorder::{ByteOrder, LittleEndian, NetworkEndian}; use std::convert::TryFrom;
use bytes::{BufMut, Bytes, BytesMut}; use bytes::{BufMut, Bytes, BytesMut};
use log::debug; use log::debug;
use rand; use rand;
@ -48,14 +49,16 @@ impl Parser {
if chunk_len < 4 { if chunk_len < 4 {
return Ok(None); return Ok(None);
} }
let len = NetworkEndian::read_uint(&src[idx..], 2) as usize; let len = usize::from(u16::from_be_bytes(
TryFrom::try_from(&src[idx..idx + 2]).unwrap(),
));
idx += 2; idx += 2;
len len
} else if len == 127 { } else if len == 127 {
if chunk_len < 10 { if chunk_len < 10 {
return Ok(None); return Ok(None);
} }
let len = NetworkEndian::read_uint(&src[idx..], 8); let len = u64::from_be_bytes(TryFrom::try_from(&src[idx..idx + 8]).unwrap());
if len > max_size as u64 { if len > max_size as u64 {
return Err(ProtocolError::Overflow); return Err(ProtocolError::Overflow);
} }
@ -75,10 +78,10 @@ impl Parser {
return Ok(None); return Ok(None);
} }
let mask: &[u8] = &src[idx..idx + 4]; let mask =
let mask_u32 = LittleEndian::read_u32(mask); u32::from_le_bytes(TryFrom::try_from(&src[idx..idx + 4]).unwrap());
idx += 4; idx += 4;
Some(mask_u32) Some(mask)
} else { } else {
None None
}; };
@ -137,7 +140,7 @@ impl Parser {
/// Parse the payload of a close frame. /// Parse the payload of a close frame.
pub fn parse_close_payload(payload: &[u8]) -> Option<CloseReason> { pub fn parse_close_payload(payload: &[u8]) -> Option<CloseReason> {
if payload.len() >= 2 { if payload.len() >= 2 {
let raw_code = NetworkEndian::read_u16(payload); let raw_code = u16::from_be_bytes(TryFrom::try_from(&payload[..2]).unwrap());
let code = CloseCode::from(raw_code); let code = CloseCode::from(raw_code);
let description = if payload.len() > 2 { let description = if payload.len() > 2 {
Some(String::from_utf8_lossy(&payload[2..]).into()) Some(String::from_utf8_lossy(&payload[2..]).into())
@ -201,10 +204,7 @@ impl Parser {
let payload = match reason { let payload = match reason {
None => Vec::new(), None => Vec::new(),
Some(reason) => { Some(reason) => {
let mut code_bytes = [0; 2]; let mut payload = Into::<u16>::into(reason.code).to_be_bytes().to_vec();
NetworkEndian::write_u16(&mut code_bytes, reason.code.into());
let mut payload = Vec::from(&code_bytes[..]);
if let Some(description) = reason.description { if let Some(description) = reason.description {
payload.extend(description.as_bytes()); payload.extend(description.as_bytes());
} }

View File

@ -105,7 +105,6 @@ fn align_buf(buf: &mut [u8]) -> (ShortSlice, &mut [u64], ShortSlice) {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::apply_mask; use super::apply_mask;
use byteorder::{ByteOrder, LittleEndian};
/// A safe unoptimized mask application. /// A safe unoptimized mask application.
fn apply_mask_fallback(buf: &mut [u8], mask: &[u8; 4]) { fn apply_mask_fallback(buf: &mut [u8], mask: &[u8; 4]) {
@ -117,7 +116,7 @@ mod tests {
#[test] #[test]
fn test_apply_mask() { fn test_apply_mask() {
let mask = [0x6d, 0xb6, 0xb2, 0x80]; let mask = [0x6d, 0xb6, 0xb2, 0x80];
let mask_u32: u32 = LittleEndian::read_u32(&mask); let mask_u32 = u32::from_le_bytes(mask);
let unmasked = vec![ let unmasked = vec![
0xf3, 0x00, 0x01, 0x02, 0x03, 0x80, 0x81, 0x82, 0xff, 0xfe, 0x00, 0x17, 0xf3, 0x00, 0x01, 0x02, 0x03, 0x80, 0x81, 0x82, 0xff, 0xfe, 0x00, 0x17,