Lightpanda: the headless browser designed for AI and automation
serve: honor --cookie-jar
Adrià Arrufat634fd2a72bae
diff --git a/src/Config.zig b/src/Config.zigindex 8c3114e92..e5fcfcb44 100644--- a/src/Config.zig+++ b/src/Config.zig@@ -842,7 +842,7 @@ pub fn cookieFile(self: *const Config) ?[]const u8 {pub fn cookieJarFile(self: *const Config) ?[]const u8 {return switch (self.mode) {- inline .fetch, .mcp, .agent => |opts| opts.cookie_jar,+ inline .serve, .fetch, .mcp, .agent => |opts| opts.cookie_jar,else => null,};}diff --git a/src/cookies.zig b/src/cookies.zigindex 15ab01b32..8e8bad1aa 100644--- a/src/cookies.zig+++ b/src/cookies.zig@@ -91,13 +91,16 @@ pub fn saveToFile(jar: *Cookie.Jar, path: []const u8) void {fn _saveToFile(jar: *Cookie.Jar, path: []const u8) !void {jar.removeExpired(null);- const file = try std.Io.Dir.cwd().createFile(lp.io, path, .{});- defer file.close(lp.io);+ // temp+rename: `serve` saves once per CDP connection, so several+ // connections may write the same jar; a reader never sees a torn file.+ var af = try std.Io.Dir.cwd().createFileAtomic(lp.io, path, .{ .replace = true });+ defer af.deinit(lp.io);var buf: [8192]u8 = undefined;- var writer = file.writer(lp.io, &buf);+ var writer = af.file.writer(lp.io, &buf);try writeJson(jar, &writer.interface);try writer.end();+ try af.replace(lp.io);log.info(.app, "Cookie.saveToFile", .{ .path = path, .count = jar.cookies.items.len });}diff --git a/src/help.zon b/src/help.zonindex 349e25caf..1dc2ce44f 100644--- a/src/help.zon+++ b/src/help.zon@@ -46,6 +46,12 @@\\ cookies.txt format written by curl and wget). The format is\\ detected from the file's contents.\\ Defaults to no cookie loading.+ \\ --cookie-jar <PATH>+ \\ Path to a JSON file to save cookies to (write-only). Written when+ \\ a CDP browser context is disposed: on disconnect, on+ \\ Target.disposeBrowserContext and on shutdown. Pass the same path+ \\ to --cookie to keep logins across restarts.+ \\ Defaults to no cookie saving.\\ --disable-metrics\\ Disables the /metrics endpoint (Prometheus text format).\\ Defaults to false (the endpoint is exposed).diff --git a/src/server/cdp/CDP.zig b/src/server/cdp/CDP.zigindex 895061c74..4e0fb9a62 100644--- a/src/server/cdp/CDP.zig+++ b/src/server/cdp/CDP.zig@@ -654,6 +654,10 @@ pub const BrowserContext = struct {self.notification.unregisterAll(self);self.clearCapturedResponses();+ if (self.cdp.app.config.cookieJarFile()) |cookie_jar_path| {+ lp.cookies.saveToFile(&self.session.cookie_jar, cookie_jar_path);+ }+// If the session has a frame, we need to clear it first. The page// context is always nested inside of the isolated world context,// so we need to shutdown the page one first.@@ -1776,3 +1780,38 @@ test "cdp: syncRequest short-circuits after disconnect" {}, null);try testing.expectError(error.ClientDisconnected, transfer.submitSync(.{}));}++test "cdp: --cookie-jar saves on dispose and --cookie loads it back" {+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const path = try testing.tmpPath(&tmp, "cookies.json");++ const opts = &testing.base.test_config.mode.serve;+ defer {+ opts.cookie = null;+ opts.cookie_jar = null;+ }+ opts.cookie = path;+ opts.cookie_jar = path;++ var ctx = try testing.context();+ defer ctx.deinit();++ {+ // no file yet: the context starts with an empty jar+ const bc = try ctx.loadBrowserContext(.{});+ try testing.expectEqual(0, bc.session.cookie_jar.cookies.items.len);+ try bc.session.cookie_jar.populateFromResponse("https://example.com/", "sid=abc; Path=/; Max-Age=3600; HttpOnly");+ ctx.cdp().disposeBrowserContext();+ }++ const written = try std.Io.Dir.cwd().readFileAlloc(lp.io, path, testing.arena_allocator, .limited(4096));+ try testing.expect(std.mem.find(u8, written, "\"name\":\"sid\",\"value\":\"abc\"") != null);++ // a new context (a reconnect, or a restart) starts logged in+ const bc = try ctx.loadBrowserContext(.{});+ const cookies = bc.session.cookie_jar.cookies.items;+ try testing.expectEqual(1, cookies.len);+ try testing.expectEqualSlices(u8, "abc", cookies[0].value);+ try testing.expectEqual(true, cookies[0].http_only);+}