Skip to content

Commit 42bcc36

Browse files
committed
deleteCookies
1 parent d9ce89a commit 42bcc36

File tree

2 files changed

+125
-70
lines changed

2 files changed

+125
-70
lines changed

src/browser/storage/cookie.zig

Lines changed: 77 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ pub const Jar = struct {
3232
self.cookies.deinit(self.allocator);
3333
}
3434

35+
pub fn clear(self: *Jar) void {
36+
for (self.cookies.items) |c| {
37+
c.deinit();
38+
}
39+
self.cookies.clearRetainingCapacity();
40+
}
41+
3542
pub fn add(
3643
self: *Jar,
3744
cookie: Cookie,
@@ -173,43 +180,43 @@ pub const Jar = struct {
173180
}
174181
};
175182

176-
pub const CookieList = struct {
177-
_cookies: std.ArrayListUnmanaged(*const Cookie) = .{},
178-
179-
pub fn deinit(self: *CookieList, allocator: Allocator) void {
180-
self._cookies.deinit(allocator);
181-
}
182-
183-
pub fn cookies(self: *const CookieList) []*const Cookie {
184-
return self._cookies.items;
185-
}
186-
187-
pub fn len(self: *const CookieList) usize {
188-
return self._cookies.items.len;
189-
}
190-
191-
pub fn write(self: *const CookieList, writer: anytype) !void {
192-
const all = self._cookies.items;
193-
if (all.len == 0) {
194-
return;
195-
}
196-
try writeCookie(all[0], writer);
197-
for (all[1..]) |cookie| {
198-
try writer.writeAll("; ");
199-
try writeCookie(cookie, writer);
200-
}
201-
}
202-
203-
fn writeCookie(cookie: *const Cookie, writer: anytype) !void {
204-
if (cookie.name.len > 0) {
205-
try writer.writeAll(cookie.name);
206-
try writer.writeByte('=');
207-
}
208-
if (cookie.value.len > 0) {
209-
try writer.writeAll(cookie.value);
210-
}
211-
}
212-
};
183+
// pub const CookieList = struct {
184+
// _cookies: std.ArrayListUnmanaged(*const Cookie) = .{},
185+
186+
// pub fn deinit(self: *CookieList, allocator: Allocator) void {
187+
// self._cookies.deinit(allocator);
188+
// }
189+
190+
// pub fn cookies(self: *const CookieList) []*const Cookie {
191+
// return self._cookies.items;
192+
// }
193+
194+
// pub fn len(self: *const CookieList) usize {
195+
// return self._cookies.items.len;
196+
// }
197+
198+
// pub fn write(self: *const CookieList, writer: anytype) !void {
199+
// const all = self._cookies.items;
200+
// if (all.len == 0) {
201+
// return;
202+
// }
203+
// try writeCookie(all[0], writer);
204+
// for (all[1..]) |cookie| {
205+
// try writer.writeAll("; ");
206+
// try writeCookie(cookie, writer);
207+
// }
208+
// }
209+
210+
// fn writeCookie(cookie: *const Cookie, writer: anytype) !void {
211+
// if (cookie.name.len > 0) {
212+
// try writer.writeAll(cookie.name);
213+
// try writer.writeByte('=');
214+
// }
215+
// if (cookie.value.len > 0) {
216+
// try writer.writeAll(cookie.value);
217+
// }
218+
// }
219+
// };
213220

