LibWebView+RequestServer: Add a simple test mode for the HTTP disk cache

This mode allows us to test the HTTP disk cache with two mechanisms:

1. If RequestServer is launched with --http-disk-cache-mode=testing, it
   will cache requests with a X-Ladybird-Enable-Disk-Cache header.

2. In test mode, RS will include a X-Ladybird-Disk-Cache-Status response
   header indicating how the response was handled by the cache. There is
   no standard way for a web request to know what happened with respect
   to the disk cache, so this fills that hole for testing.

This mode is not exposed to users.
This commit is contained in:
Timothy Flynn 2025-11-18 10:18:40 -05:00 committed by Jelle Raaijmakers
parent a853bb43ef
commit b2c112c41a
Notes: github-actions[bot] 2025-11-20 08:35:41 +00:00
11 changed files with 107 additions and 28 deletions

View file

@ -15,21 +15,26 @@ namespace RequestServer {
static constexpr auto INDEX_DATABASE = "INDEX"sv;
ErrorOr<DiskCache> DiskCache::create()
ErrorOr<DiskCache> DiskCache::create(Mode mode)
{
auto cache_directory = LexicalPath::join(Core::StandardPaths::cache_directory(), "Ladybird"sv, "Cache"sv);
auto cache_name = mode == Mode::Normal ? "Cache"sv : "TestCache"sv;
auto cache_directory = LexicalPath::join(Core::StandardPaths::cache_directory(), "Ladybird"sv, cache_name);
auto database = TRY(Database::Database::create(cache_directory.string(), INDEX_DATABASE));
auto index = TRY(CacheIndex::create(database));
return DiskCache { move(database), move(cache_directory), move(index) };
return DiskCache { mode, move(database), move(cache_directory), move(index) };
}
DiskCache::DiskCache(NonnullRefPtr<Database::Database> database, LexicalPath cache_directory, CacheIndex index)
: m_database(move(database))
DiskCache::DiskCache(Mode mode, NonnullRefPtr<Database::Database> database, LexicalPath cache_directory, CacheIndex index)
: m_mode(mode)
, m_database(move(database))
, m_cache_directory(move(cache_directory))
, m_index(move(index))
{
// Start with a clean slate in test mode.
if (m_mode == Mode::Testing)
remove_entries_accessed_since(UnixDateTime::earliest());
}
Variant<Optional<CacheEntryWriter&>, DiskCache::CacheHasOpenEntry> DiskCache::create_entry(Request& request)
@ -37,6 +42,11 @@ Variant<Optional<CacheEntryWriter&>, DiskCache::CacheHasOpenEntry> DiskCache::cr
if (!is_cacheable(request.method()))
return Optional<CacheEntryWriter&> {};
if (m_mode == Mode::Testing) {
if (!request.request_headers().contains(TEST_CACHE_ENABLED_HEADER))
return Optional<CacheEntryWriter&> {};
}
auto serialized_url = serialize_url_for_cache_storage(request.url());
auto cache_key = create_cache_key(serialized_url, request.method());