From 0b9e6922985b1c3c69a89c04ca61f469dc34ad51 Mon Sep 17 00:00:00 2001 From: Lukas Lueg Date: Tue, 6 Aug 2019 18:32:22 +0200 Subject: [PATCH 1/2] Remove byteorder-dependency --- actix-http/Cargo.toml | 1 - actix-http/src/ws/frame.rs | 22 +++++++++++----------- actix-http/src/ws/mask.rs | 3 +-- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index 50b52d126..5cf5cafc2 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -57,7 +57,6 @@ actix-threadpool = "0.1.1" base64 = "0.10" bitflags = "1.0" bytes = "0.4" -byteorder = "1.2" copyless = "0.1.4" derive_more = "0.15.0" either = "1.5.2" diff --git a/actix-http/src/ws/frame.rs b/actix-http/src/ws/frame.rs index 652746b89..46e9f36db 100644 --- a/actix-http/src/ws/frame.rs +++ b/actix-http/src/ws/frame.rs @@ -1,4 +1,5 @@ -use byteorder::{ByteOrder, LittleEndian, NetworkEndian}; +use std::convert::TryFrom; + use bytes::{BufMut, Bytes, BytesMut}; use log::debug; use rand; @@ -48,14 +49,16 @@ impl Parser { if chunk_len < 4 { 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; len } else if len == 127 { if chunk_len < 10 { 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 { return Err(ProtocolError::Overflow); } @@ -75,10 +78,10 @@ impl Parser { return Ok(None); } - let mask: &[u8] = &src[idx..idx + 4]; - let mask_u32 = LittleEndian::read_u32(mask); + let mask = + u32::from_le_bytes(TryFrom::try_from(&src[idx..idx + 4]).unwrap()); idx += 4; - Some(mask_u32) + Some(mask) } else { None }; @@ -137,7 +140,7 @@ impl Parser { /// Parse the payload of a close frame. pub fn parse_close_payload(payload: &[u8]) -> Option { 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 description = if payload.len() > 2 { Some(String::from_utf8_lossy(&payload[2..]).into()) @@ -201,10 +204,7 @@ impl Parser { let payload = match reason { None => Vec::new(), Some(reason) => { - let mut code_bytes = [0; 2]; - NetworkEndian::write_u16(&mut code_bytes, reason.code.into()); - - let mut payload = Vec::from(&code_bytes[..]); + let mut payload = Into::::into(reason.code).to_be_bytes().to_vec(); if let Some(description) = reason.description { payload.extend(description.as_bytes()); } diff --git a/actix-http/src/ws/mask.rs b/actix-http/src/ws/mask.rs index 157375417..9f7304039 100644 --- a/actix-http/src/ws/mask.rs +++ b/actix-http/src/ws/mask.rs @@ -105,7 +105,6 @@ fn align_buf(buf: &mut [u8]) -> (ShortSlice, &mut [u64], ShortSlice) { #[cfg(test)] mod tests { use super::apply_mask; - use byteorder::{ByteOrder, LittleEndian}; /// A safe unoptimized mask application. fn apply_mask_fallback(buf: &mut [u8], mask: &[u8; 4]) { @@ -117,7 +116,7 @@ mod tests { #[test] fn test_apply_mask() { 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![ 0xf3, 0x00, 0x01, 0x02, 0x03, 0x80, 0x81, 0x82, 0xff, 0xfe, 0x00, 0x17, From b70de5b99125479a718c00e99671c91c4669dcda Mon Sep 17 00:00:00 2001 From: Lukas Lueg Date: Wed, 7 Aug 2019 16:43:03 +0200 Subject: [PATCH 2/2] Update CHANGES.md --- actix-http/CHANGES.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index d7cd42f68..ab81bbcdd 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -1,5 +1,12 @@ # Changes +## [0.2.9] - 2019-08-xx + +### Changed + +* Dropped the `byteorder`-dependency in favor of `stdlib`-implementation + + ## [0.2.8] - 2019-08-01 ### Added