Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/websocket): Fix close code without reason #27578

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ext/websocket/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,14 @@ pub async fn op_ws_close(
return Ok(());
};

const EMPTY_PAYLOAD: &[u8] = &[];

let frame = reason
.map(|reason| Frame::close(code.unwrap_or(1005), reason.as_bytes()))
.unwrap_or_else(|| Frame::close_raw(vec![].into()));
.unwrap_or_else(|| match code {
Some(code) => Frame::close(code, EMPTY_PAYLOAD),
_ => Frame::close_raw(EMPTY_PAYLOAD.into()),
});

resource.closed.set(true);
let lock = resource.reserve_lock();
Expand Down
1 change: 0 additions & 1 deletion tests/integration/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2663,7 +2663,6 @@ async fn websocket_server_multi_field_connection_header() {

let message = socket.read_frame().await.unwrap();
assert_eq!(message.opcode, fastwebsockets::OpCode::Close);
assert!(message.payload.is_empty());
socket
.write_frame(fastwebsockets::Frame::close_raw(vec![].into()))
.await
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/websocket_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ Deno.test({
socket.onopen = () => socket.send("Hello");
socket.onmessage = () => {
socket.send("Bye");
socket.close();
socket.close(1000);
};
socket.onclose = () => ac.abort();
socket.onerror = () => fail();
Expand All @@ -288,7 +288,8 @@ Deno.test({
seenBye = true;
}
};
ws.onclose = () => {
ws.onclose = (e) => {
assertEquals(e.code, 1000);
deferred.resolve();
};
await Promise.all([deferred.promise, server.finished]);
Expand Down
Loading