1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

Merge pull request #192 from fuchsnj/check_if_close_code_exists

check if close code exists before reading it
This commit is contained in:
Nikolay Kim 2018-04-21 07:54:25 -07:00 committed by GitHub
commit 805dbea8e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -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(),

View File

@ -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()