RequestServer: Make WebSocket IPC APIs asynchronous

This fixes deadlocking when interacting with WebSockets while
RequestServer is trying to stream downloaded data to WebContent.
This commit is contained in:
Andreas Kling 2024-09-18 10:28:55 +02:00 committed by Andreas Kling
commit e205723b95
Notes: github-actions[bot] 2024-09-19 05:38:37 +00:00
10 changed files with 129 additions and 94 deletions

View file

@ -96,45 +96,61 @@ void RequestClient::certificate_requested(i32 request_id)
RefPtr<WebSocket> RequestClient::websocket_connect(const URL::URL& url, ByteString const& origin, Vector<ByteString> const& protocols, Vector<ByteString> const& extensions, HTTP::HeaderMap const& request_headers)
{
auto connection_id = IPCProxy::websocket_connect(url, origin, protocols, extensions, request_headers);
if (connection_id < 0)
return nullptr;
auto connection = WebSocket::create_from_id({}, *this, connection_id);
m_websockets.set(connection_id, connection);
auto websocket_id = m_next_websocket_id++;
IPCProxy::async_websocket_connect(websocket_id, url, origin, protocols, extensions, request_headers);
auto connection = WebSocket::create_from_id({}, *this, websocket_id);
m_websockets.set(websocket_id, connection);
return connection;
}
void RequestClient::websocket_connected(i32 connection_id)
void RequestClient::websocket_connected(i64 websocket_id)
{
auto maybe_connection = m_websockets.get(connection_id);
auto maybe_connection = m_websockets.get(websocket_id);
if (maybe_connection.has_value())
maybe_connection.value()->did_open({});
}
void RequestClient::websocket_received(i32 connection_id, bool is_text, ByteBuffer const& data)
void RequestClient::websocket_received(i64 websocket_id, bool is_text, ByteBuffer const& data)
{
auto maybe_connection = m_websockets.get(connection_id);
auto maybe_connection = m_websockets.get(websocket_id);
if (maybe_connection.has_value())
maybe_connection.value()->did_receive({}, data, is_text);
}
void RequestClient::websocket_errored(i32 connection_id, i32 message)
void RequestClient::websocket_errored(i64 websocket_id, i32 message)
{
auto maybe_connection = m_websockets.get(connection_id);
auto maybe_connection = m_websockets.get(websocket_id);
if (maybe_connection.has_value())
maybe_connection.value()->did_error({}, message);
}
void RequestClient::websocket_closed(i32 connection_id, u16 code, ByteString const& reason, bool clean)
void RequestClient::websocket_closed(i64 websocket_id, u16 code, ByteString const& reason, bool clean)
{
auto maybe_connection = m_websockets.get(connection_id);
auto maybe_connection = m_websockets.get(websocket_id);
if (maybe_connection.has_value())
maybe_connection.value()->did_close({}, code, reason, clean);
}
void RequestClient::websocket_certificate_requested(i32 connection_id)
void RequestClient::websocket_ready_state_changed(i64 websocket_id, u32 ready_state)
{
auto maybe_connection = m_websockets.get(connection_id);
auto maybe_connection = m_websockets.get(websocket_id);
if (maybe_connection.has_value()) {
VERIFY(ready_state <= static_cast<u32>(WebSocket::ReadyState::Closed));
maybe_connection.value()->set_ready_state(static_cast<WebSocket::ReadyState>(ready_state));
}
}
void RequestClient::websocket_subprotocol(i64 websocket_id, ByteString const& subprotocol)
{
auto maybe_connection = m_websockets.get(websocket_id);
if (maybe_connection.has_value()) {
maybe_connection.value()->set_subprotocol_in_use(subprotocol);
}
}
void RequestClient::websocket_certificate_requested(i64 websocket_id)
{
auto maybe_connection = m_websockets.get(websocket_id);
if (maybe_connection.has_value())
maybe_connection.value()->did_request_certificates({});
}

View file

