mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 12:35:14 +00:00
LibWebSocket: Move web socket to Closing state in WebSocket::close()
This commit is contained in:
parent
9bbad08546
commit
0414d7f728
Notes:
sideshowbarker
2024-07-17 04:39:29 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/0414d7f728
1 changed files with 21 additions and 7 deletions
|
@ -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()
|
||||
|
|
Loading…
Add table
Reference in a new issue