diff --git a/Libraries/LibWebSocket/WebSocket.cpp b/Libraries/LibWebSocket/WebSocket.cpp index 5402a1e2f33..2f0b37e1f1b 100644 --- a/Libraries/LibWebSocket/WebSocket.cpp +++ b/Libraries/LibWebSocket/WebSocket.cpp @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -130,7 +131,11 @@ void WebSocket::close(u16 code, ByteString const& message) // Start the WebSocket closing handshake and set this’s ready state to CLOSING (2)." 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); + // Section 5.5.1: + // > If there is a body, the first two bytes of the body MUST be a 2-byte unsigned integer (in network byte order) + // > representing a status code with value /code/ defined in Section 7.4. + NetworkOrdered network_ordered_code { code }; + close_payload.overwrite(0, &network_ordered_code, sizeof(network_ordered_code)); close_payload.overwrite(2, message_bytes.data(), message_bytes.size()); send_frame(WebSocket::OpCode::ConnectionClose, close_payload, true); set_state(InternalState::Closing);