214221
fn isCookieExpired(cookie: *const Cookie, now: i64) bool {
215222
const ce = cookie.expires orelse return false;
@@ -660,39 +667,39 @@ test "Jar: forRequest" {
660667
// the 'global2' cookie
661668
}
662669

663-
test "CookieList: write" {
664-
var arr: std.ArrayListUnmanaged(u8) = .{};
665-
defer arr.deinit(testing.allocator);
666-
667-
var cookie_list = CookieList{};
668-
defer cookie_list.deinit(testing.allocator);
669-
670-
const c1 = try Cookie.parse(testing.allocator, &test_uri, "cookie_name=cookie_value");
671-
defer c1.deinit();
672-
{
673-
try cookie_list._cookies.append(testing.allocator, &c1);
674-
try cookie_list.write(arr.writer(testing.allocator));
675-
try testing.expectEqual("cookie_name=cookie_value", arr.items);
676-
}
677-
678-
const c2 = try Cookie.parse(testing.allocator, &test_uri, "x84");
679-
defer c2.deinit();
680-
{
681-
arr.clearRetainingCapacity();
682-
try cookie_list._cookies.append(testing.allocator, &c2);
683-
try cookie_list.write(arr.writer(testing.allocator));
684-
try testing.expectEqual("cookie_name=cookie_value; x84", arr.items);
685-
}
686-
687-
const c3 = try Cookie.parse(testing.allocator, &test_uri, "nope=");
688-
defer c3.deinit();
689-
{
690-
arr.clearRetainingCapacity();
691-
try cookie_list._cookies.append(testing.allocator, &c3);
692-
try cookie_list.write(arr.writer(testing.allocator));
693-
try testing.expectEqual("cookie_name=cookie_value; x84; nope=", arr.items);
694-
}
695-
}
670+
// test "CookieList: write" {
671+
// var arr: std.ArrayListUnmanaged(u8) = .{};
672+
// defer arr.deinit(testing.allocator);
673+
674+
// var cookie_list = CookieList{};
675+
// defer cookie_list.deinit(testing.allocator);
676+
677+
// const c1 = try Cookie.parse(testing.allocator, &test_uri, "cookie_name=cookie_value");
678+
// defer c1.deinit();
679+
// {
680+
// try cookie_list._cookies.append(testing.allocator, &c1);
681+
// try cookie_list.write(arr.writer(testing.allocator));
682+
// try testing.expectEqual("cookie_name=cookie_value", arr.items);
683+
// }
684+
685+
// const c2 = try Cookie.parse(testing.allocator, &test_uri, "x84");
686+
// defer c2.deinit();
687+
// {
688+
// arr.clearRetainingCapacity();
689+
// try cookie_list._cookies.append(testing.allocator, &c2);
690+
// try cookie_list.write(arr.writer(testing.allocator));
691+
// try testing.expectEqual("cookie_name=cookie_value; x84", arr.items);
692+
// }
693+
694+
// const c3 = try Cookie.parse(testing.allocator, &test_uri, "nope=");
695+
// defer c3.deinit();
696+
// {
697+
// arr.clearRetainingCapacity();
698+
// try cookie_list._cookies.append(testing.allocator, &c3);
699+
// try cookie_list.write(arr.writer(testing.allocator));
700+
// try testing.expectEqual("cookie_name=cookie_value; x84; nope=", arr.items);
701+
// }
702+
// }
696703

697704
test "Cookie: parse key=value" {
698705
try expectError(error.Empty, null, "");

src/cdp/domains/network.zig

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ pub fn processMessage(cmd: anytype) !void {
2828
disable,
2929
setCacheDisabled,
3030
setExtraHTTPHeaders,
31+
deleteCookies,
3132
}, cmd.input.action) orelse return error.UnknownMethod;
3233

3334
switch (action) {
3435
.enable => return enable(cmd),
3536
.disable => return disable(cmd),
3637
.setCacheDisabled => return cmd.sendResult(null, .{}),
3738
.setExtraHTTPHeaders => return setExtraHTTPHeaders(cmd),
39+
.deleteCookies => return deleteCookies(cmd),
3840
}
3941
}
4042

@@ -71,6 +73,52 @@ fn setExtraHTTPHeaders(cmd: anytype) !void {
7173
return cmd.sendResult(null, .{});
7274
}
7375

76+
// const CookiePartitionKey = struct {
77+
// topLevelSite: []const u8,
78+
// hasCrossSiteAncestor: bool,
79+
// };
80+
81+
const Cookie = @import("../../browser/storage/storage.zig").Cookie;
82+
const CookieJar = @import("../../browser/storage/storage.zig").CookieJar;
83+
84+
fn cookieMatches(cookie: *const Cookie, name: []const u8, url: ?[]const u8, domain: ?[]const u8, path: ?[]const u8) bool {
85+
if (!std.mem.eql(u8, cookie.name, name)) return false;
86+
87+
_ = url; // TODO
88+
89+
if (domain) |domain_| {
90+
if (!std.mem.eql(u8, cookie.domain, domain_)) return false;
91+
}
92+
if (path) |path_| {
93+
if (!std.mem.eql(u8, cookie.path, path_)) return false;
94+
}
95+
96+
return true;
97+
}
98+
99+
fn deleteCookies(cmd: anytype) !void {
100+
const params = (try cmd.params(struct {
101+
name: []const u8,
102+
url: ?[]const u8 = null,
103+
domain: ?[]const u8 = null,
104+
path: ?[]const u8 = null,
105+
// partitionKey: ?CookiePartitionKey,
106+
})) orelse return error.InvalidParams;
107+
108+
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
109+
const cookies = &bc.session.cookie_jar.cookies;
110+
111+
var index = cookies.items.len;
112+
while (index > 0) {
113+
index -= 1;
114+
const cookie = &cookies.items[index];
115+
if (cookieMatches(cookie, params.name, params.url, params.domain, params.path)) {
116+
cookies.swapRemove(index).deinit();
117+
}
118+
}
119+
return cmd.sendResult(null, .{});
120+
}
121+
74122
// Upsert a header into the headers array.
75123
// returns true if the header was added, false if it was updated
76124
fn putAssumeCapacity(headers: *std.ArrayListUnmanaged(std.http.Header), extra: std.http.Header) bool {

0 commit comments

Comments
 (0)