mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:57:35 +00:00
LibWeb: Make WebSocket::close() arguments optional to match IDL
This commit is contained in:
parent
c843f2e3a5
commit
8abe653009
2 changed files with 19 additions and 12 deletions
|
@ -142,22 +142,29 @@ String WebSocket::protocol() const
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://websockets.spec.whatwg.org/#dom-websocket-close
|
// https://websockets.spec.whatwg.org/#dom-websocket-close
|
||||||
DOM::ExceptionOr<void> WebSocket::close(u16 code, const String& reason)
|
DOM::ExceptionOr<void> WebSocket::close(Optional<u16> code, Optional<String> reason)
|
||||||
{
|
{
|
||||||
// HACK : we should have an Optional<u16>
|
// 1. If code is present, but is neither an integer equal to 1000 nor an integer in the range 3000 to 4999, inclusive, throw an "InvalidAccessError" DOMException.
|
||||||
if (code == 0)
|
if (code.has_value() && *code != 1000 && (*code < 3000 || *code > 4099))
|
||||||
code = 1000;
|
|
||||||
if (code != 1000 && (code < 3000 || code > 4099))
|
|
||||||
return DOM::InvalidAccessError::create("The close error code is invalid");
|
return DOM::InvalidAccessError::create("The close error code is invalid");
|
||||||
if (!reason.is_empty() && reason.bytes().size() > 123)
|
// 2. If reason is present, then run these substeps:
|
||||||
return DOM::SyntaxError::create("The close reason is longer than 123 bytes");
|
if (reason.has_value()) {
|
||||||
|
// 1. Let reasonBytes be the result of encoding reason.
|
||||||
|
// 2. If reasonBytes is longer than 123 bytes, then throw a "SyntaxError" DOMException.
|
||||||
|
if (reason->bytes().size() > 123)
|
||||||
|
return DOM::SyntaxError::create("The close reason is longer than 123 bytes");
|
||||||
|
}
|
||||||
|
// 3. Run the first matching steps from the following list:
|
||||||
auto state = ready_state();
|
auto state = ready_state();
|
||||||
|
// -> If this's ready state is CLOSING (2) or CLOSED (3)
|
||||||
if (state == WebSocket::ReadyState::Closing || state == WebSocket::ReadyState::Closed)
|
if (state == WebSocket::ReadyState::Closing || state == WebSocket::ReadyState::Closed)
|
||||||
return {};
|
return {};
|
||||||
// Note : Both of these are handled by the WebSocket Protocol when calling close()
|
// -> If the WebSocket connection is not yet established [WSP]
|
||||||
// 3b. If the WebSocket connection is not yet established [WSP]
|
// -> If the WebSocket closing handshake has not yet been started [WSP]
|
||||||
// 3c. If the WebSocket closing handshake has not yet been started [WSP]
|
// -> Otherwise
|
||||||
m_websocket->close(code, reason);
|
// NOTE: All of these are handled by the WebSocket Protocol when calling close()
|
||||||
|
// FIXME: LibProtocol does not yet support sending empty Close messages, so we use default values for now
|
||||||
|
m_websocket->close(code.value_or(1000), reason.value_or(String::empty()));
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -87,7 +87,7 @@ public:
|
||||||
const String& binary_type() { return m_binary_type; };
|
const String& binary_type() { return m_binary_type; };
|
||||||
void set_binary_type(const String& type) { m_binary_type = type; };
|
void set_binary_type(const String& type) { m_binary_type = type; };
|
||||||
|
|
||||||
DOM::ExceptionOr<void> close(u16 code, const String& reason);
|
DOM::ExceptionOr<void> close(Optional<u16> code, Optional<String> reason);
|
||||||
DOM::ExceptionOr<void> send(const String& data);
|
DOM::ExceptionOr<void> send(const String& data);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue