LibWebSocket: Fix close frame status code written in wrong byte order
The close status code was written using (u8*)&code which produces platform byte order (little-endian on x86/ARM). This caused echo servers to receive invalid close codes and respond with 1011 (UnexpectedCondition). Fixes for example WPT: https://wpt.live/websockets/Close-1000-verify-code.any.html?wss
This commit is contained in:
parent
c4c0afe4d7
commit
e7525bf3df
1 changed files with 6 additions and 1 deletions
|
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
#include <AK/Base64.h>
|
||||
#include <AK/Endian.h>
|
||||
#include <AK/Random.h>
|
||||
#include <LibCrypto/Hash/HashManager.h>
|
||||
#include <LibWebSocket/Impl/WebSocketImplSerenity.h>
|
||||
|
|
@ -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<u16> 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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue