diff options
author | Andreas Kling <kling@serenityos.org> | 2022-11-08 19:31:10 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-11-09 02:06:33 +0100 |
commit | 0414d7f7289085583ff0fdb2ecab2605e62aec3c (patch) | |
tree | 8abbf76dedcd04b39eba82bc05660979417545de | |
parent | 9bbad085468c64fcebee350b13a01ed0a3cf3d34 (diff) | |
download | serenity-0414d7f7289085583ff0fdb2ecab2605e62aec3c.zip |
LibWebSocket: Move web socket to Closing state in WebSocket::close()
-rw-r--r-- | Userland/Libraries/LibWebSocket/WebSocket.cpp | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/Userland/Libraries/LibWebSocket/WebSocket.cpp b/Userland/Libraries/LibWebSocket/WebSocket.cpp index d3c7858cd4..a1e692d277 100644 --- a/Userland/Libraries/LibWebSocket/WebSocket.cpp +++ b/Userland/Libraries/LibWebSocket/WebSocket.cpp @@ -87,14 +87,28 @@ void WebSocket::send(Message const& message) void WebSocket::close(u16 code, String const& message) { - // Calling close on a socket that is not opened is not allowed - VERIFY(m_state == WebSocket::InternalState::Open); VERIFY(m_impl); - auto message_bytes = message.bytes(); - auto close_payload = ByteBuffer::create_uninitialized(message_bytes.size() + 2).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation. - close_payload.overwrite(0, (u8*)&code, 2); - close_payload.overwrite(2, message_bytes.data(), message_bytes.size()); - send_frame(WebSocket::OpCode::ConnectionClose, close_payload, true); + + switch (m_state) { + case InternalState::NotStarted: + case InternalState::EstablishingProtocolConnection: + case InternalState::SendingClientHandshake: + case InternalState::WaitingForServerHandshake: + // FIXME: Fail the connection. + m_state = InternalState::Closing; + break; + case InternalState::Open: { + auto message_bytes = message.bytes(); + auto close_payload = ByteBuffer::create_uninitialized(message_bytes.size() + 2).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation. + close_payload.overwrite(0, (u8*)&code, 2); + close_payload.overwrite(2, message_bytes.data(), message_bytes.size()); + send_frame(WebSocket::OpCode::ConnectionClose, close_payload, true); + m_state = InternalState::Closing; + break; + } + default: + break; + } } void WebSocket::drain_read() |