Skip to content

Lightpanda: the headless browser designed for AI and automation

heuristically cache only with Last-Modified header

Muki Kiboigo65cf678822ac
diff --git a/src/network/cache/Cache.zig b/src/network/cache/Cache.zig
index 161789ece..4daa65219 100644
--- a/src/network/cache/Cache.zig
+++ b/src/network/cache/Cache.zig
@@ -276,14 +276,21 @@ pub fn tryCache(
};
const cc = blk: {
- if (cache_control == null) {
+ if (cache_control) |c| {
+ if (CacheControl.parse(c)) |cc| {
+ break :blk cc;
+ }
+ } else if (last_modified != null) {
+ // Requires Last-Modified to be present to heuristically cache.
log.debug(.cache, "heuristic cache", .{ .url = url, .max_age = 86400 });
break :blk CacheControl{ .max_age = 86400, .must_revalidate = false };
}
- if (CacheControl.parse(cache_control.?)) |cc| {
- break :blk cc;
- }
- log.debug(.cache, "no store", .{ .url = url, .cache_control = cache_control.?, .reason = "cache control" });
+
+ log.debug(.cache, "no store", .{
+ .url = url,
+ .cache_control = cache_control orelse "null",
+ .last_modified = last_modified orelse "null",
+ });
return null;
};
@@ -462,6 +469,27 @@ test "Cache: tryCache heuristic when no cache-control" {
false,
false,
);
+ try testing.expectEqual(null, result);
+}
+
+test "Cache: tryCache heuristic when no cache-control with last-modified" {
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
+ defer arena.deinit();
+
+ const result = try tryCache(
+ arena.allocator(),
+ 1000,
+ "https://example.com",
+ 200,
+ "text/html",
+ null,
+ null,
+ null,
+ null,
+ "Wed, 21 Oct 2015 07:28:00 GMT",
+ false,
+ false,
+ );
try testing.expectEqual(@as(u64, 86400), result.?.cache_control.max_age);
try testing.expectEqual(false, result.?.cache_control.must_revalidate);
}