LibWebSocket: Move web socket to Closing state in WebSocket::close()

This commit is contained in:
Andreas Kling 2022-11-08 19:31:10 +01:00
parent 9bbad08546
commit 0414d7f728
Notes: sideshowbarker 2024-07-17 04:39:29 +09:00

View file

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