mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-08 06:09:58 +00:00
The end goal here is for LibHTTP to be the home of our RFC 9111 (HTTP caching) implementation. We currently have one implementation in LibWeb for our in-memory cache and another in RequestServer for our disk cache. The implementations both largely revolve around interacting with HTTP headers. But in LibWeb, we are using Fetch's header infra, and in RS we are using are home-grown header infra from LibHTTP. So to give these a common denominator, this patch replaces the LibHTTP implementation with Fetch's infra. Our existing LibHTTP implementation was not particularly compliant with any spec, so this at least gives us a standards-based common implementation. This migration also required moving a handful of other Fetch AOs over to LibHTTP. (It turns out these AOs were all from the Fetch/Infra/HTTP folder, so perhaps it makes sense for LibHTTP to be the implementation of that entire set of facilities.)
67 lines
2 KiB
C++
67 lines
2 KiB
C++
/*
|
|
* Copyright (c) 2025, Tim Flynn <trflynn89@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Error.h>
|
|
#include <AK/HashMap.h>
|
|
#include <AK/Time.h>
|
|
#include <AK/Types.h>
|
|
#include <LibDatabase/Database.h>
|
|
#include <LibHTTP/HeaderList.h>
|
|
#include <LibRequests/CacheSizes.h>
|
|
|
|
namespace RequestServer {
|
|
|
|
// The cache index is a SQL database containing metadata about each cache entry. An entry in the index is created once
|
|
// the entire cache entry has been successfully written to disk.
|
|
class CacheIndex {
|
|
struct Entry {
|
|
u64 cache_key { 0 };
|
|
|
|
String url;
|
|
NonnullRefPtr<HTTP::HeaderList> response_headers;
|
|
u64 data_size { 0 };
|
|
|
|
UnixDateTime request_time;
|
|
UnixDateTime response_time;
|
|
UnixDateTime last_access_time;
|
|
};
|
|
|
|
public:
|
|
static ErrorOr<CacheIndex> create(Database::Database&);
|
|
|
|
void create_entry(u64 cache_key, String url, NonnullRefPtr<HTTP::HeaderList>, u64 data_size, UnixDateTime request_time, UnixDateTime response_time);
|
|
void remove_entry(u64 cache_key);
|
|
void remove_entries_accessed_since(UnixDateTime, Function<void(u64 cache_key)> on_entry_removed);
|
|
|
|
Optional<Entry&> find_entry(u64 cache_key);
|
|
|
|
void update_response_headers(u64 cache_key, NonnullRefPtr<HTTP::HeaderList>);
|
|
void update_last_access_time(u64 cache_key);
|
|
|
|
Requests::CacheSizes estimate_cache_size_accessed_since(UnixDateTime since) const;
|
|
|
|
private:
|
|
struct Statements {
|
|
Database::StatementID insert_entry { 0 };
|
|
Database::StatementID remove_entry { 0 };
|
|
Database::StatementID remove_entries_accessed_since { 0 };
|
|
Database::StatementID select_entry { 0 };
|
|
Database::StatementID update_response_headers { 0 };
|
|
Database::StatementID update_last_access_time { 0 };
|
|
Database::StatementID estimate_cache_size_accessed_since { 0 };
|
|
};
|
|
|
|
CacheIndex(Database::Database&, Statements);
|
|
|
|
Database::Database& m_database;
|
|
Statements m_statements;
|
|
|
|
HashMap<u32, Entry> m_entries;
|
|
};
|
|
|
|
}
|