Skip to content

Commit d03b73f

Browse files
committed
lower case domain
1 parent ccc1e24 commit d03b73f

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

src/browser/storage/cookie.zig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ pub const Cookie = struct {
372372
if (std.mem.endsWith(u8, host, value) == false) {
373373
return error.InvalidDomain;
374374
}
375-
domain = value; // TODO to lower case: https://www.rfc-editor.org/rfc/rfc6265#section-5.2.3
375+
domain = value; // Domain is made lower case after it has relocated to the arena
376376
},
377377
.secure => secure = true,
378378
.@"max-age" => max_age = std.fmt.parseInt(i64, value, 10) catch continue,
@@ -406,6 +406,7 @@ pub const Cookie = struct {
406406
} else blk: {
407407
break :blk try aa.dupe(u8, host);
408408
};
409+
_ = toLower(owned_domain);
409410

410411
var normalized_expires: ?i64 = null;
411412
if (max_age) |ma| {
@@ -470,6 +471,13 @@ fn trimRight(str: []const u8) []const u8 {
470471
return std.mem.trimLeft(u8, str, &std.ascii.whitespace);
471472
}
472473

474+
pub fn toLower(str: []u8) []u8 {
475+
for (str, 0..) |c, i| {
476+
str[i] = std.ascii.toLower(c);
477+
}
478+
return str;
479+
}
480+
473481
const testing = @import("../../testing.zig");
474482
test "cookie: findSecondLevelDomain" {
475483
const cases = [_]struct { []const u8, []const u8 }{

src/cdp/domains/network.zig

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -160,27 +160,6 @@ fn isHostChar(c: u8) bool {
160160
};
161161
}
162162

163-
// Note: Chrome does not apply rules like removing a leading `.` from the domain.
164-
fn percentEncodedDomain(allocator: Allocator, default_url: ?[]const u8, domain: ?[]const u8) !?[]const u8 {
165-
if (domain) |domain_| {
166-
return try allocator.dupe(u8, domain_);
167-
} else if (default_url) |url| {
168-
const uri = std.Uri.parse(url) catch return error.InvalidParams;
169-
170-
switch (uri.host orelse return error.InvalidParams) {
171-
.raw => |str| {
172-
var list = std.ArrayList(u8).init(allocator);
173-
try list.ensureTotalCapacity(str.len); // Expect no precents needed
174-
try std.Uri.Component.percentEncode(list.writer(), str, isHostChar);
175-
return list.items; // @memory retains memory used before growing
176-
},
177-
.percent_encoded => |str| {
178-
return try allocator.dupe(u8, str);
179-
},
180-
}
181-
} else return null;
182-
}
183-
184163
const CdpCookie = struct {
185164
name: []const u8,
186165
value: []const u8,
@@ -254,6 +233,31 @@ fn setCdpCookie(cookie_jar: *CookieJar, param: CdpCookie) !void {
254233
try cookie_jar.add(cookie, std.time.timestamp());
255234
}
256235

236+
// Note: Chrome does not apply rules like removing a leading `.` from the domain.
237+
fn percentEncodedDomain(allocator: Allocator, default_url: ?[]const u8, domain: ?[]const u8) !?[]const u8 {
238+
const toLower = @import("../../browser/storage/cookie.zig").toLower;
239+
if (domain) |domain_| {
240+
const output = try allocator.dupe(u8, domain_);
241+
return toLower(output);
242+
} else if (default_url) |url| {
243+
const uri = std.Uri.parse(url) catch return error.InvalidParams;
244+
245+
var output: []u8 = undefined;
246+
switch (uri.host orelse return error.InvalidParams) {
247+
.raw => |str| {
248+
var list = std.ArrayList(u8).init(allocator);
249+
try list.ensureTotalCapacity(str.len); // Expect no precents needed
250+
try std.Uri.Component.percentEncode(list.writer(), str, isHostChar);
251+
output = list.items; // @memory retains memory used before growing
252+
},
253+
.percent_encoded => |str| {
254+
output = try allocator.dupe(u8, str);
255+
},
256+
}
257+
return toLower(output);
258+
} else return null;
259+
}
260+
257261
// Upsert a header into the headers array.
258262
// returns true if the header was added, false if it was updated
259263
fn putAssumeCapacity(headers: *std.ArrayListUnmanaged(std.http.Header), extra: std.http.Header) bool {

0 commit comments

Comments
 (0)