@@ -22,6 +22,7 @@ const Allocator = std.mem.Allocator;
2222const CdpStorage = @import ("storage.zig" );
2323const Transfer = @import ("../../http/Client.zig" ).Transfer ;
2424const Notification = @import ("../../notification.zig" ).Notification ;
25+ const URL = @import ("../../url.zig" ).URL ;
2526
2627pub fn processMessage (cmd : anytype ) ! void {
2728 const action = std .meta .stringToEnum (enum {
@@ -117,22 +118,33 @@ fn deleteCookies(cmd: anytype) !void {
117118 const bc = cmd .browser_context orelse return error .BrowserContextNotLoaded ;
118119 const cookies = & bc .session .cookie_jar .cookies ;
119120
120- const uri = if (params .url ) | url | std .Uri .parse (url ) catch return error .InvalidParams else null ;
121- const uri_ptr = if (uri ) | u | & u else null ;
121+ const maybe_url : ? URL = blk : {
122+ if (params .url ) | url | {
123+ break :blk URL .parse (url , null ) catch return error .InvalidParams ;
124+ }
125+
126+ break :blk null ;
127+ };
122128
123129 var index = cookies .items .len ;
124130 while (index > 0 ) {
125131 index -= 1 ;
126132 const cookie = & cookies .items [index ];
127- const domain = try Cookie .parseDomain (cmd .arena , uri_ptr , params .domain );
128- const path = try Cookie .parsePath (cmd .arena , uri_ptr , params .path );
133+ const domain = try Cookie .parseDomain (cmd .arena , maybe_url , params .domain );
134+ const path = try Cookie .parsePath (cmd .arena , maybe_url , params .path );
129135
130136 // We do not want to use Cookie.appliesTo here. As a Cookie with a shorter path would match.
131137 // Similar to deduplicating with areCookiesEqual, except domain and path are optional.
132138 if (cookieMatches (cookie , params .name , domain , path )) {
133139 cookies .swapRemove (index ).deinit ();
134140 }
135141 }
142+
143+ // Deinit URL if we had.
144+ if (maybe_url ) | url | {
145+ url .deinit ();
146+ }
147+
136148 return cmd .sendResult (null , .{});
137149}
138150
@@ -177,13 +189,14 @@ fn getCookies(cmd: anytype) !void {
177189 const param_urls = params .urls orelse &[_ ][]const u8 {page_url orelse return error .InvalidParams };
178190
179191 var urls = try std .ArrayListUnmanaged (CdpStorage .PreparedUri ).initCapacity (cmd .arena , param_urls .len );
180- for (param_urls ) | url | {
181- const uri = std .Uri .parse (url ) catch return error .InvalidParams ;
192+ for (param_urls ) | url_str | {
193+ const url = URL .parse (url_str , null ) catch return error .InvalidParams ;
194+ defer url .deinit ();
182195
183196 urls .appendAssumeCapacity (.{
184- .host = try Cookie .parseDomain (cmd .arena , & uri , null ),
185- .path = try Cookie .parsePath (cmd .arena , & uri , null ),
186- .secure = std . mem . eql ( u8 , uri . scheme , "https" ),
197+ .host = try Cookie .parseDomain (cmd .arena , url , null ),
198+ .path = try Cookie .parsePath (cmd .arena , url , null ),
199+ .secure = url . isSecure ( ),
187200 });
188201 }
189202
@@ -247,7 +260,7 @@ pub fn httpRequestStart(arena: Allocator, bc: anytype, msg: *const Notification.
247260 .requestId = try std .fmt .allocPrint (arena , "REQ-{d}" , .{transfer .id }),
248261 .frameId = target_id ,
249262 .loaderId = bc .loader_id ,
250- .documentUrl = DocumentUrlWriter .init (& page .url . uri ),
263+ .documentUrl = DocumentUrlWriter .init (page .url ),
251264 .request = TransferAsRequestWriter .init (transfer ),
252265 .initiator = .{ .type = "other" },
253266 }, .{ .session_id = session_id });
@@ -300,23 +313,17 @@ pub const TransferAsRequestWriter = struct {
300313 try jws .objectField ("url" );
301314 try jws .beginWriteRaw ();
302315 try writer .writeByte ('\" ' );
303- try transfer .uri .writeToStream (writer , .{
304- .scheme = true ,
305- .authentication = true ,
306- .authority = true ,
307- .path = true ,
308- .query = true ,
309- });
316+ try transfer .url .writeToStream (writer );
310317 try writer .writeByte ('\" ' );
311318 jws .endWriteRaw ();
312319 }
313320
314321 {
315- if (transfer .uri . fragment ) | frag | {
322+ if (transfer .url . getFragment () ) | frag | {
316323 try jws .objectField ("urlFragment" );
317324 try jws .beginWriteRaw ();
318325 try writer .writeAll ("\" #" );
319- try writer .writeAll (frag . percent_encoded );
326+ try writer .writeAll (frag );
320327 try writer .writeByte ('\" ' );
321328 jws .endWriteRaw ();
322329 }
@@ -370,13 +377,7 @@ const TransferAsResponseWriter = struct {
370377 try jws .objectField ("url" );
371378 try jws .beginWriteRaw ();
372379 try writer .writeByte ('\" ' );
373- try transfer .uri .writeToStream (writer , .{
374- .scheme = true ,
375- .authentication = true ,
376- .authority = true ,
377- .path = true ,
378- .query = true ,
379- });
380+ try transfer .url .writeToStream (writer );
380381 try writer .writeByte ('\" ' );
381382 jws .endWriteRaw ();
382383 }
@@ -417,29 +418,22 @@ const TransferAsResponseWriter = struct {
417418};
418419
419420const DocumentUrlWriter = struct {
420- uri : * std.Uri ,
421+ url : URL ,
421422
422- fn init (uri : * std.Uri ) DocumentUrlWriter {
423- return .{
424- .uri = uri ,
425- };
423+ fn init (url : URL ) DocumentUrlWriter {
424+ return .{ .url = url };
426425 }
427426
428427 pub fn jsonStringify (self : * const DocumentUrlWriter , jws : anytype ) ! void {
429428 self ._jsonStringify (jws ) catch return error .WriteFailed ;
430429 }
430+
431431 fn _jsonStringify (self : * const DocumentUrlWriter , jws : anytype ) ! void {
432432 const writer = jws .writer ;
433433
434434 try jws .beginWriteRaw ();
435435 try writer .writeByte ('\" ' );
436- try self .uri .writeToStream (writer , .{
437- .scheme = true ,
438- .authentication = true ,
439- .authority = true ,
440- .path = true ,
441- .query = true ,
442- });
436+ try self .url .writeToStream (writer );
443437 try writer .writeByte ('\" ' );
444438 jws .endWriteRaw ();
445439 }
0 commit comments