@@ -23,7 +23,6 @@ const log = @import("../../log.zig");
2323const Cookie = @import ("../../browser/storage/storage.zig" ).Cookie ;
2424const CookieJar = @import ("../../browser/storage/storage.zig" ).CookieJar ;
2525pub const PreparedUri = @import ("../../browser/storage/cookie.zig" ).PreparedUri ;
26- pub const toLower = @import ("../../browser/storage/cookie.zig" ).toLower ;
2726
2827pub fn processMessage (cmd : anytype ) ! void {
2928 const action = std .meta .stringToEnum (enum {
@@ -143,13 +142,15 @@ pub fn setCdpCookie(cookie_jar: *CookieJar, param: CdpCookie) !void {
143142
144143 // NOTE: The param.url can affect the default domain, path, source port, and source scheme.
145144 const uri = if (param .url ) | url | std .Uri .parse (url ) catch return error .InvalidParams else null ;
146- const domain = try percentEncodedDomainOrHost (a , uri , param .domain ) orelse return error .InvalidParams ; // TODO Domain needs to be prefixed with a dot if is explicitely set
145+ const uri_ptr = if (uri ) | * u | u else null ;
146+ const domain = try Cookie .parseDomain (a , uri_ptr , param .domain );
147+ const path = try Cookie .parsePath (a , uri_ptr , param .path );
147148
148149 const cookie = Cookie {
149150 .arena = arena ,
150151 .name = try a .dupe (u8 , param .name ),
151152 .value = try a .dupe (u8 , param .value ),
152- .path = if ( param . path ) | path | try a . dupe ( u8 , path ) else "/" , // Chrome does not actually take the path from the url and just defaults to "/".
153+ .path = path ,
153154 .domain = domain ,
154155 .expires = param .expires ,
155156 .secure = param .secure ,
@@ -163,51 +164,6 @@ pub fn setCdpCookie(cookie_jar: *CookieJar, param: CdpCookie) !void {
163164 try cookie_jar .add (cookie , std .time .timestamp ());
164165}
165166
166- // Note: Chrome does not apply rules like removing a leading `.` from the domain.
167- pub fn percentEncodedDomainOrHost (allocator : Allocator , default_url : ? std.Uri , domain : ? []const u8 ) ! ? []const u8 {
168- if (domain ) | domain_ | {
169- const output = try allocator .dupe (u8 , domain_ );
170- return toLower (output );
171- } else if (default_url ) | url | {
172- const host = url .host orelse return error .InvalidParams ;
173- const output = try percentEncode (allocator , host , isHostChar ); // TODO remove subdomains
174- return toLower (output );
175- } else return null ;
176- }
177-
178- pub fn percentEncode (arena : Allocator , component : std.Uri.Component , comptime isValidChar : fn (u8 ) bool ) ! []u8 {
179- switch (component ) {
180- .raw = > | str | {
181- var list = std .ArrayList (u8 ).init (arena );
182- try list .ensureTotalCapacity (str .len ); // Expect no precents needed
183- try std .Uri .Component .percentEncode (list .writer (), str , isValidChar );
184- return list .items ; // @memory retains memory used before growing
185- },
186- .percent_encoded = > | str | {
187- return try arena .dupe (u8 , str );
188- },
189- }
190- }
191-
192- pub fn isHostChar (c : u8 ) bool {
193- return switch (c ) {
194- 'A' ... 'Z' , 'a' ... 'z' , '0' ... '9' , '-' , '.' , '_' , '~' = > true ,
195- '!' , '$' , '&' , '\' ' , '(' , ')' , '*' , '+' , ',' , ';' , '=' = > true ,
196- ':' = > true ,
197- '[' , ']' = > true ,
198- else = > false ,
199- };
200- }
201-
202- pub fn isPathChar (c : u8 ) bool {
203- return switch (c ) {
204- 'A' ... 'Z' , 'a' ... 'z' , '0' ... '9' , '-' , '.' , '_' , '~' = > true ,
205- '!' , '$' , '&' , '\' ' , '(' , ')' , '*' , '+' , ',' , ';' , '=' = > true ,
206- '/' , ':' , '@' = > true ,
207- else = > false ,
208- };
209- }
210-
211167pub const CookieWriter = struct {
212168 cookies : []const Cookie ,
213169 urls : ? []const PreparedUri = null ,
0 commit comments