@ -44,14 +44,18 @@ private:
virtual void certificate_requested(i32) override;
virtual void headers_became_available(i32, HTTP::HeaderMap const&, Optional<u32> const&) override;
virtual void websocket_connected(i32) override;
virtual void websocket_received(i32, bool, ByteBuffer const&) override;
virtual void websocket_errored(i32, i32) override;
virtual void websocket_closed(i32, u16, ByteString const&, bool) override;
virtual void websocket_certificate_requested(i32) override;
virtual void websocket_connected(i64 websocket_id) override;
virtual void websocket_received(i64 websocket_id, bool, ByteBuffer const&) override;
virtual void websocket_errored(i64 websocket_id, i32) override;
virtual void websocket_closed(i64 websocket_id, u16, ByteString const&, bool) override;
virtual void websocket_ready_state_changed(i64 websocket_id, u32 ready_state) override;
virtual void websocket_subprotocol(i64 websocket_id, ByteString const& subprotocol) override;
virtual void websocket_certificate_requested(i64 websocket_id) override;
HashMap<i32, RefPtr<Request>> m_requests;
HashMap<i32, NonnullRefPtr<WebSocket>> m_websockets;
HashMap<i64, NonnullRefPtr<WebSocket>> m_websockets;
i64 m_next_websocket_id { 0 };
};
}

View file

@ -9,25 +9,35 @@
namespace Requests {
WebSocket::WebSocket(RequestClient& client, i32 connection_id)
WebSocket::WebSocket(RequestClient& client, i64 connection_id)
: m_client(client)
, m_connection_id(connection_id)
, m_websocket_id(connection_id)
{
}
WebSocket::ReadyState WebSocket::ready_state()
{
return static_cast<WebSocket::ReadyState>(m_client->websocket_ready_state(m_connection_id));
return m_ready_state;
}
void WebSocket::set_ready_state(ReadyState ready_state)
{
m_ready_state = ready_state;
}
ByteString WebSocket::subprotocol_in_use()
{
return m_client->websocket_subprotocol_in_use(m_connection_id);
return m_subprotocol;
}
void WebSocket::set_subprotocol_in_use(ByteString subprotocol)
{
m_subprotocol = move(subprotocol);
}
void WebSocket::send(ByteBuffer binary_or_text_message, bool is_text)
{
m_client->async_websocket_send(m_connection_id, is_text, move(binary_or_text_message));
m_client->async_websocket_send(m_websocket_id, is_text, move(binary_or_text_message));
}
void WebSocket::send(StringView text_message)
@ -37,7 +47,7 @@ void WebSocket::send(StringView text_message)
void WebSocket::close(u16 code, ByteString reason)
{
m_client->async_websocket_close(m_connection_id, code, move(reason));
m_client->async_websocket_close(m_websocket_id, code, move(reason));
}
void WebSocket::did_open(Badge<RequestClient>)
@ -68,7 +78,7 @@ void WebSocket::did_request_certificates(Badge<RequestClient>)
{
if (on_certificate_requested) {
auto result = on_certificate_requested();
if (!m_client->websocket_set_certificate(m_connection_id, result.certificate, result.key))
if (!m_client->websocket_set_certificate(m_websocket_id, result.certificate, result.key))
dbgln("WebSocket: set_certificate failed");
}
}

View file

@ -44,16 +44,18 @@ public:
Closed = 3,
};
static NonnullRefPtr<WebSocket> create_from_id(Badge<RequestClient>, RequestClient& client, i32 connection_id)
static NonnullRefPtr<WebSocket> create_from_id(Badge<RequestClient>, RequestClient& client, i64 websocket_id)
{
return adopt_ref(*new WebSocket(client, connection_id));
return adopt_ref(*new WebSocket(client, websocket_id));
}
int id() const { return m_connection_id; }
i64 id() const { return m_websocket_id; }
ReadyState ready_state();
void set_ready_state(ReadyState);
ByteString subprotocol_in_use();
void set_subprotocol_in_use(ByteString);
void send(ByteBuffer binary_or_text_message, bool is_text);
void send(StringView text_message);
@ -72,9 +74,11 @@ public:
void did_request_certificates(Badge<RequestClient>);
private:
explicit WebSocket(RequestClient&, i32 connection_id);
explicit WebSocket(RequestClient&, i64 websocket_id);
WeakPtr<RequestClient> m_client;
int m_connection_id { -1 };
ReadyState m_ready_state { ReadyState::Connecting };
ByteString m_subprotocol;
i64 m_websocket_id { -1 };
};
}