Skip to content

Commit c31c12d

Browse files
committed
add test for Storage shed, use map.getOrPut
1 parent 68fc87b commit c31c12d

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/storage/cookie.zig

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,67 @@ const ArenaAllocator = std.heap.ArenaAllocator;
55

66
const DateTime = @import("../datetime.zig").DateTime;
77

8+
pub const Jar = struct {
9+
allocator: Allocator,
10+
cookies: std.ArrayListUnmanaged(u8),
11+
12+
pub fn init(allocator: Allocator) Jar {
13+
return .{
14+
.cookies = .{},
15+
.allocator = allocator,
16+
};
17+
}
18+
19+
pub fn deinit(self: *Jar) void {
20+
for (self.cookies.items) |c| {
21+
c.deinit();
22+
}
23+
self.cookies.deinit(self.allocator);
24+
}
25+
26+
pub fn forRequest(
27+
self: *const Jar,
28+
allocator: Allocator,
29+
request_start: i64,
30+
origin_uri: ?Uri,
31+
target_uri: Uri,
32+
navitation: bool,
33+
) !CookieList {
34+
const is_secure = std.mem.eql(u8, target_uri.scheme, "https");
35+
const target_host = (target_uri.host orelse return error.InvalidURI).percent_encoded;
36+
const same_site = try areSameSite(origin_uri, target_host);
37+
38+
var i: usize = 0;
39+
var cookies = self.cookies.items;
40+
while (i < cookies.len) {
41+
const cookie = &cookies[i];
42+
if (cookie.isExpired(request_start)) {
43+
self.swapRemove(i);
44+
// don't increment i !
45+
continue;
46+
}
47+
i += 1;
48+
49+
if (is_secure == false and cookie.secure) {
50+
continue;
51+
}
52+
53+
// www.google.com
54+
55+
if (navitation == false and cookie.same_site != .strict) {
56+
continue;
57+
}
58+
}
59+
}
60+
};
61+
62+
// abc.lightpanda.io is the same site as lightpanda.io or 123.lightpanda.io
63+
// or spice.123.lightpanda.io
64+
fn areSameSite(origin_uri_: ?std.Uri, target_host: []const u8) !bool {
65+
const origin_uri = origin_uri_ orelse return true;
66+
const origin_host = (origin_uri.host orelse return error.InvalidURI).percent_encoded;
67+
}
68+
869
pub const Cookie = struct {
970
arena: ArenaAllocator,
1071
name: []const u8,

0 commit comments

Comments
 (0)