2021-04-23 17:45:52 -03:00
|
|
|
/*
|
2024-09-05 08:20:09 -03:00
|
|
|
* Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
|
2021-04-23 17:45:52 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2024-04-10 19:02:40 -03:00
|
|
|
#include <AK/IDAllocator.h>
|
2022-03-17 13:03:13 -03:00
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2026-02-25 05:44:32 -03:00
|
|
|
#include <AK/WeakPtr.h>
|
2024-09-19 02:54:24 -03:00
|
|
|
#include <LibCore/EventLoop.h>
|
2022-04-07 13:40:33 -03:00
|
|
|
#include <LibCore/Proxy.h>
|
2024-01-29 06:20:58 -03:00
|
|
|
#include <LibCore/Socket.h>
|
2025-07-07 12:03:37 -03:00
|
|
|
#include <LibCore/StandardPaths.h>
|
2025-12-04 11:59:18 -03:00
|
|
|
#include <LibCore/System.h>
|
2025-11-28 12:04:59 -03:00
|
|
|
#include <LibHTTP/Cache/DiskCache.h>
|
2026-03-11 16:10:08 -03:00
|
|
|
#include <LibIPC/TransportHandle.h>
|
2025-02-20 09:21:22 -03:00
|
|
|
#include <LibRequests/WebSocket.h>
|
2024-03-05 21:50:52 -03:00
|
|
|
#include <LibWebSocket/ConnectionInfo.h>
|
|
|
|
|
#include <LibWebSocket/Message.h>
|
2025-10-23 16:21:33 -03:00
|
|
|
#include <RequestServer/CURL.h>
|
2022-02-25 07:18:30 -03:00
|
|
|
#include <RequestServer/ConnectionFromClient.h>
|
2025-10-23 21:44:55 -03:00
|
|
|
#include <RequestServer/Request.h>
|
2025-10-22 20:26:17 -03:00
|
|
|
#include <RequestServer/Resolver.h>
|
|
|
|
|
#include <RequestServer/WebSocketImplCurl.h>
|
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 20:59:21 -03:00
|
|
|
|
2021-04-23 17:45:52 -03:00
|
|
|
namespace RequestServer {
|
|
|
|
|
|
2026-02-07 16:06:22 -03:00
|
|
|
static ConnectionFromClient* g_primary_connection = nullptr;
|
2024-09-05 08:20:09 -03:00
|
|
|
static IDAllocator s_client_ids;
|
2025-10-22 20:26:17 -03:00
|
|
|
|
2026-04-26 01:56:19 -03:00
|
|
|
static constexpr i64 TICK_GAP_THRESHOLD_MS = 100;
|
|
|
|
|
static Optional<MonotonicTime> s_last_tick_at;
|
|
|
|
|
static StringView s_last_tick_label;
|
|
|
|
|
|
|
|
|
|
// When libcurl asks us (via on_timeout_callback) to wake it up after N ms, we record when. If `curl-timer-fired`
|
|
|
|
|
// then runs close to that time the gap is by design (libcurl's heartbeat) and we suppress the wire-stall log.
|
|
|
|
|
static Optional<MonotonicTime> s_curl_timer_due_at;
|
|
|
|
|
static constexpr i64 CURL_TIMER_ON_TIME_TOLERANCE_MS = 50;
|
|
|
|
|
|
|
|
|
|
static void note_event_tick(StringView label)
|
|
|
|
|
{
|
|
|
|
|
if constexpr (!REQUESTSERVER_WIRE_DEBUG)
|
|
|
|
|
return;
|
|
|
|
|
auto now = MonotonicTime::now();
|
|
|
|
|
if (s_last_tick_at.has_value()) {
|
|
|
|
|
auto gap = (now - *s_last_tick_at).to_milliseconds();
|
|
|
|
|
if (gap > TICK_GAP_THRESHOLD_MS) {
|
|
|
|
|
bool curl_timer_fired_on_schedule = false;
|
|
|
|
|
if (label == "curl-timer-fired"sv && s_curl_timer_due_at.has_value()) {
|
|
|
|
|
auto overshoot_ms = (now - *s_curl_timer_due_at).to_milliseconds();
|
|
|
|
|
if (overshoot_ms >= -CURL_TIMER_ON_TIME_TOLERANCE_MS && overshoot_ms <= CURL_TIMER_ON_TIME_TOLERANCE_MS)
|
|
|
|
|
curl_timer_fired_on_schedule = true;
|
|
|
|
|
}
|
|
|
|
|
if (!curl_timer_fired_on_schedule) {
|
|
|
|
|
dbgln("RequestServer wire-stall: {} ms event-loop gap before '{}' (previous handler: '{}')",
|
|
|
|
|
gap, label, s_last_tick_label);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
s_last_tick_at = now;
|
|
|
|
|
s_last_tick_label = label;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static constexpr i64 CURL_CALL_THRESHOLD_MS = 50;
|
|
|
|
|
template<typename F>
|
|
|
|
|
static auto time_curl_call(StringView label, F&& f)
|
|
|
|
|
{
|
|
|
|
|
if constexpr (!REQUESTSERVER_WIRE_DEBUG)
|
|
|
|
|
return f();
|
|
|
|
|
auto start = MonotonicTime::now();
|
|
|
|
|
auto result = f();
|
|
|
|
|
auto elapsed_ms = (MonotonicTime::now() - start).to_milliseconds();
|
|
|
|
|
if (elapsed_ms > CURL_CALL_THRESHOLD_MS)
|
|
|
|
|
dbgln("RequestServer wire-stall: curl call '{}' took {} ms (synchronous in event loop)", label, elapsed_ms);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Per-client burst-of-requests counter. Tracks how many `start_request` IPC
|
|
|
|
|
// calls land in a tight window, so we can see if WebContent is dumping a
|
|
|
|
|
// page worth of requests on us in one shot. State lives on ConnectionFromClient.
|
|
|
|
|
static constexpr i64 BURST_WINDOW_MS = 100;
|
|
|
|
|
static constexpr u64 BURST_REPORT_THRESHOLD = 5;
|
|
|
|
|
|
2026-02-15 11:23:55 -03:00
|
|
|
ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<IPC::Transport> transport, IsPrimaryConnection is_primary_connection, ConnectionMap& connections, Optional<HTTP::DiskCache&> disk_cache)
|
2024-10-22 18:47:33 -03:00
|
|
|
: IPC::ConnectionFromClient<RequestClientEndpoint, RequestServerEndpoint>(*this, move(transport), s_client_ids.allocate())
|
2026-02-15 11:23:55 -03:00
|
|
|
, m_connections(connections)
|
|
|
|
|
, m_disk_cache(disk_cache)
|
|
|
|
|
, m_curl_multi(curl_multi_init())
|
2025-10-22 20:26:17 -03:00
|
|
|
, m_resolver(Resolver::default_resolver())
|
2026-02-15 11:23:55 -03:00
|
|
|
, m_alt_svc_cache_path(ByteString::formatted("{}/Ladybird/alt-svc-cache.txt", Core::StandardPaths::cache_directory()))
|
2021-04-23 17:45:52 -03:00
|
|
|
{
|
2026-02-15 11:23:55 -03:00
|
|
|
if (is_primary_connection == IsPrimaryConnection::Yes) {
|
|
|
|
|
VERIFY(g_primary_connection == nullptr);
|
|
|
|
|
g_primary_connection = this;
|
|
|
|
|
}
|
2025-07-07 12:03:37 -03:00
|
|
|
|
2026-02-15 11:23:55 -03:00
|
|
|
m_connections.set(client_id(), *this);
|
2024-09-05 08:20:09 -03:00
|
|
|
|
|
|
|
|
auto set_option = [this](auto option, auto value) {
|
|
|
|
|
auto result = curl_multi_setopt(m_curl_multi, option, value);
|
|
|
|
|
VERIFY(result == CURLM_OK);
|
|
|
|
|
};
|
|
|
|
|
set_option(CURLMOPT_SOCKETFUNCTION, &on_socket_callback);
|
|
|
|
|
set_option(CURLMOPT_SOCKETDATA, this);
|
|
|
|
|
set_option(CURLMOPT_TIMERFUNCTION, &on_timeout_callback);
|
|
|
|
|
set_option(CURLMOPT_TIMERDATA, this);
|
|
|
|
|
|
|
|
|
|
m_timer = Core::Timer::create_single_shot(0, [this] {
|
2026-04-26 01:56:19 -03:00
|
|
|
note_event_tick("curl-timer-fired"sv);
|
|
|
|
|
s_curl_timer_due_at = {};
|
|
|
|
|
auto result = time_curl_call("multi_socket_action(timeout)"sv, [this] {
|
|
|
|
|
return curl_multi_socket_action(m_curl_multi, CURL_SOCKET_TIMEOUT, 0, nullptr);
|
|
|
|
|
});
|
2024-09-05 08:20:09 -03:00
|
|
|
VERIFY(result == CURLM_OK);
|
|
|
|
|
check_active_requests();
|
|
|
|
|
});
|
2021-04-23 17:45:52 -03:00
|
|
|
}
|
|
|
|
|
|
2024-07-27 19:22:58 -03:00
|
|
|
ConnectionFromClient::~ConnectionFromClient()
|
|
|
|
|
{
|
2024-12-25 13:33:23 -03:00
|
|
|
m_active_requests.clear();
|
2025-12-12 10:33:17 -03:00
|
|
|
m_active_revalidation_requests.clear();
|
2026-04-17 08:31:36 -03:00
|
|
|
m_pending_websockets.clear();
|
|
|
|
|
m_websockets.clear();
|
2024-12-25 13:33:23 -03:00
|
|
|
|
|
|
|
|
curl_multi_cleanup(m_curl_multi);
|
|
|
|
|
m_curl_multi = nullptr;
|
2024-07-27 19:22:58 -03:00
|
|
|
}
|
|
|
|
|
|
2026-02-07 16:06:22 -03:00
|
|
|
Optional<ConnectionFromClient&> ConnectionFromClient::primary_connection()
|
|
|
|
|
{
|
|
|
|
|
if (g_primary_connection)
|
|
|
|
|
return *g_primary_connection;
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-12 10:33:17 -03:00
|
|
|
void ConnectionFromClient::request_complete(Badge<Request>, Request const& request)
|
2025-10-23 21:44:55 -03:00
|
|
|
{
|
2025-12-12 10:33:17 -03:00
|
|
|
Core::deferred_invoke([weak_self = make_weak_ptr<ConnectionFromClient>(), request_id = request.request_id(), type = request.type()] {
|
|
|
|
|
if (auto self = weak_self.strong_ref()) {
|
2026-04-13 17:43:32 -03:00
|
|
|
if (type == RequestType::BackgroundRevalidation)
|
2025-12-12 10:33:17 -03:00
|
|
|
self->m_active_revalidation_requests.remove(request_id);
|
|
|
|
|
else
|
|
|
|
|
self->m_active_requests.remove(request_id);
|
|
|
|
|
}
|
2025-10-23 21:44:55 -03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 07:18:30 -03:00
|
|
|
void ConnectionFromClient::die()
|
2021-04-23 17:45:52 -03:00
|
|
|
{
|
2026-02-07 16:06:22 -03:00
|
|
|
if (g_primary_connection == this)
|
|
|
|
|
g_primary_connection = nullptr;
|
|
|
|
|
|
2024-04-10 19:02:40 -03:00
|
|
|
auto client_id = this->client_id();
|
2026-02-15 11:23:55 -03:00
|
|
|
m_connections.remove(client_id);
|
2024-04-10 19:02:40 -03:00
|
|
|
s_client_ids.deallocate(client_id);
|
|
|
|
|
|
2026-02-15 11:23:55 -03:00
|
|
|
if (m_connections.is_empty())
|
2021-04-23 17:45:52 -03:00
|
|
|
Core::EventLoop::current().quit(0);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-03 13:19:46 -03:00
|
|
|
Messages::RequestServer::InitTransportResponse ConnectionFromClient::init_transport([[maybe_unused]] int peer_pid)
|
|
|
|
|
{
|
|
|
|
|
#ifdef AK_OS_WINDOWS
|
2025-06-28 07:24:38 -03:00
|
|
|
m_transport->set_peer_pid(peer_pid);
|
2025-01-03 13:19:46 -03:00
|
|
|
return Core::System::getpid();
|
|
|
|
|
#endif
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 19:02:40 -03:00
|
|
|
Messages::RequestServer::ConnectNewClientResponse ConnectionFromClient::connect_new_client()
|
2025-08-09 14:04:48 -03:00
|
|
|
{
|
|
|
|
|
auto client_socket = create_client_socket();
|
|
|
|
|
if (client_socket.is_error()) {
|
|
|
|
|
dbgln("Failed to create client socket: {}", client_socket.error());
|
2026-03-11 16:10:08 -03:00
|
|
|
return IPC::TransportHandle {};
|
2025-08-09 14:04:48 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return client_socket.release_value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Messages::RequestServer::ConnectNewClientsResponse ConnectionFromClient::connect_new_clients(size_t count)
|
|
|
|
|
{
|
2026-03-11 16:10:08 -03:00
|
|
|
Vector<IPC::TransportHandle> handles;
|
|
|
|
|
handles.ensure_capacity(count);
|
2025-08-09 14:04:48 -03:00
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
|
auto client_socket = create_client_socket();
|
|
|
|
|
if (client_socket.is_error()) {
|
|
|
|
|
dbgln("Failed to create client socket: {}", client_socket.error());
|
2026-03-11 16:10:08 -03:00
|
|
|
return Vector<IPC::TransportHandle> {};
|
2025-08-09 14:04:48 -03:00
|
|
|
}
|
|
|
|
|
|
2026-03-11 16:10:08 -03:00
|
|
|
handles.unchecked_append(client_socket.release_value());
|
2025-08-09 14:04:48 -03:00
|
|
|
}
|
|
|
|
|
|
2026-03-11 16:10:08 -03:00
|
|
|
return handles;
|
2025-08-09 14:04:48 -03:00
|
|
|
}
|
|
|
|
|
|
2026-03-11 16:10:08 -03:00
|
|
|
ErrorOr<IPC::TransportHandle> ConnectionFromClient::create_client_socket()
|
2024-04-10 19:02:40 -03:00
|
|
|
{
|
2026-03-11 09:05:09 -03:00
|
|
|
auto paired = TRY(IPC::Transport::create_paired());
|
2026-03-14 13:34:46 -03:00
|
|
|
auto handle = move(paired.remote_handle);
|
2025-08-09 14:04:48 -03:00
|
|
|
|
2026-02-15 11:23:55 -03:00
|
|
|
// Note: A ref is stored in the m_connections map
|
2026-03-11 09:05:09 -03:00
|
|
|
auto client = adopt_ref(*new ConnectionFromClient(move(paired.local), IsPrimaryConnection::No, m_connections, m_disk_cache));
|
2024-04-10 19:02:40 -03:00
|
|
|
|
2026-03-11 16:10:08 -03:00
|
|
|
return handle;
|
2024-04-10 19:02:40 -03:00
|
|
|
}
|
|
|
|
|
|
2026-02-05 14:25:14 -03:00
|
|
|
void ConnectionFromClient::set_disk_cache_settings(HTTP::DiskCacheSettings disk_cache_settings)
|
|
|
|
|
{
|
2026-02-15 11:23:55 -03:00
|
|
|
if (m_disk_cache.has_value())
|
|
|
|
|
m_disk_cache->set_maximum_disk_cache_size(disk_cache_settings.maximum_size);
|
2026-02-05 14:25:14 -03:00
|
|
|
}
|
|
|
|
|
|
2025-03-08 14:22:39 -03:00
|
|
|
Messages::RequestServer::IsSupportedProtocolResponse ConnectionFromClient::is_supported_protocol(ByteString protocol)
|
2021-04-23 17:45:52 -03:00
|
|
|
{
|
2024-09-05 08:20:09 -03:00
|
|
|
return protocol == "http"sv || protocol == "https"sv;
|
2021-04-23 17:45:52 -03:00
|
|
|
}
|
|
|
|
|
|
2025-05-13 07:34:55 -03:00
|
|
|
void ConnectionFromClient::set_dns_server(ByteString host_or_address, u16 port, bool use_tls, bool validate_dnssec_locally)
|
2024-11-01 19:53:43 -03:00
|
|
|
{
|
2025-10-22 20:26:17 -03:00
|
|
|
auto& dns_info = DNSInfo::the();
|
|
|
|
|
|
|
|
|
|
if (host_or_address == dns_info.server_hostname && port == dns_info.port && use_tls == dns_info.use_dns_over_tls && validate_dnssec_locally == dns_info.validate_dnssec_locally)
|
2024-11-01 19:53:43 -03:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
auto result = [&] -> ErrorOr<void> {
|
|
|
|
|
Core::SocketAddress addr;
|
|
|
|
|
if (auto v4 = IPv4Address::from_string(host_or_address); v4.has_value())
|
|
|
|
|
addr = { v4.value(), port };
|
|
|
|
|
else if (auto v6 = IPv6Address::from_string(host_or_address); v6.has_value())
|
|
|
|
|
addr = { v6.value(), port };
|
|
|
|
|
else
|
2025-10-22 20:26:17 -03:00
|
|
|
TRY(m_resolver->dns.lookup(host_or_address)->await())->cached_addresses().first().visit([&](auto& address) { addr = { address, port }; });
|
2024-11-01 19:53:43 -03:00
|
|
|
|
2025-10-22 20:26:17 -03:00
|
|
|
dns_info.server_address = addr;
|
|
|
|
|
dns_info.server_hostname = host_or_address;
|
|
|
|
|
dns_info.port = port;
|
|
|
|
|
dns_info.use_dns_over_tls = use_tls;
|
|
|
|
|
dns_info.validate_dnssec_locally = validate_dnssec_locally;
|
2024-11-01 19:53:43 -03:00
|
|
|
return {};
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
if (result.is_error())
|
|
|
|
|
dbgln("Failed to set DNS server: {}", result.error());
|
|
|
|
|
else
|
2025-10-22 20:26:17 -03:00
|
|
|
m_resolver->dns.reset_connection();
|
2024-11-01 19:53:43 -03:00
|
|
|
}
|
|
|
|
|
|
2025-04-07 22:56:35 -03:00
|
|
|
void ConnectionFromClient::set_use_system_dns()
|
|
|
|
|
{
|
2025-10-22 20:26:17 -03:00
|
|
|
auto& dns_info = DNSInfo::the();
|
|
|
|
|
dns_info.server_hostname = {};
|
|
|
|
|
dns_info.server_address = {};
|
|
|
|
|
|
|
|
|
|
m_resolver->dns.reset_connection();
|
2025-04-07 22:56:35 -03:00
|
|
|
}
|
|
|
|
|
|
2026-02-07 16:06:22 -03:00
|
|
|
void ConnectionFromClient::start_request(u64 request_id, ByteString method, URL::URL url, Vector<HTTP::Header> request_headers, ByteBuffer request_body, HTTP::CacheMode cache_mode, HTTP::Cookie::IncludeCredentials include_credentials, Core::ProxyData proxy_data)
|
2021-04-23 17:45:52 -03:00
|
|
|
{
|
2026-04-26 01:56:19 -03:00
|
|
|
note_event_tick("ipc-start-request"sv);
|
2025-08-13 07:38:55 -03:00
|
|
|
dbgln_if(REQUESTSERVER_DEBUG, "RequestServer: start_request({}, {})", request_id, url);
|
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 20:59:21 -03:00
|
|
|
|
2026-04-26 01:56:19 -03:00
|
|
|
if constexpr (REQUESTSERVER_WIRE_DEBUG) {
|
|
|
|
|
auto now = MonotonicTime::now();
|
|
|
|
|
if (m_burst_window_started_at.has_value() && (now - *m_burst_window_started_at).to_milliseconds() < BURST_WINDOW_MS) {
|
|
|
|
|
++m_requests_in_burst_window;
|
|
|
|
|
} else {
|
|
|
|
|
if (m_requests_in_burst_window > BURST_REPORT_THRESHOLD) {
|
|
|
|
|
dbgln("RequestServer wire-burst: client {} sent {} requests in <{} ms",
|
|
|
|
|
client_id(), m_requests_in_burst_window, BURST_WINDOW_MS);
|
|
|
|
|
}
|
|
|
|
|
m_burst_window_started_at = now;
|
|
|
|
|
m_requests_in_burst_window = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-15 11:23:55 -03:00
|
|
|
auto request = Request::fetch(request_id, m_disk_cache, cache_mode, *this, m_curl_multi, m_resolver, move(url), move(method), HTTP::HeaderList::create(move(request_headers)), move(request_body), include_credentials, m_alt_svc_cache_path, proxy_data);
|
2025-10-23 21:44:55 -03:00
|
|
|
m_active_requests.set(request_id, move(request));
|
2025-10-15 15:59:25 -03:00
|
|
|
}
|
|
|
|
|
|
2026-02-07 16:06:22 -03:00
|
|
|
void ConnectionFromClient::start_revalidation_request(Badge<Request>, ByteString method, URL::URL url, NonnullRefPtr<HTTP::HeaderList> request_headers, ByteBuffer request_body, HTTP::Cookie::IncludeCredentials include_credentials, Core::ProxyData proxy_data)
|
2025-12-12 10:33:17 -03:00
|
|
|
{
|
2026-04-26 01:56:19 -03:00
|
|
|
note_event_tick("ipc-start-revalidation"sv);
|
2025-12-12 10:33:17 -03:00
|
|
|
auto request_id = m_next_revalidation_request_id++;
|
|
|
|
|
|
|
|
|
|
dbgln_if(REQUESTSERVER_DEBUG, "RequestServer: start_revalidation_request({}, {})", request_id, url);
|
|
|
|
|
|
2026-02-15 11:23:55 -03:00
|
|
|
auto request = Request::revalidate(request_id, m_disk_cache, *this, m_curl_multi, m_resolver, move(url), move(method), move(request_headers), move(request_body), include_credentials, m_alt_svc_cache_path, proxy_data);
|
2025-12-12 10:33:17 -03:00
|
|
|
m_active_revalidation_requests.set(request_id, move(request));
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-23 21:44:55 -03:00
|
|
|
int ConnectionFromClient::on_socket_callback(CURL*, int sockfd, int what, void* user_data, void*)
|
2025-10-15 15:59:25 -03:00
|
|
|
{
|
2025-10-23 21:44:55 -03:00
|
|
|
auto* client = static_cast<ConnectionFromClient*>(user_data);
|
2025-10-15 15:59:25 -03:00
|
|
|
|
2025-10-23 21:44:55 -03:00
|
|
|
if (what == CURL_POLL_REMOVE) {
|
|
|
|
|
client->m_read_notifiers.remove(sockfd);
|
|
|
|
|
client->m_write_notifiers.remove(sockfd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2024-11-24 21:04:29 -03:00
|
|
|
|
2026-04-21 09:58:37 -03:00
|
|
|
auto update_notifier = [client, sockfd, what](auto& notifiers, Core::NotificationType type, int poll_flag, int select_flag) {
|
|
|
|
|
if (!(what & poll_flag)) {
|
|
|
|
|
if (auto notifier = notifiers.get(sockfd); notifier.has_value())
|
|
|
|
|
notifier.value()->set_enabled(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto& notifier = notifiers.ensure(sockfd, [client, sockfd, multi = client->m_curl_multi, type, select_flag] {
|
|
|
|
|
auto notifier = Core::Notifier::construct(sockfd, type);
|
|
|
|
|
notifier->on_activation = [client, sockfd, multi, select_flag] {
|
2026-04-26 01:56:19 -03:00
|
|
|
note_event_tick("curl-socket-ready"sv);
|
|
|
|
|
auto result = time_curl_call("multi_socket_action(socket)"sv, [&] {
|
|
|
|
|
return curl_multi_socket_action(multi, sockfd, select_flag, nullptr);
|
|
|
|
|
});
|
2025-10-23 21:44:55 -03:00
|
|
|
VERIFY(result == CURLM_OK);
|
2024-11-24 21:04:29 -03:00
|
|
|
|
2025-10-23 21:44:55 -03:00
|
|
|
client->check_active_requests();
|
2024-11-24 21:04:29 -03:00
|
|
|
};
|
2024-09-05 08:20:09 -03:00
|
|
|
|
2025-10-23 21:44:55 -03:00
|
|
|
return notifier;
|
|
|
|
|
});
|
2024-11-24 21:04:29 -03:00
|
|
|
|
2026-04-21 09:58:37 -03:00
|
|
|
notifier->set_enabled(true);
|
|
|
|
|
};
|
2024-09-05 08:20:09 -03:00
|
|
|
|
2026-04-21 09:58:37 -03:00
|
|
|
update_notifier(client->m_read_notifiers, Core::NotificationType::Read, CURL_POLL_IN, CURL_CSELECT_IN);
|
|
|
|
|
update_notifier(client->m_write_notifiers, Core::NotificationType::Write, CURL_POLL_OUT, CURL_CSELECT_OUT);
|
2025-10-23 21:44:55 -03:00
|
|
|
|
|
|
|
|
return 0;
|
2021-04-23 17:45:52 -03:00
|
|
|
}
|
|
|
|
|
|
2025-10-23 21:44:55 -03:00
|
|
|
int ConnectionFromClient::on_timeout_callback(void*, long timeout_ms, void* user_data)
|
2025-02-26 10:28:21 -03:00
|
|
|
{
|
2025-10-23 21:44:55 -03:00
|
|
|
auto* client = static_cast<ConnectionFromClient*>(user_data);
|
|
|
|
|
if (!client->m_timer)
|
|
|
|
|
return 0;
|
2025-02-26 10:28:21 -03:00
|
|
|
|
2026-04-26 01:56:19 -03:00
|
|
|
if (timeout_ms < 0) {
|
2025-10-23 21:44:55 -03:00
|
|
|
client->m_timer->stop();
|
2026-04-26 01:56:19 -03:00
|
|
|
s_curl_timer_due_at = {};
|
|
|
|
|
} else {
|
2025-10-23 21:44:55 -03:00
|
|
|
client->m_timer->restart(timeout_ms);
|
2026-04-26 01:56:19 -03:00
|
|
|
s_curl_timer_due_at = MonotonicTime::now() + AK::Duration::from_milliseconds(timeout_ms);
|
|
|
|
|
}
|
2025-02-26 10:28:21 -03:00
|
|
|
|
2025-10-23 21:44:55 -03:00
|
|
|
return 0;
|
2025-02-26 10:28:21 -03:00
|
|
|
}
|
|
|
|
|
|
2024-09-05 08:20:09 -03:00
|
|
|
void ConnectionFromClient::check_active_requests()
|
2021-04-23 17:45:52 -03:00
|
|
|
{
|
2026-04-26 01:56:19 -03:00
|
|
|
note_event_tick("check-active-requests"sv);
|
2024-09-05 08:20:09 -03:00
|
|
|
int msgs_in_queue = 0;
|
2026-04-26 01:56:19 -03:00
|
|
|
u64 completions_drained = 0;
|
2024-09-05 08:20:09 -03:00
|
|
|
while (auto* msg = curl_multi_info_read(m_curl_multi, &msgs_in_queue)) {
|
|
|
|
|
if (msg->msg != CURLMSG_DONE)
|
|
|
|
|
continue;
|
|
|
|
|
|
2025-02-20 08:20:31 -03:00
|
|
|
void* application_private = nullptr;
|
|
|
|
|
auto result = curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &application_private);
|
2024-09-19 06:16:57 -03:00
|
|
|
VERIFY(result == CURLE_OK);
|
2025-02-20 08:20:31 -03:00
|
|
|
VERIFY(application_private != nullptr);
|
|
|
|
|
|
|
|
|
|
// FIXME: Come up with a unified way to track websockets and standard fetches instead of this nasty tagged pointer
|
|
|
|
|
if (reinterpret_cast<uintptr_t>(application_private) & websocket_private_tag) {
|
|
|
|
|
auto* websocket_impl = reinterpret_cast<WebSocketImplCurl*>(reinterpret_cast<uintptr_t>(application_private) & ~websocket_private_tag);
|
2025-03-13 22:13:04 -03:00
|
|
|
if (msg->data.result == CURLE_OK) {
|
|
|
|
|
if (!websocket_impl->did_connect())
|
|
|
|
|
websocket_impl->on_connection_error();
|
|
|
|
|
} else {
|
2025-02-20 08:20:31 -03:00
|
|
|
websocket_impl->on_connection_error();
|
2025-03-13 22:13:04 -03:00
|
|
|
}
|
2025-02-20 08:20:31 -03:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 01:56:19 -03:00
|
|
|
++completions_drained;
|
2025-10-23 21:44:55 -03:00
|
|
|
auto* request = static_cast<Request*>(application_private);
|
|
|
|
|
request->notify_fetch_complete({}, msg->data.result);
|
2024-09-05 08:20:09 -03:00
|
|
|
}
|
2026-04-26 01:56:19 -03:00
|
|
|
|
|
|
|
|
if (completions_drained > 1)
|
|
|
|
|
dbgln_if(REQUESTSERVER_WIRE_DEBUG, "RequestServer wire-batch: drained {} completions in one curl multi tick", completions_drained);
|
2021-04-23 17:45:52 -03:00
|
|
|
}
|
|
|
|
|
|
2026-04-17 08:31:36 -03:00
|
|
|
void ConnectionFromClient::fail_websocket(u64 websocket_id, Requests::WebSocket::Error error)
|
|
|
|
|
{
|
|
|
|
|
async_websocket_ready_state_changed(websocket_id, to_underlying(Requests::WebSocket::ReadyState::Closed));
|
|
|
|
|
async_websocket_errored(websocket_id, to_underlying(error));
|
|
|
|
|
async_websocket_closed(websocket_id, to_underlying(WebSocket::CloseStatusCode::AbnormalClosure), {}, false);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-12 11:56:53 -03:00
|
|
|
Messages::RequestServer::StopRequestResponse ConnectionFromClient::stop_request(u64 request_id)
|
2021-04-23 17:45:52 -03:00
|
|
|
{
|
2024-09-05 08:20:09 -03:00
|
|
|
auto request = m_active_requests.take(request_id);
|
|
|
|
|
if (!request.has_value()) {
|
|
|
|
|
dbgln("StopRequest: Request ID {} not found", request_id);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2021-04-23 17:45:52 -03:00
|
|
|
}
|
|
|
|
|
|
2025-12-12 11:56:53 -03:00
|
|
|
Messages::RequestServer::SetCertificateResponse ConnectionFromClient::set_certificate(u64 request_id, ByteString certificate, ByteString key)
|
2021-04-23 17:45:52 -03:00
|
|
|
{
|
2024-09-05 08:20:09 -03:00
|
|
|
(void)request_id;
|
|
|
|
|
(void)certificate;
|
|
|
|
|
(void)key;
|
|
|
|
|
TODO();
|
2024-05-08 15:15:05 -03:00
|
|
|
}
|
2022-03-17 13:03:13 -03:00
|
|
|
|
2025-12-11 16:31:12 -03:00
|
|
|
void ConnectionFromClient::ensure_connection(u64 request_id, URL::URL url, ::RequestServer::CacheLevel cache_level)
|
2021-09-27 17:36:52 -03:00
|
|
|
{
|
2025-12-11 16:31:12 -03:00
|
|
|
auto request = Request::connect(request_id, *this, m_curl_multi, m_resolver, move(url), cache_level);
|
|
|
|
|
m_active_requests.set(request_id, move(request));
|
2021-09-27 17:36:52 -03:00
|
|
|
}
|
|
|
|
|
|
2026-04-13 17:53:29 -03:00
|
|
|
void ConnectionFromClient::retrieved_http_cookie(int client_id, u64 request_id, RequestServer::RequestType request_type, String cookie)
|
2026-02-07 16:06:22 -03:00
|
|
|
{
|
2026-04-26 01:56:19 -03:00
|
|
|
note_event_tick("ipc-retrieved-cookie"sv);
|
2026-02-15 11:23:55 -03:00
|
|
|
if (auto connection = m_connections.get(client_id); connection.has_value()) {
|
2026-04-13 17:53:29 -03:00
|
|
|
auto request = [&]() {
|
|
|
|
|
switch (request_type) {
|
|
|
|
|
case RequestType::Fetch:
|
|
|
|
|
return (*connection)->m_active_requests.get(request_id);
|
|
|
|
|
case RequestType::BackgroundRevalidation:
|
|
|
|
|
return (*connection)->m_active_revalidation_requests.get(request_id);
|
|
|
|
|
case RequestType::Connect:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
if (request.has_value())
|
2026-02-07 16:06:22 -03:00
|
|
|
(*request)->notify_retrieved_http_cookie({}, cookie);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-02 15:10:27 -03:00
|
|
|
void ConnectionFromClient::estimate_cache_size_accessed_since(u64 cache_size_estimation_id, UnixDateTime since)
|
|
|
|
|
{
|
|
|
|
|
Requests::CacheSizes sizes;
|
|
|
|
|
|
2026-02-15 11:23:55 -03:00
|
|
|
if (m_disk_cache.has_value())
|
|
|
|
|
sizes = m_disk_cache->estimate_cache_size_accessed_since(since);
|
2025-11-02 15:10:27 -03:00
|
|
|
|
|
|
|
|
async_estimated_cache_size(cache_size_estimation_id, sizes);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-02 18:44:30 -03:00
|
|
|
void ConnectionFromClient::remove_cache_entries_accessed_since(UnixDateTime since)
|
2025-10-09 15:24:47 -03:00
|
|
|
{
|
2026-02-15 11:23:55 -03:00
|
|
|
if (m_disk_cache.has_value())
|
|
|
|
|
m_disk_cache->remove_entries_accessed_since(since);
|
2025-10-09 15:24:47 -03:00
|
|
|
}
|
|
|
|
|
|
2026-05-03 15:04:35 -03:00
|
|
|
Messages::RequestServer::StoreCacheAssociatedDataResponse ConnectionFromClient::store_cache_associated_data(URL::URL url, ByteString method, Vector<HTTP::Header> request_headers, Optional<u64> vary_key, HTTP::CacheEntryAssociatedData associated_data, Core::AnonymousBuffer data)
|
2026-05-03 14:12:09 -03:00
|
|
|
{
|
|
|
|
|
if (!m_disk_cache.has_value() || !data.is_valid())
|
|
|
|
|
return false;
|
|
|
|
|
|
2026-05-03 15:04:35 -03:00
|
|
|
auto result = m_disk_cache->store_associated_data(url, method, *HTTP::HeaderList::create(move(request_headers)), vary_key, associated_data, data.bytes());
|
2026-05-03 14:12:09 -03:00
|
|
|
if (result.is_error()) {
|
|
|
|
|
dbgln("Failed to store cache associated data for {}: {}", url, result.error());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.value();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 15:04:35 -03:00
|
|
|
Messages::RequestServer::RetrieveCacheAssociatedDataResponse ConnectionFromClient::retrieve_cache_associated_data(URL::URL url, ByteString method, Vector<HTTP::Header> request_headers, Optional<u64> vary_key, HTTP::CacheEntryAssociatedData associated_data)
|
2026-05-03 14:12:09 -03:00
|
|
|
{
|
|
|
|
|
if (!m_disk_cache.has_value())
|
|
|
|
|
return Optional<Core::AnonymousBuffer> {};
|
|
|
|
|
|
2026-05-03 15:04:35 -03:00
|
|
|
auto data = m_disk_cache->retrieve_associated_data(url, method, *HTTP::HeaderList::create(move(request_headers)), vary_key, associated_data);
|
2026-05-03 14:12:09 -03:00
|
|
|
if (data.is_error()) {
|
|
|
|
|
dbgln("Failed to retrieve cache associated data for {}: {}", url, data.error());
|
|
|
|
|
return Optional<Core::AnonymousBuffer> {};
|
|
|
|
|
}
|
|
|
|
|
if (!data.value().has_value())
|
|
|
|
|
return Optional<Core::AnonymousBuffer> {};
|
|
|
|
|
|
|
|
|
|
auto buffer = Core::AnonymousBuffer::create_with_size(data.value()->size());
|
|
|
|
|
if (buffer.is_error()) {
|
|
|
|
|
dbgln("Failed to allocate cache associated data buffer for {}: {}", url, buffer.error());
|
|
|
|
|
return Optional<Core::AnonymousBuffer> {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(buffer.value().data<void>(), data.value()->data(), data.value()->size());
|
|
|
|
|
return Optional<Core::AnonymousBuffer> { buffer.release_value() };
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 10:57:16 -03:00
|
|
|
Messages::RequestServer::CreateSyntheticCacheEntryResponse ConnectionFromClient::create_synthetic_cache_entry(URL::URL url, ByteString method)
|
|
|
|
|
{
|
|
|
|
|
if (!m_disk_cache.has_value())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
auto result = m_disk_cache->create_synthetic_entry(url, method);
|
|
|
|
|
if (result.is_error()) {
|
|
|
|
|
dbgln("Failed to create synthetic cache entry for {}: {}", url, result.error());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return result.value();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 13:58:50 -03:00
|
|
|
void ConnectionFromClient::websocket_connect(u64 websocket_id, URL::URL url, ByteString origin, Vector<ByteString> protocols, Vector<ByteString> extensions, Vector<HTTP::Header> additional_request_headers)
|
2024-03-05 21:50:52 -03:00
|
|
|
{
|
2025-02-20 09:21:22 -03:00
|
|
|
auto host = url.serialized_host().to_byte_string();
|
2026-03-31 07:39:59 -03:00
|
|
|
m_pending_websockets.set(websocket_id);
|
2026-04-17 08:31:36 -03:00
|
|
|
auto weak_self = make_weak_ptr<ConnectionFromClient>();
|
2024-03-05 21:50:52 -03:00
|
|
|
|
2025-02-20 09:21:22 -03:00
|
|
|
m_resolver->dns.lookup(host, DNS::Messages::Class::IN, { DNS::Messages::ResourceType::A, DNS::Messages::ResourceType::AAAA })
|
2026-04-17 08:31:36 -03:00
|
|
|
->when_rejected([weak_self, websocket_id](auto const& error) {
|
|
|
|
|
auto self = weak_self.strong_ref();
|
|
|
|
|
if (!self)
|
|
|
|
|
return;
|
2025-02-20 09:21:22 -03:00
|
|
|
dbgln("WebSocketConnect: DNS lookup failed: {}", error);
|
2026-04-17 08:31:36 -03:00
|
|
|
if (!self->m_pending_websockets.remove(websocket_id))
|
|
|
|
|
return;
|
|
|
|
|
self->fail_websocket(websocket_id, Requests::WebSocket::Error::CouldNotEstablishConnection);
|
2025-02-20 09:21:22 -03:00
|
|
|
})
|
2026-04-17 08:31:36 -03:00
|
|
|
.when_resolved([weak_self, websocket_id, host = move(host), url = move(url), origin = move(origin), protocols = move(protocols), extensions = move(extensions), additional_request_headers = move(additional_request_headers)](auto const& dns_result) mutable {
|
|
|
|
|
auto self = weak_self.strong_ref();
|
|
|
|
|
if (!self)
|
|
|
|
|
return;
|
2025-08-13 07:36:41 -03:00
|
|
|
if (dns_result->is_empty() || !dns_result->has_cached_addresses()) {
|
2025-02-20 09:21:22 -03:00
|
|
|
dbgln("WebSocketConnect: DNS lookup failed for '{}'", host);
|
2026-04-17 08:31:36 -03:00
|
|
|
if (!self->m_pending_websockets.remove(websocket_id))
|
|
|
|
|
return;
|
|
|
|
|
self->fail_websocket(websocket_id, Requests::WebSocket::Error::CouldNotEstablishConnection);
|
2025-02-20 09:21:22 -03:00
|
|
|
return;
|
|
|
|
|
}
|
2025-02-20 08:20:31 -03:00
|
|
|
|
2026-03-31 07:39:59 -03:00
|
|
|
// Don't connect the websocket if we already requested to close it before the DNS lookup completed.
|
2026-04-17 08:31:36 -03:00
|
|
|
if (!self->m_pending_websockets.remove(websocket_id))
|
2026-03-31 07:39:59 -03:00
|
|
|
return;
|
|
|
|
|
|
2025-03-08 14:22:39 -03:00
|
|
|
WebSocket::ConnectionInfo connection_info(move(url));
|
|
|
|
|
connection_info.set_origin(move(origin));
|
|
|
|
|
connection_info.set_protocols(move(protocols));
|
|
|
|
|
connection_info.set_extensions(move(extensions));
|
2025-11-26 16:13:23 -03:00
|
|
|
connection_info.set_headers(HTTP::HeaderList::create(move(additional_request_headers)));
|
2025-02-20 09:21:22 -03:00
|
|
|
connection_info.set_dns_result(move(dns_result));
|
|
|
|
|
|
2025-10-22 20:26:17 -03:00
|
|
|
if (auto const& path = default_certificate_path(); !path.is_empty())
|
|
|
|
|
connection_info.set_root_certificates_path(path);
|
2025-02-20 09:21:22 -03:00
|
|
|
|
2026-04-17 08:31:36 -03:00
|
|
|
auto impl = WebSocketImplCurl::create(self->m_curl_multi);
|
2025-02-20 09:21:22 -03:00
|
|
|
auto connection = WebSocket::WebSocket::create(move(connection_info), move(impl));
|
2024-03-05 21:50:52 -03:00
|
|
|
|
2026-04-17 08:31:36 -03:00
|
|
|
connection->on_open = [self = weak_self, websocket_id]() {
|
|
|
|
|
if (auto strong_self = self.strong_ref())
|
|
|
|
|
strong_self->async_websocket_connected(websocket_id);
|
2025-02-20 09:21:22 -03:00
|
|
|
};
|
2026-04-17 08:31:36 -03:00
|
|
|
connection->on_message = [self = weak_self, websocket_id](auto message) {
|
|
|
|
|
if (auto strong_self = self.strong_ref())
|
|
|
|
|
strong_self->async_websocket_received(websocket_id, message.is_text(), message.data());
|
2025-02-20 09:21:22 -03:00
|
|
|
};
|
2026-04-17 08:31:36 -03:00
|
|
|
connection->on_error = [self = weak_self, websocket_id](auto message) {
|
|
|
|
|
if (auto strong_self = self.strong_ref())
|
|
|
|
|
strong_self->async_websocket_errored(websocket_id, (i32)message);
|
2025-02-20 09:21:22 -03:00
|
|
|
};
|
2026-04-17 08:31:36 -03:00
|
|
|
connection->on_close = [self = weak_self, websocket_id](u16 code, ByteString reason, bool was_clean) {
|
|
|
|
|
if (auto strong_self = self.strong_ref()) {
|
|
|
|
|
strong_self->async_websocket_closed(websocket_id, code, move(reason), was_clean);
|
|
|
|
|
Core::deferred_invoke([self, websocket_id] {
|
|
|
|
|
if (auto strong_self = self.strong_ref())
|
|
|
|
|
strong_self->m_websockets.remove(websocket_id);
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-02-20 09:21:22 -03:00
|
|
|
};
|
2026-04-17 08:31:36 -03:00
|
|
|
connection->on_ready_state_change = [self = weak_self, websocket_id](auto state) {
|
|
|
|
|
if (auto strong_self = self.strong_ref())
|
|
|
|
|
strong_self->async_websocket_ready_state_changed(websocket_id, (u32)state);
|
2025-02-20 09:21:22 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
connection->start();
|
2026-04-17 08:31:36 -03:00
|
|
|
self->m_websockets.set(websocket_id, move(connection));
|
2025-02-20 09:21:22 -03:00
|
|
|
});
|
2024-03-05 21:50:52 -03:00
|
|
|
}
|
|
|
|
|
|
2025-12-11 13:58:50 -03:00
|
|
|
void ConnectionFromClient::websocket_send(u64 websocket_id, bool is_text, ByteBuffer data)
|
2024-03-05 21:50:52 -03:00
|
|
|
{
|
2025-12-11 13:58:50 -03:00
|
|
|
if (auto* connection = m_websockets.get(websocket_id).value_or({}); connection && connection->ready_state() == WebSocket::ReadyState::Open)
|
2025-03-08 14:22:39 -03:00
|
|
|
connection->send(WebSocket::Message { move(data), is_text });
|
2024-03-05 21:50:52 -03:00
|
|
|
}
|
|
|
|
|
|
2026-05-14 09:39:07 -03:00
|
|
|
void ConnectionFromClient::websocket_send_shared(u64 websocket_id, bool is_text, Core::AnonymousBuffer data)
|
|
|
|
|
{
|
|
|
|
|
auto* connection = m_websockets.get(websocket_id).value_or({});
|
|
|
|
|
if (!connection || connection->ready_state() != WebSocket::ReadyState::Open)
|
|
|
|
|
return;
|
|
|
|
|
auto byte_buffer_or_error = ByteBuffer::copy(data.bytes());
|
|
|
|
|
if (byte_buffer_or_error.is_error()) {
|
|
|
|
|
dbgln("websocket_send_shared: failed to copy {} bytes from shared buffer: {}", data.size(), byte_buffer_or_error.error());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
connection->send(WebSocket::Message { byte_buffer_or_error.release_value(), is_text });
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 13:58:50 -03:00
|
|
|
void ConnectionFromClient::websocket_close(u64 websocket_id, u16 code, ByteString reason)
|
2024-03-05 21:50:52 -03:00
|
|
|
{
|
2026-04-17 08:31:36 -03:00
|
|
|
if (m_pending_websockets.remove(websocket_id)) {
|
|
|
|
|
fail_websocket(websocket_id, Requests::WebSocket::Error::CouldNotEstablishConnection);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (auto* connection = m_websockets.get(websocket_id).value_or({}); connection && connection->ready_state() != WebSocket::ReadyState::Closed)
|
2024-03-05 21:50:52 -03:00
|
|
|
connection->close(code, reason);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 13:58:50 -03:00
|
|
|
Messages::RequestServer::WebsocketSetCertificateResponse ConnectionFromClient::websocket_set_certificate(u64 websocket_id, ByteString, ByteString)
|
2024-03-05 21:50:52 -03:00
|
|
|
{
|
|
|
|
|
auto success = false;
|
2025-12-11 13:58:50 -03:00
|
|
|
if (auto* connection = m_websockets.get(websocket_id).value_or({}); connection) {
|
2024-03-05 21:50:52 -03:00
|
|
|
// NO OP here
|
|
|
|
|
// connection->set_certificate(certificate, key);
|
|
|
|
|
success = true;
|
|
|
|
|
}
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-23 17:45:52 -03:00
|
|
|
}
|