Skip to content

Truly independent web browser

LibWeb: Queue WebSocket ready state changes as tasks

Tim Ledbetter4663386d870c
diff --git a/Libraries/LibWeb/WebSockets/WebSocket.cpp b/Libraries/LibWeb/WebSockets/WebSocket.cpp
index c3880f5143d..d0c2591fb48 100644
--- a/Libraries/LibWeb/WebSockets/WebSocket.cpp
+++ b/Libraries/LibWeb/WebSockets/WebSocket.cpp
@@ -154,7 +154,7 @@ void WebSocket::finalize()
// If a WebSocket object is garbage collected while its connection is still open, the user agent must start the
// WebSocket closing handshake, with no status code for the Close message. [WSP]
- if (ready_state != Requests::WebSocket::ReadyState::Closing && ready_state != Requests::WebSocket::ReadyState::Closed) {
+ if (m_websocket && ready_state != Requests::WebSocket::ReadyState::Closing && ready_state != Requests::WebSocket::ReadyState::Closed) {
// FIXME: LibProtocol does not yet support sending empty Close messages, so we use default values for now
m_websocket->close(1000);
}
@@ -173,6 +173,9 @@ ErrorOr<void> WebSocket::establish_web_socket_connection(URL::URL const& url_rec
// FIXME: Integrate properly with FETCH as per https://fetch.spec.whatwg.org/#websocket-opening-handshake
// That means following https://websockets.spec.whatwg.org/#concept-websocket-establish
+ if (m_ready_state != Requests::WebSocket::ReadyState::Connecting)
+ return Error::from_string_literal("The WebSocket was closed before it connected");
+
auto* window_or_worker = HTML::window_or_worker_global_scope_from_global_object(client.global_object());
VERIFY(window_or_worker);
auto origin_string = window_or_worker->origin().to_byte_string();
@@ -211,10 +214,10 @@ ErrorOr<void> WebSocket::establish_web_socket_connection(URL::URL const& url_rec
self.on_error();
});
m_websocket->on_ready_state_change = GC::weak_callback(*this, [](auto& self) {
- self.update_activity_root();
+ if (self.m_websocket->ready_state() == Requests::WebSocket::ReadyState::Closing)
+ self.on_closing_handshake_started();
});
- update_activity_root();
return {};
}
@@ -258,9 +261,13 @@ void WebSocket::event_listener_list_changed()
// https://websockets.spec.whatwg.org/#dom-websocket-readystate
Requests::WebSocket::ReadyState WebSocket::ready_state() const
{
- if (m_websocket)
- return m_websocket->ready_state();
- return Requests::WebSocket::ReadyState::Closed;
+ return m_ready_state;
+}
+
+void WebSocket::set_ready_state(Requests::WebSocket::ReadyState ready_state)
+{
+ m_ready_state = ready_state;
+ update_activity_root();
}
// https://websockets.spec.whatwg.org/#dom-websocket-extensions
@@ -309,11 +316,11 @@ WebIDL::ExceptionOr<void> WebSocket::close(Optional<u16> code, Optional<Utf16Str
// NB: All of these are handled by the WebSocket Protocol when calling close(). We still set the ready state to
// CLOSING now though (which every case above expects), to prevent handling any messages from the remote server
// in the meantime.
- m_websocket->set_ready_state(Requests::WebSocket::ReadyState::Closing);
- update_activity_root();
+ set_ready_state(Requests::WebSocket::ReadyState::Closing);
// FIXME: LibProtocol does not yet support sending empty Close messages, so we use default values for now
- m_websocket->close(code.value_or(1000), encoded_reason.to_byte_string());
+ if (m_websocket)
+ m_websocket->close(code.value_or(1000), encoded_reason.to_byte_string());
return {};
}
@@ -354,13 +361,18 @@ WebIDL::ExceptionOr<void> WebSocket::send(WebSocketSendData const& data)
// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
void WebSocket::on_open()
{
- update_activity_root();
-
// When the WebSocket connection is established, the user agent must queue a task to run these steps:
HTML::queue_a_task(HTML::Task::Source::WebSocket, nullptr, nullptr, GC::create_function(GC::Heap::the(), [this] {
- // 1. Change the readyState attribute's value to OPEN (1).
+ // AD-HOC: Matching other engines, do not fire an open event if close() was called before this task ran.
+ if (m_ready_state != Requests::WebSocket::ReadyState::Connecting)
+ return;
+
+ // 1. Change the ready state to OPEN (1).
+ set_ready_state(Requests::WebSocket::ReadyState::Open);
+
// 2. Change the extensions attribute's value to the extensions in use, if it is not the null value. [WSP]
// 3. Change the protocol attribute's value to the subprotocol in use, if it is not the null value. [WSP]
+ // 4. Fire an event named open at the WebSocket object.
dispatch_event(DOM::Event::create(HTML::EventNames::open, HighResolutionTime::current_high_resolution_time(relevant_global_object())));
}));
}
@@ -371,7 +383,7 @@ void WebSocket::on_message(ByteBuffer message, bool is_text)
// When a WebSocket message has been received with type type and data data, the user agent must queue a task to follow these steps:
HTML::queue_a_task(HTML::Task::Source::WebSocket, nullptr, nullptr, GC::create_function(GC::Heap::the(), [this, message = move(message), is_text] mutable {
// 1. If ready state is not OPEN (1), then return.
- if (m_websocket->ready_state() != Requests::WebSocket::ReadyState::Open)
+ if (m_ready_state != Requests::WebSocket::ReadyState::Open)
return;
auto& realm = HTML::relevant_realm(relevant_global_object());
@@ -407,11 +419,30 @@ void WebSocket::on_message(ByteBuffer message, bool is_text)
}));
}
+// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
+void WebSocket::on_closing_handshake_started()
+{
+ // When the WebSocket closing handshake is started, the user agent must queue a task to change the ready state to
+ // CLOSING (2). (If the close() method was called, the ready state will already be set to CLOSING (2) when this task
+ // runs.) [WSP]
+ HTML::queue_a_task(HTML::Task::Source::WebSocket, nullptr, nullptr, GC::create_function(GC::Heap::the(), [this] {
+ // AD-HOC: Matching other engines, a WebSocket that an error event has already closed stays CLOSED.
+ if (m_ready_state == Requests::WebSocket::ReadyState::Closed)
+ return;
+ set_ready_state(Requests::WebSocket::ReadyState::Closing);
+ }));
+}
+
// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
void WebSocket::on_error()
{
// When the WebSocket connection is closed, possibly cleanly, the user agent must queue a task to run the following substeps:
HTML::queue_a_task(HTML::Task::Source::WebSocket, nullptr, nullptr, GC::create_function(GC::Heap::the(), [this] {
+ // 1. Change the ready state to CLOSED (3).
+ set_ready_state(Requests::WebSocket::ReadyState::Closed);
+
+ // 2. If the user agent was required to fail the WebSocket connection, or if the WebSocket connection was
+ // closed after being flagged as full, fire an event named error at the WebSocket object. [WSP]
dispatch_event(DOM::Event::create(HTML::EventNames::error, HighResolutionTime::current_high_resolution_time(relevant_global_object())));
}));
}
@@ -419,11 +450,11 @@ void WebSocket::on_error()
// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
void WebSocket::on_close(u16 code, Utf16String reason, bool was_clean)
{
- update_activity_root();
-
// When the WebSocket connection is closed, possibly cleanly, the user agent must queue a task to run the following substeps:
HTML::queue_a_task(HTML::Task::Source::WebSocket, nullptr, nullptr, GC::create_function(GC::Heap::the(), [this, code, reason = move(reason), was_clean] {
- // 1. Change the readyState attribute's value to CLOSED. This is handled by the Protocol's WebSocket
+ // 1. Change the ready state to CLOSED (3).
+ set_ready_state(Requests::WebSocket::ReadyState::Closed);
+
// 2. If [needed], fire an event named error at the WebSocket object. This is handled by the Protocol's WebSocket
HTML::CloseEventInit event_init {};
event_init.was_clean = was_clean;
@@ -446,7 +477,7 @@ void WebSocket::make_disappear()
// - Do nothing.
// NOTE: All of these are handled by the WebSocket Protocol when calling close()
auto ready_state = this->ready_state();
- if (ready_state == Requests::WebSocket::ReadyState::Closing || ready_state == Requests::WebSocket::ReadyState::Closed) {
+ if (!m_websocket || ready_state == Requests::WebSocket::ReadyState::Closing || ready_state == Requests::WebSocket::ReadyState::Closed) {
m_activity_root.release();
return;
}
diff --git a/Libraries/LibWeb/WebSockets/WebSocket.h b/Libraries/LibWeb/WebSockets/WebSocket.h
index 43bfef9c320..b483b0b53f0 100644
--- a/Libraries/LibWeb/WebSockets/WebSocket.h
+++ b/Libraries/LibWeb/WebSockets/WebSocket.h
@@ -67,9 +67,12 @@ public:
private:
void on_open();
void on_message(ByteBuffer message, bool is_text);
+ void on_closing_handshake_started();
void on_error();
void on_close(u16 code, Utf16String reason, bool was_clean);
+ void set_ready_state(Requests::WebSocket::ReadyState);
+
JS::Object& relevant_global_object() const;
WebSocket(GC::Ref<DOM::EventTarget> relevant_global_object);
@@ -84,6 +87,7 @@ private:
void update_activity_root();
URL::URL m_url;
+ Requests::WebSocket::ReadyState m_ready_state { Requests::WebSocket::ReadyState::Connecting };
Bindings::BinaryType m_binary_type { Bindings::BinaryType::Blob };
RefPtr<Requests::WebSocket> m_websocket;
GC::Ref<DOM::EventTarget> m_global_object;
diff --git a/Tests/LibWeb/Text/expected/WebSocket/close-while-connecting.txt b/Tests/LibWeb/Text/expected/WebSocket/close-while-connecting.txt
new file mode 100644
index 00000000000..03b50f5bd0e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/WebSocket/close-while-connecting.txt
@@ -0,0 +1,4 @@
+readyState after construction: 0
+readyState after close(): 2
+error, readyState: 3
+close, readyState: 3, wasClean: false, code: 1006
diff --git a/Tests/LibWeb/Text/input/WebSocket/close-while-connecting.html b/Tests/LibWeb/Text/input/WebSocket/close-while-connecting.html
new file mode 100644
index 00000000000..3fb2efce547
--- /dev/null
+++ b/Tests/LibWeb/Text/input/WebSocket/close-while-connecting.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(done => {
+ const ws = new WebSocket(`ws://localhost:${internals.getEchoServerPort()}/`);
+ println(`readyState after construction: ${ws.readyState}`);
+ ws.close();
+ println(`readyState after close(): ${ws.readyState}`);
+
+ ws.onopen = () => {
+ println("open");
+ ws.close();
+ };
+ ws.onerror = () => {
+ println(`error, readyState: ${ws.readyState}`);
+ };
+ ws.onclose = event => {
+ println(`close, readyState: ${ws.readyState}, wasClean: ${event.wasClean}, code: ${event.code}`);
+ done();
+ };
+ });
+</script>