ladybird/Libraries/LibHTTP/Cache/MemoryCache.h
Andreas Kling 72720bc229 LibWeb: Preserve JS bytecode in memory cache
Store JavaScript bytecode side data in the WebContent HTTP memory
cache and replay it when serving cached responses. Also update an
already-complete memory-cache entry when asynchronous bytecode cache
generation finishes, so the first source-only response does not keep
shadowing the disk-cache sidecar during same-process navigations.

Keep the HTTP memory-cache backfill keyed with the request headers that
populated the memory-cache entry, so Vary responses still receive their
generated bytecode sidecar.

Add LibHTTP coverage for round-tripping bytecode side data through a
memory-cache entry, attaching it after the response body has already
been cached, and matching Vary headers during updates. Add LibWeb
coverage for preserving the memory-cache request headers when cloning
responses.
2026-06-06 09:15:09 +02:00

51 lines
1.9 KiB
C++

/*
* Copyright (c) 2025-2026, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/ByteString.h>
#include <AK/HashMap.h>
#include <AK/NonnullRefPtr.h>
#include <AK/RefCounted.h>
#include <AK/Time.h>
#include <LibCore/ImmutableBytes.h>
#include <LibHTTP/Cache/CacheMode.h>
#include <LibHTTP/Forward.h>
#include <LibURL/URL.h>
namespace HTTP {
class MemoryCache : public RefCounted<MemoryCache> {
public:
struct Entry {
u64 vary_key { 0 };
u32 status_code { 0 };
ByteString reason_phrase;
NonnullRefPtr<HeaderList> request_headers;
NonnullRefPtr<HeaderList> response_headers;
Core::ImmutableBytes response_body;
Optional<Core::ImmutableBytes> javascript_bytecode_cache;
Optional<u64> javascript_bytecode_cache_vary_key;
UnixDateTime request_time;
UnixDateTime response_time;
};
static NonnullRefPtr<MemoryCache> create();
Optional<Entry const&> open_entry(URL::URL const&, StringView method, HeaderList const& request_headers, CacheMode);
void create_entry(URL::URL const&, StringView method, HeaderList const& request_headers, UnixDateTime request_time, u32 status_code, ByteString reason_phrase, HeaderList const& response_headers, Optional<Core::ImmutableBytes> javascript_bytecode_cache = {}, Optional<u64> javascript_bytecode_cache_vary_key = {});
void finalize_entry(URL::URL const&, StringView method, HeaderList const& request_headers, u32 status_code, HeaderList const& response_headers, Core::ImmutableBytes response_body);
void update_javascript_bytecode_cache(URL::URL const&, StringView method, HeaderList const& request_headers, u64 vary_key, Core::ImmutableBytes javascript_bytecode_cache);
private:
HashMap<u64, Vector<Entry>, IdentityHashTraits<u64>> m_pending_entries;
HashMap<u64, Vector<Entry>, IdentityHashTraits<u64>> m_complete_entries;
};
}