Skip to content

Lightpanda: the headless browser designed for AI and automation

Keep the jar in sort order

Karl Seguind68eef7f1270
diff --git a/src/browser/webapi/storage/Cookie.zig b/src/browser/webapi/storage/Cookie.zig
index 77564480d..3ccfd3a61 100644
--- a/src/browser/webapi/storage/Cookie.zig
+++ b/src/browser/webapi/storage/Cookie.zig
@@ -706,8 +706,7 @@ pub const Jar = struct {
// Dispatch while c still points at the live old cookie,
// then free its arena, then remove from the array. After
// orderedRemove, items[i] holds a different (still-live)
- // entry, so deinit must happen before that. orderedRemove
- // keeps the jar in creation order for forRequest.
+ // entry, so deinit must happen before that.
self.dispatchChange(.deleted, c);
c.deinit();
_ = self.cookies.orderedRemove(i);
@@ -726,9 +725,14 @@ pub const Jar = struct {
}
if (!is_expired) {
- try self.cookies.append(self.allocator, cookie);
+ // keep it in send order: longest path then creation time.
+ var pos = self.cookies.items.len;
+ while (pos > 0 and sendsBefore(&cookie, &self.cookies.items[pos - 1])) {
+ pos -= 1;
+ }
+ try self.cookies.insert(self.allocator, pos, cookie);
stored = true;
- self.dispatchChange(.changed, &self.cookies.items[self.cookies.items.len - 1]);
+ self.dispatchChange(.changed, &self.cookies.items[pos]);
}
}
@@ -779,8 +783,7 @@ pub const Jar = struct {
removeExpired(self, now);
- var matches: std.ArrayList(*const Cookie) = .empty;
- defer matches.deinit(self.allocator);
+ var first = true;
for (self.cookies.items) |*cookie| {
const applies = cookie.appliesTo(&target, .{
.same_site = same_site,
@@ -788,21 +791,16 @@ pub const Jar = struct {
.kind = opts.kind,
.now = now,
});
- if (applies) {
- try matches.append(self.allocator, cookie);
+ if (!applies) {
+ continue;
}
- }
-
- // RFC 6265bis 5.8.3: longer paths first, then earlier creation times.
- // The sort is stable, so cookies created in the same second keep the
- // order they were added in.
- std.sort.insertion(*const Cookie, matches.items, {}, sendsBefore);
- for (matches.items, 0..) |cookie, i| {
- if (i == 0) {
+ // we have a match!
+ if (first) {
if (opts.prefix) |prefix| {
try writer.writeAll(prefix);
}
+ first = false;
} else {
try writer.writeAll("; ");
}
@@ -810,7 +808,7 @@ pub const Jar = struct {
}
}
- fn sendsBefore(_: void, a: *const Cookie, b: *const Cookie) bool {
+ fn sendsBefore(a: *const Cookie, b: *const Cookie) bool {
if (a.path.len != b.path.len) {
return a.path.len > b.path.len;
}
diff --git a/src/server/bidi/storage.zig b/src/server/bidi/storage.zig
index 76f55d484..f6aa71068 100644
--- a/src/server/bidi/storage.zig
+++ b/src/server/bidi/storage.zig
@@ -88,7 +88,7 @@ pub fn remove(jar: *Cookie.Jar, context: anytype, comptime matches: fn (@TypeOf(
while (i > 0) {
i -= 1;
if (matches(context, &cookies.items[i])) {
- cookies.swapRemove(i).deinit();
+ cookies.orderedRemove(i).deinit();
}
}
}
diff --git a/src/server/cdp/domains/network.zig b/src/server/cdp/domains/network.zig
index 4407d3986..f076b6972 100644
--- a/src/server/cdp/domains/network.zig
+++ b/src/server/cdp/domains/network.zig
@@ -323,7 +323,7 @@ fn deleteCookies(cmd: *CDP.Command) !void {
// We do not want to use Cookie.appliesTo here. As a Cookie with a shorter path would match.
// Similar to deduplicating with areCookiesEqual, except domain and path are optional.
if (cookieMatches(cookie, params.name, domain, path)) {
- cookies.swapRemove(index).deinit();
+ cookies.orderedRemove(index).deinit();
}
}
return cmd.sendResult(null, .{});