From d2e3602170a6e40b1bc0370b6a9b042420894ca1 Mon Sep 17 00:00:00 2001 From: Alexey Galakhov Date: Wed, 11 Jul 2018 18:29:20 +0200 Subject: [PATCH 1/5] frame: fix partial write Signed-off-by: Alexey Galakhov --- src/protocol/frame/frame.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/protocol/frame/frame.rs b/src/protocol/frame/frame.rs index cee9b00..6d45c7b 100644 --- a/src/protocol/frame/frame.rs +++ b/src/protocol/frame/frame.rs @@ -107,7 +107,7 @@ impl FrameHeader { | if self.mask.is_some() { 0x80 } else { 0 } }; - output.write(&[one, two])?; + output.write_all(&[one, two])?; match lenfmt { LengthFormat::U8(_) => (), LengthFormat::U16 => output.write_u16::(length as u16)?, From e6ebf5ac76523c6df31285eb6c7adae8718c3063 Mon Sep 17 00:00:00 2001 From: Alexey Galakhov Date: Wed, 11 Jul 2018 18:36:31 +0200 Subject: [PATCH 2/5] trivial: style fixes Signed-off-by: Alexey Galakhov --- src/handshake/client.rs | 4 ++-- src/handshake/headers.rs | 2 +- src/handshake/machine.rs | 4 ++-- src/handshake/mod.rs | 2 +- src/protocol/frame/frame.rs | 4 ++-- src/protocol/frame/mod.rs | 4 ++-- src/protocol/mod.rs | 6 +++--- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/handshake/client.rs b/src/handshake/client.rs index 15494f9..014af64 100644 --- a/src/handshake/client.rs +++ b/src/handshake/client.rs @@ -52,7 +52,7 @@ impl<'t> Request<'t> { /// Adds a custom header to the request. pub fn add_header(&mut self, name: Cow<'t, str>, value: Cow<'t, str>) { - let mut headers = self.extra_headers.take().unwrap_or(vec![]); + let mut headers = self.extra_headers.take().unwrap_or_else(Vec::new); headers.push((name, value)); self.extra_headers = Some(headers); } @@ -113,7 +113,7 @@ impl ClientHandshake { }; trace!("Client handshake initiated."); - MidHandshake { role: client, machine: machine } + MidHandshake { role: client, machine } } } diff --git a/src/handshake/headers.rs b/src/handshake/headers.rs index 73419b6..23f0d77 100644 --- a/src/handshake/headers.rs +++ b/src/handshake/headers.rs @@ -28,7 +28,7 @@ impl Headers { /// Iterate over all headers with the given name. pub fn find<'headers, 'name>(&'headers self, name: &'name str) -> HeadersIter<'name, 'headers> { HeadersIter { - name: name, + name, iter: self.data.iter() } } diff --git a/src/handshake/machine.rs b/src/handshake/machine.rs index a945be1..39f37a5 100644 --- a/src/handshake/machine.rs +++ b/src/handshake/machine.rs @@ -16,14 +16,14 @@ impl HandshakeMachine { /// Start reading data from the peer. pub fn start_read(stream: Stream) -> Self { HandshakeMachine { - stream: stream, + stream, state: HandshakeState::Reading(InputBuffer::with_capacity(MIN_READ)), } } /// Start writing data to the peer. pub fn start_write>>(stream: Stream, data: D) -> Self { HandshakeMachine { - stream: stream, + stream, state: HandshakeState::Writing(Cursor::new(data.into())), } } diff --git a/src/handshake/mod.rs b/src/handshake/mod.rs index fcb3f78..f28445d 100644 --- a/src/handshake/mod.rs +++ b/src/handshake/mod.rs @@ -110,7 +110,7 @@ pub enum ProcessingResult { fn convert_key(input: &[u8]) -> Result { // ... field is constructed by concatenating /key/ ... // ... with the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" (RFC 6455) - const WS_GUID: &'static [u8] = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; + const WS_GUID: &[u8] = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; let mut sha1 = Sha1::default(); sha1.input(input); sha1.input(WS_GUID); diff --git a/src/protocol/frame/frame.rs b/src/protocol/frame/frame.rs index 6d45c7b..fba686a 100644 --- a/src/protocol/frame/frame.rs +++ b/src/protocol/frame/frame.rs @@ -296,7 +296,7 @@ impl Frame { let code = NetworkEndian::read_u16(&data[0..2]).into(); data.drain(0..2); let text = String::from_utf8(data)?; - Ok(Some(CloseFrame { code: code, reason: text.into() })) + Ok(Some(CloseFrame { code, reason: text.into() })) } } } @@ -357,7 +357,7 @@ impl Frame { Frame { header: FrameHeader::default(), - payload: payload, + payload, } } diff --git a/src/protocol/frame/mod.rs b/src/protocol/frame/mod.rs index 470a795..9d17338 100644 --- a/src/protocol/frame/mod.rs +++ b/src/protocol/frame/mod.rs @@ -30,7 +30,7 @@ impl FrameSocket { /// Create a new frame socket. pub fn new(stream: Stream) -> Self { FrameSocket { - stream: stream, + stream, in_buffer: InputBuffer::with_capacity(MIN_READ), out_buffer: Vec::new(), header: None, @@ -40,7 +40,7 @@ impl FrameSocket { /// Create a new frame socket from partially read data. pub fn from_partially_read(stream: Stream, part: Vec) -> Self { FrameSocket { - stream: stream, + stream, in_buffer: InputBuffer::from_partially_read(part), out_buffer: Vec::new(), header: None, diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index e35b9b7..d1eb958 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -116,13 +116,13 @@ impl WebSocket { config: Option ) -> Self { WebSocket { - role: role, - socket: socket, + role, + socket, state: WebSocketState::Active, incomplete: None, send_queue: VecDeque::new(), pong: None, - config: config.unwrap_or_else(|| WebSocketConfig::default()), + config: config.unwrap_or_else(WebSocketConfig::default), } } } From a2e122ab9e315da24a1d49d3988d12ec84503d85 Mon Sep 17 00:00:00 2001 From: Alexey Galakhov Date: Wed, 11 Jul 2018 19:04:02 +0200 Subject: [PATCH 3/5] refactor: mask: pass mask by value Signed-off-by: Alexey Galakhov --- src/protocol/frame/frame.rs | 2 +- src/protocol/frame/mask.rs | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/protocol/frame/frame.rs b/src/protocol/frame/frame.rs index fba686a..c5bf9c2 100644 --- a/src/protocol/frame/frame.rs +++ b/src/protocol/frame/frame.rs @@ -269,7 +269,7 @@ impl Frame { #[inline] pub(crate) fn apply_mask(&mut self) { if let Some(mask) = self.header.mask.take() { - apply_mask(&mut self.payload, &mask) + apply_mask(&mut self.payload, mask) } } diff --git a/src/protocol/frame/mask.rs b/src/protocol/frame/mask.rs index 055be08..8b39d76 100644 --- a/src/protocol/frame/mask.rs +++ b/src/protocol/frame/mask.rs @@ -11,14 +11,14 @@ pub fn generate_mask() -> [u8; 4] { /// Mask/unmask a frame. #[inline] -pub fn apply_mask(buf: &mut [u8], mask: &[u8; 4]) { +pub fn apply_mask(buf: &mut [u8], mask: [u8; 4]) { apply_mask_fast32(buf, mask) } /// A safe unoptimized mask application. #[inline] #[allow(dead_code)] -fn apply_mask_fallback(buf: &mut [u8], mask: &[u8; 4]) { +fn apply_mask_fallback(buf: &mut [u8], mask: [u8; 4]) { for (i, byte) in buf.iter_mut().enumerate() { *byte ^= mask[i & 3]; } @@ -27,7 +27,7 @@ fn apply_mask_fallback(buf: &mut [u8], mask: &[u8; 4]) { /// Faster version of `apply_mask()` which operates on 4-byte blocks. #[inline] #[allow(dead_code)] -fn apply_mask_fast32(buf: &mut [u8], mask: &[u8; 4]) { +fn apply_mask_fast32(buf: &mut [u8], mask: [u8; 4]) { let mask_u32: u32 = unsafe { read_unaligned(mask.as_ptr() as *const u32) }; @@ -101,10 +101,10 @@ mod tests { // Check masking with proper alignment. { let mut masked = unmasked.clone(); - apply_mask_fallback(&mut masked, &mask); + apply_mask_fallback(&mut masked, mask); let mut masked_fast = unmasked.clone(); - apply_mask_fast32(&mut masked_fast, &mask); + apply_mask_fast32(&mut masked_fast, mask); assert_eq!(masked, masked_fast); } @@ -112,10 +112,10 @@ mod tests { // Check masking without alignment. { let mut masked = unmasked.clone(); - apply_mask_fallback(&mut masked[1..], &mask); + apply_mask_fallback(&mut masked[1..], mask); let mut masked_fast = unmasked.clone(); - apply_mask_fast32(&mut masked_fast[1..], &mask); + apply_mask_fast32(&mut masked_fast[1..], mask); assert_eq!(masked, masked_fast); } From 9b0a2e0f114524f5f9af484d2b21da1325cbd255 Mon Sep 17 00:00:00 2001 From: Alexey Galakhov Date: Mon, 16 Jul 2018 14:57:14 +0200 Subject: [PATCH 4/5] trivial: split public and private impl Signed-off-by: Alexey Galakhov --- src/protocol/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index d1eb958..62ecbf2 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -108,7 +108,9 @@ impl WebSocket { pub fn get_mut(&mut self) -> &mut Stream { self.socket.get_mut() } +} +impl WebSocket { /// Convert a frame socket into a WebSocket. fn from_frame_socket( socket: FrameSocket, From a7e87488c4d0fe86baa87dac5a4d775ca7b2a315 Mon Sep 17 00:00:00 2001 From: Alexey Galakhov Date: Mon, 16 Jul 2018 15:57:22 +0200 Subject: [PATCH 5/5] config: make config settable at runtime Signed-off-by: Alexey Galakhov --- src/protocol/mod.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 62ecbf2..8b3de04 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -108,6 +108,11 @@ impl WebSocket { pub fn get_mut(&mut self) -> &mut Stream { self.socket.get_mut() } + + /// Change the configuration. + pub fn set_config(&mut self, set_func: impl FnOnce(&mut WebSocketConfig)) { + set_func(&mut self.config) + } } impl WebSocket {