lower case domain

This commit is contained in:
sjorsdonkers
2025-06-11 15:57:14 +02:00
committed by Sjors
parent fe16f06aee
commit 18dff8455c
2 changed files with 34 additions and 22 deletions

View File

@@ -372,7 +372,7 @@ pub const Cookie = struct {
if (std.mem.endsWith(u8, host, value) == false) {
return error.InvalidDomain;
}
domain = value; // TODO to lower case: https://www.rfc-editor.org/rfc/rfc6265#section-5.2.3
domain = value; // Domain is made lower case after it has relocated to the arena
},
.secure => secure = true,
.@"max-age" => max_age = std.fmt.parseInt(i64, value, 10) catch continue,
@@ -406,6 +406,7 @@ pub const Cookie = struct {
} else blk: {
break :blk try aa.dupe(u8, host);
};
_ = toLower(owned_domain);
var normalized_expires: ?i64 = null;
if (max_age) |ma| {
@@ -470,6 +471,13 @@ fn trimRight(str: []const u8) []const u8 {
return std.mem.trimLeft(u8, str, &std.ascii.whitespace);
}
pub fn toLower(str: []u8) []u8 {
for (str, 0..) |c, i| {
str[i] = std.ascii.toLower(c);
}
return str;
}
const testing = @import("../../testing.zig");
test "cookie: findSecondLevelDomain" {
const cases = [_]struct { []const u8, []const u8 }{

View File

@@ -160,27 +160,6 @@ fn isHostChar(c: u8) bool {
};
}
// Note: Chrome does not apply rules like removing a leading `.` from the domain.
fn percentEncodedDomain(allocator: Allocator, default_url: ?[]const u8, domain: ?[]const u8) !?[]const u8 {
if (domain) |domain_| {
return try allocator.dupe(u8, domain_);
} else if (default_url) |url| {
const uri = std.Uri.parse(url) catch return error.InvalidParams;
switch (uri.host orelse return error.InvalidParams) {
.raw => |str| {
var list = std.ArrayList(u8).init(allocator);
try list.ensureTotalCapacity(str.len); // Expect no precents needed
try std.Uri.Component.percentEncode(list.writer(), str, isHostChar);
return list.items; // @memory retains memory used before growing
},
.percent_encoded => |str| {
return try allocator.dupe(u8, str);
},
}
} else return null;
}
const CdpCookie = struct {
name: []const u8,
value: []const u8,
@@ -254,6 +233,31 @@ fn setCdpCookie(cookie_jar: *CookieJar, param: CdpCookie) !void {
try cookie_jar.add(cookie, std.time.timestamp());
}
// Note: Chrome does not apply rules like removing a leading `.` from the domain.
fn percentEncodedDomain(allocator: Allocator, default_url: ?[]const u8, domain: ?[]const u8) !?[]const u8 {
const toLower = @import("../../browser/storage/cookie.zig").toLower;
if (domain) |domain_| {
const output = try allocator.dupe(u8, domain_);
return toLower(output);
} else if (default_url) |url| {
const uri = std.Uri.parse(url) catch return error.InvalidParams;
var output: []u8 = undefined;
switch (uri.host orelse return error.InvalidParams) {
.raw => |str| {
var list = std.ArrayList(u8).init(allocator);
try list.ensureTotalCapacity(str.len); // Expect no precents needed
try std.Uri.Component.percentEncode(list.writer(), str, isHostChar);
output = list.items; // @memory retains memory used before growing
},
.percent_encoded => |str| {
output = try allocator.dupe(u8, str);
},
}
return toLower(output);
} else return null;
}
// Upsert a header into the headers array.
// returns true if the header was added, false if it was updated
fn putAssumeCapacity(headers: *std.ArrayListUnmanaged(std.http.Header), extra: std.http.Header) bool {