2021-04-23 17:45:52 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2021-04-23 17:45:52 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2026-04-17 12:10:14 -03:00
|
|
|
#include <LibCore/EventLoop.h>
|
2025-11-02 15:10:27 -03:00
|
|
|
#include <LibCore/Promise.h>
|
2024-12-14 21:40:31 -03:00
|
|
|
#include <LibCore/System.h>
|
2024-08-07 00:51:20 -03:00
|
|
|
#include <LibRequests/Request.h>
|
|
|
|
|
#include <LibRequests/RequestClient.h>
|
2021-04-23 17:45:52 -03:00
|
|
|
|
2024-08-07 00:51:20 -03:00
|
|
|
namespace Requests {
|
2021-04-23 17:45:52 -03:00
|
|
|
|
2025-04-08 17:01:46 -03:00
|
|
|
RequestClient::RequestClient(NonnullOwnPtr<IPC::Transport> transport)
|
2024-10-22 18:47:33 -03:00
|
|
|
: IPC::ConnectionToServer<RequestClientEndpoint, RequestServerEndpoint>(*this, move(transport))
|
2021-04-23 17:45:52 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 05:38:35 -03:00
|
|
|
RequestClient::~RequestClient() = default;
|
|
|
|
|
|
2024-06-06 08:16:14 -03:00
|
|
|
void RequestClient::die()
|
|
|
|
|
{
|
2025-08-09 14:07:09 -03:00
|
|
|
for (auto& [id, request] : m_requests) {
|
|
|
|
|
if (request)
|
|
|
|
|
request->did_finish({}, {}, {}, NetworkError::RequestServerDied);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-02 15:10:27 -03:00
|
|
|
for (auto& [id, promise] : m_pending_cache_size_estimations)
|
|
|
|
|
promise->reject(Error::from_string_literal("RequestServer process died"));
|
|
|
|
|
|
2026-04-17 12:10:14 -03:00
|
|
|
auto websockets = move(m_websockets);
|
|
|
|
|
|
2025-08-09 14:07:09 -03:00
|
|
|
m_requests.clear();
|
2025-11-02 15:10:27 -03:00
|
|
|
m_pending_cache_size_estimations.clear();
|
2026-04-17 12:10:14 -03:00
|
|
|
m_websockets.clear();
|
|
|
|
|
|
|
|
|
|
for (auto& [id, websocket] : websockets) {
|
|
|
|
|
auto ready_state = websocket->ready_state();
|
|
|
|
|
websocket->detach_from_client({});
|
|
|
|
|
websocket->set_ready_state(WebSocket::ReadyState::Closed);
|
|
|
|
|
|
|
|
|
|
auto error = ready_state == WebSocket::ReadyState::Connecting
|
|
|
|
|
? WebSocket::Error::CouldNotEstablishConnection
|
|
|
|
|
: WebSocket::Error::ServerClosedSocket;
|
|
|
|
|
websocket->did_error({}, to_underlying(error));
|
|
|
|
|
websocket->did_close({}, to_underlying(::WebSocket::CloseStatusCode::AbnormalClosure), {}, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (auto request_server_died_callback = move(on_request_server_died)) {
|
|
|
|
|
Core::deferred_invoke([request_server_died_callback = move(request_server_died_callback)]() mutable {
|
|
|
|
|
request_server_died_callback();
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-06-06 08:16:14 -03:00
|
|
|
}
|
|
|
|
|
|
2026-02-07 16:06:22 -03:00
|
|
|
RefPtr<Request> RequestClient::start_request(ByteString const& method, URL::URL const& url, Optional<HTTP::HeaderList const&> request_headers, ReadonlyBytes request_body, HTTP::CacheMode cache_mode, HTTP::Cookie::IncludeCredentials include_credentials, Core::ProxyData const& proxy_data)
|
2021-04-23 17:45:52 -03:00
|
|
|
{
|
2025-12-11 16:31:12 -03:00
|
|
|
auto request_id = m_next_request_id++;
|
2025-11-26 16:13:23 -03:00
|
|
|
auto headers = request_headers.map([](auto const& headers) { return headers.headers().span(); }).value_or({});
|
|
|
|
|
|
2026-02-07 16:06:22 -03:00
|
|
|
IPCProxy::async_start_request(request_id, method, url, headers, request_body, cache_mode, include_credentials, proxy_data);
|
2021-04-23 17:45:52 -03:00
|
|
|
auto request = Request::create_from_id({}, *this, request_id);
|
|
|
|
|
m_requests.set(request_id, request);
|
|
|
|
|
return request;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 15:04:35 -03:00
|
|
|
ErrorOr<bool> RequestClient::store_cache_associated_data(URL::URL const& url, ByteString const& method, Optional<HTTP::HeaderList const&> request_headers, Optional<u64> vary_key, HTTP::CacheEntryAssociatedData associated_data, ReadonlyBytes data)
|
2026-05-03 14:12:09 -03:00
|
|
|
{
|
|
|
|
|
auto buffer = TRY(Core::AnonymousBuffer::create_with_size(data.size()));
|
|
|
|
|
memcpy(buffer.data<void>(), data.data(), data.size());
|
|
|
|
|
|
|
|
|
|
auto headers = request_headers.map([](auto const& headers) { return headers.headers(); }).value_or({});
|
2026-05-03 15:04:35 -03:00
|
|
|
return IPCProxy::store_cache_associated_data(url, method, headers, vary_key, associated_data, move(buffer));
|
2026-05-03 14:12:09 -03:00
|
|
|
}
|
|
|
|
|
|
2026-05-03 15:04:35 -03:00
|
|
|
ErrorOr<Optional<Core::AnonymousBuffer>> RequestClient::retrieve_cache_associated_data(URL::URL const& url, ByteString const& method, Optional<HTTP::HeaderList const&> request_headers, Optional<u64> vary_key, HTTP::CacheEntryAssociatedData associated_data)
|
2026-05-03 14:12:09 -03:00
|
|
|
{
|
|
|
|
|
auto headers = request_headers.map([](auto const& headers) { return headers.headers(); }).value_or({});
|
2026-05-03 15:04:35 -03:00
|
|
|
return IPCProxy::retrieve_cache_associated_data(url, method, headers, vary_key, associated_data);
|
2026-05-03 14:12:09 -03:00
|
|
|
}
|
|
|
|
|
|
2021-04-23 17:45:52 -03:00
|
|
|
bool RequestClient::stop_request(Badge<Request>, Request& request)
|
|
|
|
|
{
|
|
|
|
|
if (!m_requests.contains(request.id()))
|
|
|
|
|
return false;
|
2021-05-03 08:55:29 -03:00
|
|
|
return IPCProxy::stop_request(request.id());
|
2021-04-23 17:45:52 -03:00
|
|
|
}
|
|
|
|
|
|
2026-02-07 09:50:20 -03:00
|
|
|
void RequestClient::ensure_connection(URL::URL const& url, RequestServer::CacheLevel cache_level)
|
|
|
|
|
{
|
|
|
|
|
auto request_id = m_next_request_id++;
|
|
|
|
|
async_ensure_connection(request_id, url, cache_level);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-16 11:19:34 -03:00
|
|
|
bool RequestClient::set_certificate(Badge<Request>, Request& request, ByteString certificate, ByteString key)
|
2021-04-23 17:45:52 -03:00
|
|
|
{
|
|
|
|
|
if (!m_requests.contains(request.id()))
|
|
|
|
|
return false;
|
2021-05-03 08:55:29 -03:00
|
|
|
return IPCProxy::set_certificate(request.id(), move(certificate), move(key));
|
2021-04-23 17:45:52 -03:00
|
|
|
}
|
|
|
|
|
|
2025-11-02 15:10:27 -03:00
|
|
|
NonnullRefPtr<Core::Promise<CacheSizes>> RequestClient::estimate_cache_size_accessed_since(UnixDateTime since)
|
|
|
|
|
{
|
|
|
|
|
auto promise = Core::Promise<CacheSizes>::construct();
|
|
|
|
|
|
|
|
|
|
auto cache_size_estimation_id = m_next_cache_size_estimation_id++;
|
|
|
|
|
m_pending_cache_size_estimations.set(cache_size_estimation_id, promise);
|
|
|
|
|
|
|
|
|
|
async_estimate_cache_size_accessed_since(cache_size_estimation_id, since);
|
|
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RequestClient::estimated_cache_size(u64 cache_size_estimation_id, CacheSizes sizes)
|
|
|
|
|
{
|
|
|
|
|
if (auto promise = m_pending_cache_size_estimations.take(cache_size_estimation_id); promise.has_value())
|
|
|
|
|
(*promise)->resolve(sizes);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-07 09:50:20 -03:00
|
|
|
void RequestClient::request_started(u64 request_id, IPC::File response_file)
|
|
|
|
|
{
|
|
|
|
|
auto request = m_requests.get(request_id);
|
|
|
|
|
if (!request.has_value()) {
|
|
|
|
|
warnln("Received response for non-existent request {}", request_id);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto response_fd = response_file.take_fd();
|
|
|
|
|
request.value()->set_request_fd({}, response_fd);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-12 11:56:53 -03:00
|
|
|
void RequestClient::request_finished(u64 request_id, u64 total_size, RequestTimingInfo timing_info, Optional<NetworkError> network_error)
|
2021-04-23 17:45:52 -03:00
|
|
|
{
|
2026-02-07 09:50:20 -03:00
|
|
|
if (RefPtr<Request> request = m_requests.get(request_id).value_or(nullptr)) {
|
2025-02-26 10:28:21 -03:00
|
|
|
request->did_finish({}, total_size, timing_info, network_error);
|
2026-02-07 09:50:20 -03:00
|
|
|
m_requests.remove(request_id);
|
2021-04-23 17:45:52 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-03 15:04:35 -03:00
|
|
|
void RequestClient::headers_became_available(u64 request_id, Vector<HTTP::Header> response_headers, Optional<u32> status_code, Optional<String> reason_phrase, Optional<Core::AnonymousBuffer> javascript_bytecode, Optional<u64> javascript_bytecode_cache_vary_key)
|
2021-04-23 17:45:52 -03:00
|
|
|
{
|
2026-02-07 09:50:20 -03:00
|
|
|
if (auto request = m_requests.get(request_id); request.has_value())
|
2026-05-03 15:04:35 -03:00
|
|
|
(*request)->did_receive_headers({}, HTTP::HeaderList::create(move(response_headers)), status_code, reason_phrase, move(javascript_bytecode), javascript_bytecode_cache_vary_key);
|
2026-02-07 09:50:20 -03:00
|
|
|
else
|
2023-05-14 18:04:47 -03:00
|
|
|
warnln("Received headers for non-existent request {}", request_id);
|
2021-04-23 17:45:52 -03:00
|
|
|
}
|
|
|
|
|
|
2026-04-13 17:53:29 -03:00
|
|
|
void RequestClient::retrieve_http_cookie(int client_id, u64 request_id, RequestServer::RequestType request_type, URL::URL url)
|
2026-02-07 16:06:22 -03:00
|
|
|
{
|
|
|
|
|
String cookie;
|
|
|
|
|
|
|
|
|
|
if (on_retrieve_http_cookie)
|
|
|
|
|
cookie = on_retrieve_http_cookie(url);
|
|
|
|
|
|
2026-04-13 17:53:29 -03:00
|
|
|
async_retrieved_http_cookie(client_id, request_id, request_type, cookie);
|
2026-02-07 16:06:22 -03:00
|
|
|
}
|
|
|
|
|
|
2025-12-12 11:56:53 -03:00
|
|
|
void RequestClient::certificate_requested(u64 request_id)
|
2021-04-23 17:45:52 -03:00
|
|
|
{
|
2026-02-07 09:50:20 -03:00
|
|
|
if (auto request = m_requests.get(request_id); request.has_value())
|
|
|
|
|
(*request)->did_request_certificates({});
|
2021-04-23 17:45:52 -03:00
|
|
|
}
|
|
|
|
|
|
2025-11-26 16:13:23 -03:00
|
|
|
RefPtr<WebSocket> RequestClient::websocket_connect(URL::URL const& url, ByteString const& origin, Vector<ByteString> const& protocols, Vector<ByteString> const& extensions, HTTP::HeaderList const& request_headers)
|
2024-03-05 21:50:52 -03:00
|
|
|
{
|
2024-09-18 05:28:55 -03:00
|
|
|
auto websocket_id = m_next_websocket_id++;
|
2025-11-26 16:13:23 -03:00
|
|
|
IPCProxy::async_websocket_connect(websocket_id, url, origin, protocols, extensions, request_headers.headers());
|
2024-09-18 05:28:55 -03:00
|
|
|
auto connection = WebSocket::create_from_id({}, *this, websocket_id);
|
|
|
|
|
m_websockets.set(websocket_id, connection);
|
2024-03-05 21:50:52 -03:00
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 13:58:50 -03:00
|
|
|
void RequestClient::websocket_connected(u64 websocket_id)
|
2024-03-05 21:50:52 -03:00
|
|
|
{
|
2026-02-07 09:50:20 -03:00
|
|
|
if (auto connection = m_websockets.get(websocket_id); connection.has_value())
|
|
|
|
|
(*connection)->did_open({});
|
2024-03-05 21:50:52 -03:00
|
|
|
}
|
|
|
|
|
|
2025-12-11 13:58:50 -03:00
|
|
|
void RequestClient::websocket_received(u64 websocket_id, bool is_text, ByteBuffer data)
|
2024-03-05 21:50:52 -03:00
|
|
|
{
|
2026-02-07 09:50:20 -03:00
|
|
|
if (auto connection = m_websockets.get(websocket_id); connection.has_value())
|
|
|
|
|
(*connection)->did_receive({}, move(data), is_text);
|
2024-03-05 21:50:52 -03:00
|
|
|
}
|
|
|
|
|
|
2025-12-11 13:58:50 -03:00
|
|
|
void RequestClient::websocket_errored(u64 websocket_id, i32 message)
|
2024-03-05 21:50:52 -03:00
|
|
|
{
|
2026-02-07 09:50:20 -03:00
|
|
|
if (auto connection = m_websockets.get(websocket_id); connection.has_value())
|
|
|
|
|
(*connection)->did_error({}, message);
|
2024-03-05 21:50:52 -03:00
|
|
|
}
|
|
|
|
|
|
2025-12-11 13:58:50 -03:00
|
|
|
void RequestClient::websocket_closed(u64 websocket_id, u16 code, ByteString reason, bool clean)
|
2024-03-05 21:50:52 -03:00
|
|
|
{
|
2026-04-17 08:31:36 -03:00
|
|
|
if (auto connection = m_websockets.take(websocket_id); connection.has_value()) {
|
|
|
|
|
(*connection)->set_ready_state(WebSocket::ReadyState::Closed);
|
2026-02-07 09:50:20 -03:00
|
|
|
(*connection)->did_close({}, code, move(reason), clean);
|
2026-04-17 08:31:36 -03:00
|
|
|
}
|
2024-03-05 21:50:52 -03:00
|
|
|
}
|
|
|
|
|
|
2025-12-11 13:58:50 -03:00
|
|
|
void RequestClient::websocket_ready_state_changed(u64 websocket_id, u32 ready_state)
|
2024-09-18 05:28:55 -03:00
|
|
|
{
|
2026-02-07 09:50:20 -03:00
|
|
|
VERIFY(ready_state <= static_cast<u32>(WebSocket::ReadyState::Closed));
|
|
|
|
|
|
|
|
|
|
if (auto connection = m_websockets.get(websocket_id); connection.has_value())
|
|
|
|
|
(*connection)->set_ready_state(static_cast<WebSocket::ReadyState>(ready_state));
|
2024-09-18 05:28:55 -03:00
|
|
|
}
|
|
|
|
|
|
2025-12-11 13:58:50 -03:00
|
|
|
void RequestClient::websocket_subprotocol(u64 websocket_id, ByteString subprotocol)
|
2024-09-18 05:28:55 -03:00
|
|
|
{
|
2026-02-07 09:50:20 -03:00
|
|
|
if (auto connection = m_websockets.get(websocket_id); connection.has_value()) {
|
|
|
|
|
(*connection)->set_subprotocol_in_use(move(subprotocol));
|
2024-09-18 05:28:55 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 13:58:50 -03:00
|
|
|
void RequestClient::websocket_certificate_requested(u64 websocket_id)
|
2024-03-05 21:50:52 -03:00
|
|
|
{
|
2026-02-07 09:50:20 -03:00
|
|
|
if (auto connection = m_websockets.get(websocket_id); connection.has_value())
|
|
|
|
|
(*connection)->did_request_certificates({});
|
2024-03-05 21:50:52 -03:00
|
|
|
}
|
|
|
|
|
|
2021-04-23 17:45:52 -03:00
|
|
|
}
|