From 5528cf62f05397087ba3140206f9f418556e565d Mon Sep 17 00:00:00 2001 From: Nathan Fox Date: Fri, 20 Apr 2018 21:30:18 -0400 Subject: [PATCH 1/2] check if close code exists before reading it --- src/ws/mod.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/ws/mod.rs b/src/ws/mod.rs index 97162b19..f79f3f77 100644 --- a/src/ws/mod.rs +++ b/src/ws/mod.rs @@ -310,10 +310,14 @@ where } OpCode::Close => { self.closed = true; - let code = NetworkEndian::read_uint(payload.as_ref(), 2) as u16; - Ok(Async::Ready(Some(Message::Close(CloseCode::from( - code, - ))))) + let close_code = if payload.len() >= 2{ + let raw_code = NetworkEndian::read_uint(payload.as_ref(), 2) as u16; + CloseCode::from(raw_code) + }else{ + CloseCode::Status + }; + + Ok(Async::Ready(Some(Message::Close(close_code)))) } OpCode::Ping => Ok(Async::Ready(Some(Message::Ping( String::from_utf8_lossy(payload.as_ref()).into(), From dc9a24a189f5b32e0815b09d290f6154a7871341 Mon Sep 17 00:00:00 2001 From: Nathan Fox Date: Fri, 20 Apr 2018 21:55:07 -0400 Subject: [PATCH 2/2] add websocket empty close status test --- tests/test_ws.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_ws.rs b/tests/test_ws.rs index 2126543e..61f4af42 100644 --- a/tests/test_ws.rs +++ b/tests/test_ws.rs @@ -60,6 +60,16 @@ fn test_simple() { assert_eq!(item, Some(ws::Message::Close(ws::CloseCode::Normal))); } +#[test] +fn test_empty_close_code() { + let mut srv = test::TestServer::new(|app| app.handler(|req| ws::start(req, Ws))); + let (reader, mut writer) = srv.ws().unwrap(); + + writer.close(ws::CloseCode::Empty, ""); + let (item, _) = srv.execute(reader.into_future()).unwrap(); + assert_eq!(item, Some(ws::Message::Close(ws::CloseCode::Status))); +} + #[test] fn test_large_text() { let data = rand::thread_rng()