Skip to content

Commit

Permalink
fix(ext/websocket): Fix close code without reason (#27578)
Browse files Browse the repository at this point in the history
Fixes #27566

The close code wasn't sent if reason was None, defaulting to 1005. This
patch allows sending close code without reason.
  • Loading branch information
littledivy authored Jan 8, 2025
1 parent 1661ddd commit e233173
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
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

0 comments on commit e233173

Please sign in to comment.