LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
/*
2025-12-24 11:08:14 -05:00
* Copyright ( c ) 2025 - 2026 , Tim Flynn < trflynn89 @ ladybird . org >
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
2025-12-01 15:15:45 -05:00
# include <AK/Debug.h>
2025-10-29 15:36:33 -04:00
# include <AK/StringBuilder.h>
2026-02-04 17:53:00 -05:00
# include <LibFileSystem/FileSystem.h>
2025-11-28 10:04:59 -05:00
# include <LibHTTP/Cache/CacheIndex.h>
# include <LibHTTP/Cache/Utilities.h>
# include <LibHTTP/Cache/Version.h>
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
2025-11-28 10:04:59 -05:00
namespace HTTP {
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
2025-10-29 12:39:08 -04:00
static constexpr u32 CACHE_METADATA_KEY = 12389u ;
2025-11-28 10:04:59 -05:00
static ByteString serialize_headers ( HeaderList const & headers )
2025-10-29 15:36:33 -04:00
{
StringBuilder builder ;
2025-11-26 14:13:23 -05:00
for ( auto const & header : headers ) {
2025-10-29 15:36:33 -04:00
builder . append ( header . name ) ;
builder . append ( ' : ' ) ;
builder . append ( header . value ) ;
builder . append ( ' \n ' ) ;
}
return builder . to_byte_string ( ) ;
}
2025-11-28 10:04:59 -05:00
static NonnullRefPtr < HeaderList > deserialize_headers ( StringView serialized_headers )
2025-10-29 15:36:33 -04:00
{
2025-11-28 10:04:59 -05:00
auto headers = HeaderList : : create ( ) ;
2025-10-29 15:36:33 -04:00
serialized_headers . for_each_split_view ( ' \n ' , SplitBehavior : : Nothing , [ & ] ( StringView serialized_header ) {
auto index = serialized_header . find ( ' : ' ) ;
if ( ! index . has_value ( ) )
return ;
2026-02-26 15:04:14 -05:00
auto name = serialized_header . substring_view ( 0 , * index ) ;
2025-10-29 15:36:33 -04:00
if ( is_header_exempted_from_storage ( name ) )
return ;
2026-02-26 15:04:14 -05:00
auto value = serialized_header . substring_view ( * index + 1 ) ;
2025-11-26 14:13:23 -05:00
headers - > append ( { name , value } ) ;
2025-10-29 15:36:33 -04:00
} ) ;
return headers ;
}
2026-02-15 09:35:44 -05:00
ErrorOr < CacheIndex > CacheIndex : : create ( Database : : Database & database , LexicalPath const & cache_directory )
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
{
2025-10-29 12:39:08 -04:00
auto create_cache_metadata_table = TRY ( database . prepare_statement ( R " #(
CREATE TABLE IF NOT EXISTS CacheMetadata (
metadata_key INTEGER ,
version INTEGER ,
PRIMARY KEY ( metadata_key )
) ;
) # " sv));
database . execute_statement ( create_cache_metadata_table , { } ) ;
auto read_cache_version = TRY ( database . prepare_statement ( " SELECT version FROM CacheMetadata WHERE metadata_key = ?; " sv ) ) ;
auto cache_version = 0u ;
database . execute_statement (
read_cache_version ,
[ & ] ( auto statement_id ) { cache_version = database . result_column < u32 > ( statement_id , 0 ) ; } ,
CACHE_METADATA_KEY ) ;
if ( cache_version ! = CACHE_VERSION ) {
2025-11-20 15:11:01 -05:00
if ( cache_version ! = 0 )
2026-01-09 11:36:47 -05:00
dbgln_if ( HTTP_DISK_CACHE_DEBUG , " \033 [36m[disk] \033 [0m \033 [31;1mDisk cache version mismatch: \033 [0m stored version = {}, new version = {} " , cache_version , CACHE_VERSION ) ;
2025-10-29 12:39:08 -04:00
// FIXME: We should more elegantly handle minor changes, i.e. use ALTER TABLE to add fields to CacheIndex.
auto delete_cache_index_table = TRY ( database . prepare_statement ( " DROP TABLE IF EXISTS CacheIndex; " sv ) ) ;
database . execute_statement ( delete_cache_index_table , { } ) ;
auto set_cache_version = TRY ( database . prepare_statement ( " INSERT OR REPLACE INTO CacheMetadata VALUES (?, ?); " sv ) ) ;
database . execute_statement ( set_cache_version , { } , CACHE_METADATA_KEY , CACHE_VERSION ) ;
}
auto create_cache_index_table = TRY ( database . prepare_statement ( R " #(
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
CREATE TABLE IF NOT EXISTS CacheIndex (
cache_key INTEGER ,
2025-12-28 10:23:56 -05:00
vary_key INTEGER ,
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
url TEXT ,
2025-12-24 11:08:14 -05:00
request_headers BLOB ,
response_headers BLOB ,
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
data_size INTEGER ,
request_time INTEGER ,
response_time INTEGER ,
last_access_time INTEGER ,
2025-12-28 10:23:56 -05:00
PRIMARY KEY ( cache_key , vary_key )
2025-10-29 12:39:08 -04:00
) ;
) # " sv));
database . execute_statement ( create_cache_index_table , { } ) ;
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
Statements statements { } ;
2025-12-28 10:23:56 -05:00
statements . insert_entry = TRY ( database . prepare_statement ( " INSERT OR REPLACE INTO CacheIndex VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?); " sv ) ) ;
2026-04-17 16:54:21 +02:00
statements . remove_entry = TRY ( database . prepare_statement ( R " #(
DELETE FROM CacheIndex
WHERE cache_key = ? AND vary_key = ?
RETURNING data_size + OCTET_LENGTH ( request_headers ) + OCTET_LENGTH ( response_headers ) ;
) # " sv));
statements . remove_entries_accessed_since = TRY ( database . prepare_statement ( R " #(
DELETE FROM CacheIndex
WHERE last_access_time > = ?
RETURNING cache_key , vary_key , data_size + OCTET_LENGTH ( request_headers ) + OCTET_LENGTH ( response_headers ) ;
) # " sv));
2025-12-28 10:23:56 -05:00
statements . select_entries = TRY ( database . prepare_statement ( " SELECT * FROM CacheIndex WHERE cache_key = ?; " sv ) ) ;
statements . update_response_headers = TRY ( database . prepare_statement ( " UPDATE CacheIndex SET response_headers = ? WHERE cache_key = ? AND vary_key = ?; " sv ) ) ;
statements . update_last_access_time = TRY ( database . prepare_statement ( " UPDATE CacheIndex SET last_access_time = ? WHERE cache_key = ? AND vary_key = ?; " sv ) ) ;
2026-02-04 17:29:04 -05:00
2026-02-04 17:53:00 -05:00
statements . remove_entries_exceeding_cache_limit = TRY ( database . prepare_statement ( R " #(
WITH RankedCacheIndex AS (
SELECT
cache_key ,
vary_key ,
SUM ( data_size + OCTET_LENGTH ( request_headers ) + OCTET_LENGTH ( response_headers ) )
OVER ( ORDER BY last_access_time DESC )
AS cumulative_estimated_size
FROM CacheIndex
)
DELETE FROM CacheIndex
WHERE ( cache_key , vary_key ) IN (
SELECT cache_key , vary_key
FROM RankedCacheIndex
WHERE cumulative_estimated_size > ?
)
2026-04-17 16:54:21 +02:00
RETURNING cache_key , vary_key , data_size + OCTET_LENGTH ( request_headers ) + OCTET_LENGTH ( response_headers ) ;
2026-02-04 17:53:00 -05:00
) # " sv));
2026-02-04 17:29:04 -05:00
statements . estimate_cache_size_accessed_since = TRY ( database . prepare_statement ( R " #(
2026-04-17 16:54:21 +02:00
SELECT COALESCE ( SUM ( data_size + OCTET_LENGTH ( request_headers ) + OCTET_LENGTH ( response_headers ) ) , 0 )
2026-02-04 17:29:04 -05:00
FROM CacheIndex
WHERE last_access_time > = ? ;
) # " sv));
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
2026-04-17 16:54:21 +02:00
statements . select_total_estimated_size = TRY ( database . prepare_statement ( R " #(
SELECT COALESCE ( SUM ( data_size + OCTET_LENGTH ( request_headers ) + OCTET_LENGTH ( response_headers ) ) , 0 )
FROM CacheIndex ;
) # " sv));
2026-02-15 09:35:44 -05:00
auto disk_space = TRY ( FileSystem : : compute_disk_space ( cache_directory ) ) ;
2026-02-04 17:53:00 -05:00
auto maximum_disk_cache_size = compute_maximum_disk_cache_size ( disk_space . free_bytes ) ;
Limits limits {
. free_disk_space = disk_space . free_bytes ,
. maximum_disk_cache_size = maximum_disk_cache_size ,
2026-02-04 19:03:41 -05:00
. maximum_disk_cache_entry_size = compute_maximum_disk_cache_entry_size ( maximum_disk_cache_size ) ,
2026-02-04 17:53:00 -05:00
} ;
2026-04-17 16:54:21 +02:00
u64 total_estimated_size { 0 } ;
database . execute_statement (
statements . select_total_estimated_size ,
[ & ] ( auto statement_id ) { total_estimated_size = database . result_column < u64 > ( statement_id , 0 ) ; } ) ;
return CacheIndex { database , statements , limits , total_estimated_size } ;
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
}
2026-04-17 16:54:21 +02:00
CacheIndex : : CacheIndex ( Database : : Database & database , Statements statements , Limits limits , u64 total_estimated_size )
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
: m_database ( database )
, m_statements ( statements )
2026-02-04 17:53:00 -05:00
, m_limits ( limits )
2026-04-17 16:54:21 +02:00
, m_total_estimated_size ( total_estimated_size )
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
{
}
2026-02-04 19:03:41 -05:00
ErrorOr < void > CacheIndex : : create_entry ( u64 cache_key , u64 vary_key , String url , NonnullRefPtr < HeaderList > request_headers , NonnullRefPtr < HeaderList > response_headers , u64 data_size , UnixDateTime request_time , UnixDateTime response_time )
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
{
auto now = UnixDateTime : : now ( ) ;
2025-12-24 11:08:14 -05:00
auto remove_exempted_headers = [ ] ( HeaderList & headers ) {
2026-01-22 12:39:23 -05:00
headers . delete_all_matching ( [ & ] ( auto const & header ) {
return is_header_exempted_from_storage ( header . name ) ;
} ) ;
2025-12-24 11:08:14 -05:00
} ;
remove_exempted_headers ( request_headers ) ;
remove_exempted_headers ( response_headers ) ;
2025-11-16 11:49:06 -05:00
2026-02-04 19:03:41 -05:00
auto serialized_request_headers = serialize_headers ( request_headers ) ;
auto serialized_response_headers = serialize_headers ( response_headers ) ;
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
Entry entry {
2025-12-28 10:23:56 -05:00
. vary_key = vary_key ,
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
. url = move ( url ) ,
2025-12-24 11:08:14 -05:00
. request_headers = move ( request_headers ) ,
2025-10-29 15:36:33 -04:00
. response_headers = move ( response_headers ) ,
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
. data_size = data_size ,
2026-04-17 16:54:21 +02:00
. serialized_request_headers_size = static_cast < u64 > ( serialized_request_headers . length ( ) ) ,
. serialized_response_headers_size = static_cast < u64 > ( serialized_response_headers . length ( ) ) ,
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
. request_time = request_time ,
. response_time = response_time ,
. last_access_time = now ,
} ;
2026-04-17 16:54:21 +02:00
auto entry_size = entry . estimated_size ( ) ;
if ( entry_size > m_limits . maximum_disk_cache_entry_size )
return Error : : from_string_literal ( " Cache entry size exceeds allowed maximum " ) ;
m_database - > execute_statement (
m_statements . remove_entry ,
[ & ] ( auto statement_id ) {
auto removed_size = m_database - > result_column < u64 > ( statement_id , 0 ) ;
m_total_estimated_size - = removed_size ;
} ,
cache_key ,
vary_key ) ;
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
2026-04-17 16:53:04 +02:00
auto & entries = m_entries . ensure ( cache_key ) ;
auto existing_entry_index = entries . find_first_index_if ( [ & ] ( auto const & existing_entry ) {
return existing_entry . vary_key = = vary_key ;
} ) ;
2026-02-04 19:03:41 -05:00
m_database - > execute_statement ( m_statements . insert_entry , { } , cache_key , vary_key , entry . url , serialized_request_headers , serialized_response_headers , entry . data_size , entry . request_time , entry . response_time , entry . last_access_time ) ;
2026-04-17 16:53:04 +02:00
if ( existing_entry_index . has_value ( ) )
entries [ * existing_entry_index ] = move ( entry ) ;
else
entries . append ( move ( entry ) ) ;
2026-02-04 19:03:41 -05:00
2026-04-17 16:54:21 +02:00
m_total_estimated_size + = entry_size ;
2026-02-04 19:03:41 -05:00
return { } ;
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
}
2025-12-28 10:23:56 -05:00
void CacheIndex : : remove_entry ( u64 cache_key , u64 vary_key )
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
{
2026-04-17 16:54:21 +02:00
m_database - > execute_statement (
m_statements . remove_entry ,
[ & ] ( auto statement_id ) {
auto removed_size = m_database - > result_column < u64 > ( statement_id , 0 ) ;
m_total_estimated_size - = removed_size ;
} ,
cache_key ,
vary_key ) ;
2026-02-04 17:53:00 -05:00
delete_entry ( cache_key , vary_key ) ;
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
}
2026-02-04 17:53:00 -05:00
void CacheIndex : : remove_entries_exceeding_cache_limit ( Function < void ( u64 cache_key , u64 vary_key ) > on_entry_removed )
2025-10-09 14:24:47 -04:00
{
2026-04-17 16:54:21 +02:00
if ( m_total_estimated_size < = m_limits . maximum_disk_cache_size )
return ;
2025-11-28 10:04:59 -05:00
m_database - > execute_statement (
2026-02-04 17:53:00 -05:00
m_statements . remove_entries_exceeding_cache_limit ,
2025-11-02 16:44:30 -05:00
[ & ] ( auto statement_id ) {
2025-11-28 10:04:59 -05:00
auto cache_key = m_database - > result_column < u64 > ( statement_id , 0 ) ;
2025-12-28 10:23:56 -05:00
auto vary_key = m_database - > result_column < u64 > ( statement_id , 1 ) ;
2026-04-17 16:54:21 +02:00
auto removed_size = m_database - > result_column < u64 > ( statement_id , 2 ) ;
m_total_estimated_size - = removed_size ;
2026-02-04 17:53:00 -05:00
delete_entry ( cache_key , vary_key ) ;
2025-11-02 16:44:30 -05:00
2026-02-04 17:53:00 -05:00
if ( on_entry_removed )
on_entry_removed ( cache_key , vary_key ) ;
} ,
m_limits . maximum_disk_cache_size ) ;
}
2025-12-28 10:23:56 -05:00
2026-02-04 17:53:00 -05:00
void CacheIndex : : remove_entries_accessed_since ( UnixDateTime since , Function < void ( u64 cache_key , u64 vary_key ) > on_entry_removed )
{
m_database - > execute_statement (
m_statements . remove_entries_accessed_since ,
[ & ] ( auto statement_id ) {
auto cache_key = m_database - > result_column < u64 > ( statement_id , 0 ) ;
auto vary_key = m_database - > result_column < u64 > ( statement_id , 1 ) ;
2026-04-17 16:54:21 +02:00
auto removed_size = m_database - > result_column < u64 > ( statement_id , 2 ) ;
m_total_estimated_size - = removed_size ;
2026-02-04 17:53:00 -05:00
delete_entry ( cache_key , vary_key ) ;
2025-12-28 10:23:56 -05:00
2026-02-04 17:53:00 -05:00
if ( on_entry_removed )
2025-12-28 10:23:56 -05:00
on_entry_removed ( cache_key , vary_key ) ;
2025-11-02 16:44:30 -05:00
} ,
since ) ;
2025-10-09 14:24:47 -04:00
}
2025-12-28 10:23:56 -05:00
void CacheIndex : : update_response_headers ( u64 cache_key , u64 vary_key , NonnullRefPtr < HeaderList > response_headers )
2025-10-28 17:09:35 -04:00
{
2025-12-28 10:23:56 -05:00
auto entry = get_entry ( cache_key , vary_key ) ;
2025-10-28 17:09:35 -04:00
if ( ! entry . has_value ( ) )
return ;
2026-04-17 16:54:21 +02:00
auto serialized_response_headers = serialize_headers ( response_headers ) ;
auto serialized_response_headers_size = static_cast < u64 > ( serialized_response_headers . length ( ) ) ;
m_database - > execute_statement ( m_statements . update_response_headers , { } , serialized_response_headers , cache_key , vary_key ) ;
m_total_estimated_size - = entry - > serialized_response_headers_size ;
m_total_estimated_size + = serialized_response_headers_size ;
2025-10-28 17:09:35 -04:00
entry - > response_headers = move ( response_headers ) ;
2026-04-17 16:54:21 +02:00
entry - > serialized_response_headers_size = serialized_response_headers_size ;
2025-10-28 17:09:35 -04:00
}
2025-12-28 10:23:56 -05:00
void CacheIndex : : update_last_access_time ( u64 cache_key , u64 vary_key )
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
{
2025-12-28 10:23:56 -05:00
auto entry = get_entry ( cache_key , vary_key ) ;
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
if ( ! entry . has_value ( ) )
return ;
auto now = UnixDateTime : : now ( ) ;
2026-02-06 08:24:42 -05:00
m_database - > execute_statement ( m_statements . update_last_access_time , { } , now , cache_key , vary_key ) ;
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
entry - > last_access_time = now ;
}
2025-12-28 10:23:56 -05:00
Optional < CacheIndex : : Entry const & > CacheIndex : : find_entry ( u64 cache_key , HeaderList const & request_headers )
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
{
2025-12-28 10:23:56 -05:00
auto & entries = m_entries . ensure ( cache_key , [ & ] ( ) {
Vector < Entry > entries ;
m_database - > execute_statement (
m_statements . select_entries ,
[ & ] ( auto statement_id ) {
int column = 1 ; // Skip the cache_key column.
auto vary_key = m_database - > result_column < u64 > ( statement_id , column + + ) ;
auto url = m_database - > result_column < String > ( statement_id , column + + ) ;
auto request_headers = m_database - > result_column < ByteString > ( statement_id , column + + ) ;
auto response_headers = m_database - > result_column < ByteString > ( statement_id , column + + ) ;
auto data_size = m_database - > result_column < u64 > ( statement_id , column + + ) ;
auto request_time = m_database - > result_column < UnixDateTime > ( statement_id , column + + ) ;
auto response_time = m_database - > result_column < UnixDateTime > ( statement_id , column + + ) ;
auto last_access_time = m_database - > result_column < UnixDateTime > ( statement_id , column + + ) ;
2026-04-17 16:54:21 +02:00
entries . empend ( vary_key , move ( url ) , deserialize_headers ( request_headers ) , deserialize_headers ( response_headers ) , data_size , request_headers . length ( ) , response_headers . length ( ) , request_time , response_time , last_access_time ) ;
2025-12-28 10:23:56 -05:00
} ,
cache_key ) ;
return entries ;
} ) ;
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
2025-12-28 10:23:56 -05:00
return find_value ( entries , [ & ] ( auto const & entry ) {
return create_vary_key ( request_headers , entry . response_headers ) = = entry . vary_key ;
} ) ;
}
Optional < CacheIndex : : Entry & > CacheIndex : : get_entry ( u64 cache_key , u64 vary_key )
{
auto entries = m_entries . get ( cache_key ) ;
if ( ! entries . has_value ( ) )
return { } ;
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
2025-12-28 10:23:56 -05:00
return find_value ( * entries , [ & ] ( auto const & entry ) { return entry . vary_key = = vary_key ; } ) ;
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
}
2026-02-04 17:53:00 -05:00
void CacheIndex : : delete_entry ( u64 cache_key , u64 vary_key )
{
auto entries = m_entries . get ( cache_key ) ;
if ( ! entries . has_value ( ) )
return ;
entries - > remove_first_matching ( [ & ] ( auto const & entry ) { return entry . vary_key = = vary_key ; } ) ;
if ( entries - > is_empty ( ) )
m_entries . remove ( cache_key ) ;
}
2025-11-28 10:04:59 -05:00
Requests : : CacheSizes CacheIndex : : estimate_cache_size_accessed_since ( UnixDateTime since )
2025-11-02 13:10:27 -05:00
{
Requests : : CacheSizes sizes ;
2025-11-28 10:04:59 -05:00
m_database - > execute_statement (
2025-11-02 13:10:27 -05:00
m_statements . estimate_cache_size_accessed_since ,
2025-11-28 10:04:59 -05:00
[ & ] ( auto statement_id ) { sizes . since_requested_time = m_database - > result_column < u64 > ( statement_id , 0 ) ; } ,
2025-11-02 13:10:27 -05:00
since ) ;
2025-11-28 10:04:59 -05:00
m_database - > execute_statement (
2025-11-02 13:10:27 -05:00
m_statements . estimate_cache_size_accessed_since ,
2025-11-28 10:04:59 -05:00
[ & ] ( auto statement_id ) { sizes . total = m_database - > result_column < u64 > ( statement_id , 0 ) ; } ,
2025-11-02 13:10:27 -05:00
UnixDateTime : : earliest ( ) ) ;
return sizes ;
}
2026-02-05 12:25:14 -05:00
void CacheIndex : : set_maximum_disk_cache_size ( u64 maximum_disk_cache_size )
{
if ( maximum_disk_cache_size = = m_limits . maximum_disk_cache_size )
return ;
m_limits . maximum_disk_cache_size = compute_maximum_disk_cache_size ( m_limits . free_disk_space , maximum_disk_cache_size ) ;
m_limits . maximum_disk_cache_entry_size = compute_maximum_disk_cache_entry_size ( m_limits . maximum_disk_cache_size ) ;
}
LibRequests+RequestServer: Begin implementing an HTTP disk cache
This adds a disk cache for HTTP responses received from the network. For
now, we take a rather conservative approach to caching. We don't cache a
response until we're 100% sure it is cacheable (there are heuristics we
can implement in the future based on the absence of specific headers).
The cache is broken into 2 categories of files:
1. An index file. This is a SQL database containing metadata about each
cache entry (URL, timestamps, etc.).
2. Cache files. Each cached response is in its own file. The file is an
amalgamation of all info needed to reconstruct an HTTP response. This
includes the status code, headers, body, etc.
A cache entry is created once we receive the headers for a response. The
index, however, is not updated at this point. We stream the body into
the cache entry as it is received. Once we've successfully cached the
entire body, we create an index entry in the database. If any of these
steps failed along the way, the cache entry is removed and the index is
left untouched.
Subsequent requests are checked for cache hits from the index. If a hit
is found, we read just enough of the cache entry to inform WebContent of
the status code and headers. The body of the response is piped to WC via
syscalls, such that the transfer happens entirely in the kernel; no need
to allocate the memory for the body in userspace (WC still allocates a
buffer to hold the data, of course). If an error occurs while piping the
body, we currently error out the request. There is a FIXME to switch to
a network request.
Cache hits are also validated for freshness before they are used. If a
response has expired, we remove it and its index entry, and proceed with
a network request.
2025-10-07 19:59:21 -04:00
}