From 95d23d02f13b23108331a9cdc8ca8cc838fe5df5 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 25 Oct 2025 08:09:11 -0400 Subject: [PATCH] RequestServer: Pass the Request object to disk cache entry factories This object will be needed in a future commit to store requests awaiting other requests to finish. Doing this in a separate commit just to make that commit less noisy. --- Services/RequestServer/Cache/DiskCache.cpp | 31 +++++++++++----------- Services/RequestServer/Cache/DiskCache.h | 4 +-- Services/RequestServer/Request.cpp | 4 +-- Services/RequestServer/Request.h | 4 +++ 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/Services/RequestServer/Cache/DiskCache.cpp b/Services/RequestServer/Cache/DiskCache.cpp index 138bdea8e4f..54ffc5fc26b 100644 --- a/Services/RequestServer/Cache/DiskCache.cpp +++ b/Services/RequestServer/Cache/DiskCache.cpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace RequestServer { @@ -32,21 +33,21 @@ DiskCache::DiskCache(NonnullRefPtr database, LexicalPath cac { } -Optional DiskCache::create_entry(URL::URL const& url, StringView method, UnixDateTime request_time) +Optional DiskCache::create_entry(Request& request) { - if (!is_cacheable(method)) + if (!is_cacheable(request.method())) return {}; - auto serialized_url = serialize_url_for_cache_storage(url); - auto cache_key = create_cache_key(serialized_url, method); + auto serialized_url = serialize_url_for_cache_storage(request.url()); + auto cache_key = create_cache_key(serialized_url, request.method()); - auto cache_entry = CacheEntryWriter::create(*this, m_index, cache_key, move(serialized_url), request_time); + auto cache_entry = CacheEntryWriter::create(*this, m_index, cache_key, move(serialized_url), request.request_start_time()); if (cache_entry.is_error()) { - dbgln("\033[31;1mUnable to create cache entry for\033[0m {}: {}", url, cache_entry.error()); + dbgln("\033[31;1mUnable to create cache entry for\033[0m {}: {}", request.url(), cache_entry.error()); return {}; } - dbgln("\033[32;1mCreated disk cache entry for\033[0m {}", url); + dbgln("\033[32;1mCreated disk cache entry for\033[0m {}", request.url()); auto address = reinterpret_cast(cache_entry.value().ptr()); m_open_cache_entries.set(address, cache_entry.release_value()); @@ -54,23 +55,23 @@ Optional DiskCache::create_entry(URL::URL const& url, StringV return static_cast(**m_open_cache_entries.get(address)); } -Optional DiskCache::open_entry(URL::URL const& url, StringView method) +Optional DiskCache::open_entry(Request& request) { - if (!is_cacheable(method)) + if (!is_cacheable(request.method())) return {}; - auto serialized_url = serialize_url_for_cache_storage(url); - auto cache_key = create_cache_key(serialized_url, method); + auto serialized_url = serialize_url_for_cache_storage(request.url()); + auto cache_key = create_cache_key(serialized_url, request.method()); auto index_entry = m_index.find_entry(cache_key); if (!index_entry.has_value()) { - dbgln("\033[35;1mNo disk cache entry for\033[0m {}", url); + dbgln("\033[35;1mNo disk cache entry for\033[0m {}", request.url()); return {}; } auto cache_entry = CacheEntryReader::create(*this, m_index, cache_key, index_entry->data_size); if (cache_entry.is_error()) { - dbgln("\033[31;1mUnable to open cache entry for\033[0m {}: {}", url, cache_entry.error()); + dbgln("\033[31;1mUnable to open cache entry for\033[0m {}: {}", request.url(), cache_entry.error()); m_index.remove_entry(cache_key); return {}; } @@ -79,12 +80,12 @@ Optional DiskCache::open_entry(URL::URL const& url, StringVie auto current_age = calculate_age(cache_entry.value()->headers(), index_entry->request_time, index_entry->response_time); if (!is_response_fresh(freshness_lifetime, current_age)) { - dbgln("\033[33;1mCache entry expired for\033[0m {} (lifetime={}s age={}s)", url, freshness_lifetime.to_seconds(), current_age.to_seconds()); + dbgln("\033[33;1mCache entry expired for\033[0m {} (lifetime={}s age={}s)", request.url(), freshness_lifetime.to_seconds(), current_age.to_seconds()); cache_entry.value()->remove(); return {}; } - dbgln("\033[32;1mOpened disk cache entry for\033[0m {} (lifetime={}s age={}s) ({} bytes)", url, freshness_lifetime.to_seconds(), current_age.to_seconds(), index_entry->data_size); + dbgln("\033[32;1mOpened disk cache entry for\033[0m {} (lifetime={}s age={}s) ({} bytes)", request.url(), freshness_lifetime.to_seconds(), current_age.to_seconds(), index_entry->data_size); auto address = reinterpret_cast(cache_entry.value().ptr()); m_open_cache_entries.set(address, cache_entry.release_value()); diff --git a/Services/RequestServer/Cache/DiskCache.h b/Services/RequestServer/Cache/DiskCache.h index efba22d5595..089b413a89d 100644 --- a/Services/RequestServer/Cache/DiskCache.h +++ b/Services/RequestServer/Cache/DiskCache.h @@ -23,8 +23,8 @@ class DiskCache { public: static ErrorOr create(); - Optional create_entry(URL::URL const&, StringView method, UnixDateTime request_time); - Optional open_entry(URL::URL const&, StringView method); + Optional create_entry(Request&); + Optional open_entry(Request&); void clear_cache(); LexicalPath const& cache_directory() { return m_cache_directory; } diff --git a/Services/RequestServer/Request.cpp b/Services/RequestServer/Request.cpp index f8090050ca2..b1545187b0e 100644 --- a/Services/RequestServer/Request.cpp +++ b/Services/RequestServer/Request.cpp @@ -167,13 +167,13 @@ void Request::process() void Request::handle_initial_state() { if (m_disk_cache.has_value()) { - m_cache_entry_reader = m_disk_cache->open_entry(m_url, m_method); + m_cache_entry_reader = m_disk_cache->open_entry(*this); if (m_cache_entry_reader.has_value()) { transition_to_state(State::ReadCache); return; } - m_cache_entry_writer = m_disk_cache->create_entry(m_url, m_method, m_request_start_time); + m_cache_entry_writer = m_disk_cache->create_entry(*this); } transition_to_state(State::DNSLookup); diff --git a/Services/RequestServer/Request.h b/Services/RequestServer/Request.h index 03f66989b27..c362bfd0634 100644 --- a/Services/RequestServer/Request.h +++ b/Services/RequestServer/Request.h @@ -50,6 +50,10 @@ public: ~Request(); + URL::URL const& url() const { return m_url; } + ByteString const& method() const { return m_method; } + UnixDateTime request_start_time() const { return m_request_start_time; } + void notify_fetch_complete(Badge, int result_code); private: