Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / external / lib_http / memory_cache.lucb

#==============================================================================================
#
#   memory_cache - HTTP::MemoryCache, as far as Fetch reaches it with the cache off
#
#   DESCRIPTION:
#       Partial port of Libraries/LibHTTP/Cache/MemoryCache.cpp (lines 1-142), by region p2c.
#       Fetch makes one memory cache per network partition key (HTTPCache::get) and, after an
#       HTTP response's body, finalizes the entry that storing the response created. Looking
#       up and storing entries happen only when the memory cache is enabled
#       (Fetch::Fetching::set_http_memory_cache_enabled; WebContent's option, off by
#       default), and they need LibHTTP/Cache/Utilities (cacheability, freshness and age,
#       cache and vary keys over SHA-1, HTTP dates), which is not ported: open_entry and
#       create_entry trap. With no entry ever created, finalize_entry has nothing to finalize.
#
#       Copyright (c) 2025-2026, Tim Flynn <trflynn89@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from Libraries/LibHTTP/Cache/MemoryCache.cpp:13 HTTP::MemoryCache::create. HTTPCache keeps its caches for the
## process: in the C heap (DESIGN.md ยง3.3 rule 7).
pub func http_memory_cache_create() -> HttpMemoryCache*:
    let saved_atomic_allocator = ak.atomic_allocator
    ak.atomic_allocator = none
    defer ak.atomic_allocator = saved_atomic_allocator
    with memory.heap:
        return new HttpMemoryCache() else trap("out of memory")

## Ported from Libraries/LibHTTP/Cache/MemoryCache.cpp:19 HTTP::MemoryCache::open_entry.
# https://httpwg.org/specs/rfc9111.html#constructing.responses.from.caches
pub func http_memory_cache_open_entry(this: HttpMemoryCache*, url: web_url.Url, method: ak.StringView, request_headers: const HttpHeaderList*, cache_mode: HttpCacheMode) -> const HttpMemoryCacheEntry*?:
    trap("unported: HTTP::MemoryCache::open_entry (LibHTTP/Cache/Utilities)")

## Ported from Libraries/LibHTTP/Cache/MemoryCache.cpp:76 HTTP::MemoryCache::create_entry.
pub func http_memory_cache_create_entry(this: HttpMemoryCache*, url: web_url.Url, method: ak.StringView, request_headers: const HttpHeaderList*, request_time: ak.UnixDateTime, status_code: u32, reason_phrase: ak.ByteString, response_headers: const HttpHeaderList*):
    trap("unported: HTTP::MemoryCache::create_entry (LibHTTP/Cache/Utilities)")

## Ported from Libraries/LibHTTP/Cache/MemoryCache.cpp:112 HTTP::MemoryCache::finalize_entry.
pub func http_memory_cache_finalize_entry(this: HttpMemoryCache*, url: web_url.Url, method: ak.StringView, request_headers: const HttpHeaderList*, status_code: u32, response_headers: const HttpHeaderList*, response_body: ak.ByteBuffer):
    # An entry is pending only once create_entry stored it, and create_entry is not ported: with no pending
    # entry, C++ returns after its cacheability checks and the lookup of the cache key (Utilities, not ported).
    if ak.hash_map_is_empty(&this.m_pending_entries):
        return
    trap("unported: HTTP::MemoryCache::finalize_entry (LibHTTP/Cache/Utilities)")