mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 09:07:45 +00:00
Everywhere: Merge the WebSocket service into RequestServer
This keeps the APIs separate as they are wildly different, a future improvement could be to somehow unify the APIs (if possible). Closes #23080.
This commit is contained in:
parent
daf5484d6b
commit
6dfb2f9dc8
56 changed files with 231 additions and 845 deletions
|
@ -26,4 +26,4 @@ set(GENERATED_SOURCES
|
|||
)
|
||||
|
||||
serenity_bin(RequestServer)
|
||||
target_link_libraries(RequestServer PRIVATE LibCore LibCrypto LibIPC LibGemini LibHTTP LibMain LibTLS)
|
||||
target_link_libraries(RequestServer PRIVATE LibCore LibCrypto LibIPC LibGemini LibHTTP LibMain LibTLS LibWebSocket)
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#include <AK/Weakable.h>
|
||||
#include <LibCore/Proxy.h>
|
||||
#include <LibCore/Socket.h>
|
||||
#include <LibWebSocket/ConnectionInfo.h>
|
||||
#include <LibWebSocket/Message.h>
|
||||
#include <RequestServer/ConnectionFromClient.h>
|
||||
#include <RequestServer/Protocol.h>
|
||||
#include <RequestServer/Request.h>
|
||||
|
@ -187,4 +189,80 @@ void ConnectionFromClient::ensure_connection(URL const& url, ::RequestServer::Ca
|
|||
dbgln("EnsureConnection: Invalid URL scheme: '{}'", url.scheme());
|
||||
}
|
||||
|
||||
static i32 s_next_websocket_id = 1;
|
||||
Messages::RequestServer::WebsocketConnectResponse ConnectionFromClient::websocket_connect(URL const& url, ByteString const& origin, Vector<ByteString> const& protocols, Vector<ByteString> const& extensions, HashMap<ByteString, ByteString> const& additional_request_headers)
|
||||
{
|
||||
if (!url.is_valid()) {
|
||||
dbgln("WebSocket::Connect: Invalid URL requested: '{}'", url);
|
||||
return -1;
|
||||
}
|
||||
|
||||
WebSocket::ConnectionInfo connection_info(url);
|
||||
connection_info.set_origin(origin);
|
||||
connection_info.set_protocols(protocols);
|
||||
connection_info.set_extensions(extensions);
|
||||
|
||||
Vector<WebSocket::ConnectionInfo::Header> headers;
|
||||
for (auto const& header : additional_request_headers) {
|
||||
headers.append({ header.key, header.value });
|
||||
}
|
||||
connection_info.set_headers(headers);
|
||||
|
||||
auto id = ++s_next_websocket_id;
|
||||
auto connection = WebSocket::WebSocket::create(move(connection_info));
|
||||
connection->on_open = [this, id]() {
|
||||
async_websocket_connected(id);
|
||||
};
|
||||
connection->on_message = [this, id](auto message) {
|
||||
async_websocket_received(id, message.is_text(), message.data());
|
||||
};
|
||||
connection->on_error = [this, id](auto message) {
|
||||
async_websocket_errored(id, (i32)message);
|
||||
};
|
||||
connection->on_close = [this, id](u16 code, ByteString reason, bool was_clean) {
|
||||
async_websocket_closed(id, code, move(reason), was_clean);
|
||||
};
|
||||
|
||||
connection->start();
|
||||
m_websockets.set(id, move(connection));
|
||||
return id;
|
||||
}
|
||||
|
||||
Messages::RequestServer::WebsocketReadyStateResponse ConnectionFromClient::websocket_ready_state(i32 connection_id)
|
||||
{
|
||||
if (auto connection = m_websockets.get(connection_id).value_or({}))
|
||||
return (u32)connection->ready_state();
|
||||
return (u32)WebSocket::ReadyState::Closed;
|
||||
}
|
||||
|
||||
Messages::RequestServer::WebsocketSubprotocolInUseResponse ConnectionFromClient::websocket_subprotocol_in_use(i32 connection_id)
|
||||
{
|
||||
if (auto connection = m_websockets.get(connection_id).value_or({}))
|
||||
return connection->subprotocol_in_use();
|
||||
return ByteString::empty();
|
||||
}
|
||||
|
||||
void ConnectionFromClient::websocket_send(i32 connection_id, bool is_text, ByteBuffer const& data)
|
||||
{
|
||||
if (auto connection = m_websockets.get(connection_id).value_or({}); connection && connection->ready_state() == WebSocket::ReadyState::Open)
|
||||
connection->send(WebSocket::Message { data, is_text });
|
||||
}
|
||||
|
||||
void ConnectionFromClient::websocket_close(i32 connection_id, u16 code, ByteString const& reason)
|
||||
{
|
||||
if (auto connection = m_websockets.get(connection_id).value_or({}); connection && connection->ready_state() == WebSocket::ReadyState::Open)
|
||||
connection->close(code, reason);
|
||||
}
|
||||
|
||||
Messages::RequestServer::WebsocketSetCertificateResponse ConnectionFromClient::websocket_set_certificate(i32 connection_id, ByteString const&, ByteString const&)
|
||||
{
|
||||
auto success = false;
|
||||
if (auto connection = m_websockets.get(connection_id).value_or({}); connection) {
|
||||
// NO OP here
|
||||
// connection->set_certificate(certificate, key);
|
||||
success = true;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include <AK/HashMap.h>
|
||||
#include <LibIPC/ConnectionFromClient.h>
|
||||
#include <LibWebSocket/WebSocket.h>
|
||||
#include <RequestServer/Forward.h>
|
||||
#include <RequestServer/RequestClientEndpoint.h>
|
||||
#include <RequestServer/RequestServerEndpoint.h>
|
||||
|
@ -37,7 +38,15 @@ private:
|
|||
virtual Messages::RequestServer::SetCertificateResponse set_certificate(i32, ByteString const&, ByteString const&) override;
|
||||
virtual void ensure_connection(URL const& url, ::RequestServer::CacheLevel const& cache_level) override;
|
||||
|
||||
virtual Messages::RequestServer::WebsocketConnectResponse websocket_connect(URL const&, ByteString const&, Vector<ByteString> const&, Vector<ByteString> const&, HashMap<ByteString, ByteString> const&) override;
|
||||
virtual Messages::RequestServer::WebsocketReadyStateResponse websocket_ready_state(i32) override;
|
||||
virtual Messages::RequestServer::WebsocketSubprotocolInUseResponse websocket_subprotocol_in_use(i32) override;
|
||||
virtual void websocket_send(i32, bool, ByteBuffer const&) override;
|
||||
virtual void websocket_close(i32, u16, ByteString const&) override;
|
||||
virtual Messages::RequestServer::WebsocketSetCertificateResponse websocket_set_certificate(i32, ByteString const&, ByteString const&) override;
|
||||
|
||||
HashMap<i32, OwnPtr<Request>> m_requests;
|
||||
HashMap<i32, RefPtr<WebSocket::WebSocket>> m_websockets;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -7,6 +7,14 @@ endpoint RequestClient
|
|||
request_finished(i32 request_id, bool success, u64 total_size) =|
|
||||
headers_became_available(i32 request_id, HashMap<ByteString,ByteString,CaseInsensitiveStringTraits> response_headers, Optional<u32> status_code) =|
|
||||
|
||||
// Websocket API
|
||||
// FIXME: See if this can be merged with the regular APIs
|
||||
websocket_connected(i32 connection_id) =|
|
||||
websocket_received(i32 connection_id, bool is_text, ByteBuffer data) =|
|
||||
websocket_errored(i32 connection_id, i32 message) =|
|
||||
websocket_closed(i32 connection_id, u16 code, ByteString reason, bool clean) =|
|
||||
websocket_certificate_requested(i32 request_id) =|
|
||||
|
||||
// Certificate requests
|
||||
certificate_requested(i32 request_id) =|
|
||||
}
|
||||
|
|
|
@ -11,4 +11,12 @@ endpoint RequestServer
|
|||
set_certificate(i32 request_id, ByteString certificate, ByteString key) => (bool success)
|
||||
|
||||
ensure_connection(URL url, ::RequestServer::CacheLevel cache_level) =|
|
||||
|
||||
// Websocket Connection API
|
||||
websocket_connect(URL url, ByteString origin, Vector<ByteString> protocols, Vector<ByteString> extensions, HashMap<ByteString,ByteString> additional_request_headers) => (i32 connection_id)
|
||||
websocket_ready_state(i32 connection_id) => (u32 ready_state)
|
||||
websocket_subprotocol_in_use(i32 connection_id) => (ByteString subprotocol_in_use)
|
||||
websocket_send(i32 connection_id, bool is_text, ByteBuffer data) =|
|
||||
websocket_close(i32 connection_id, u16 code, ByteString reason) =|
|
||||
websocket_set_certificate(i32 request_id, ByteString certificate, ByteString key) => (bool success)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue