@@ -70,6 +70,10 @@ pub const InterceptState = struct {
7070 pub fn deinit (self : * InterceptState ) void {
7171 self .waiting .deinit (self .allocator );
7272 }
73+
74+ pub fn pendingTransfers (self : * const InterceptState ) []* Transfer {
75+ return self .waiting .values ();
76+ }
7377};
7478
7579const RequestPattern = struct {
@@ -134,11 +138,13 @@ fn disable(cmd: anytype) !void {
134138
135139fn enable (cmd : anytype ) ! void {
136140 const params = (try cmd .params (EnableParam )) orelse EnableParam {};
137- if (params .patterns .len != 0 ) {
138- log .warn (.cdp , "not implemented" , .{ .feature = "Fetch.enable No patterns yet" });
141+ if (! arePatternsSupported (params .patterns )) {
142+ log .warn (.cdp , "not implemented" , .{ .feature = "Fetch.enable advanced patterns are not" });
143+ return cmd .sendResult (null , .{});
139144 }
145+
140146 if (params .handleAuthRequests ) {
141- log .warn (.cdp , "not implemented" , .{ .feature = "Fetch.enable No auth yet" });
147+ log .warn (.cdp , "not implemented" , .{ .feature = "Fetch.enable handleAuthRequests is not supported yet" });
142148 }
143149
144150 const bc = cmd .browser_context orelse return error .BrowserContextNotLoaded ;
@@ -147,9 +153,33 @@ fn enable(cmd: anytype) !void {
147153 return cmd .sendResult (null , .{});
148154}
149155
150- pub fn requestIntercept (arena : Allocator , bc : anytype , intercept : * const Notification.RequestIntercept ) ! void {
151- var cdp = bc .cdp ;
156+ fn arePatternsSupported (patterns : []RequestPattern ) bool {
157+ if (patterns .len == 0 ) {
158+ return true ;
159+ }
160+ if (patterns .len > 1 ) {
161+ return false ;
162+ }
152163
164+ // While we don't support patterns, yet, both Playwright and Puppeteer send
165+ // a default pattern which happens to be what we support:
166+ // [{"urlPattern":"*","requestStage":"Request"}]
167+ // So, rather than erroring on this case because we don't support patterns,
168+ // we'll allow it, because this pattern is how it works as-is.
169+ const pattern = patterns [0 ];
170+ if (! std .mem .eql (u8 , pattern .urlPattern , "*" )) {
171+ return false ;
172+ }
173+ if (pattern .resourceType != null ) {
174+ return false ;
175+ }
176+ if (pattern .requestStage != .Request ) {
177+ return false ;
178+ }
179+ return true ;
180+ }
181+
182+ pub fn requestIntercept (arena : Allocator , bc : anytype , intercept : * const Notification.RequestIntercept ) ! void {
153183 // unreachable because we _have_ to have a page.
154184 const session_id = bc .session_id orelse unreachable ;
155185 const target_id = bc .target_id orelse unreachable ;
@@ -160,9 +190,9 @@ pub fn requestIntercept(arena: Allocator, bc: anytype, intercept: *const Notific
160190 // TODO: What to do when receiving replies for a previous page's requests?
161191
162192 const transfer = intercept .transfer ;
163- try cdp .intercept_state .put (transfer );
193+ try bc .intercept_state .put (transfer );
164194
165- try cdp .sendEvent ("Fetch.requestPaused" , .{
195+ try bc . cdp .sendEvent ("Fetch.requestPaused" , .{
166196 .requestId = try std .fmt .allocPrint (arena , "INTERCEPT-{d}" , .{transfer .id }),
167197 .request = network .TransferAsRequestWriter .init (transfer ),
168198 .frameId = target_id ,
@@ -202,7 +232,7 @@ fn continueRequest(cmd: anytype) !void {
202232
203233 const page = bc .session .currentPage () orelse return error .PageNotLoaded ;
204234
205- var intercept_state = & bc .cdp . intercept_state ;
235+ var intercept_state = & bc .intercept_state ;
206236 const request_id = try idFromRequestId (params .requestId );
207237 const transfer = intercept_state .remove (request_id ) orelse return error .RequestNotFound ;
208238
@@ -238,7 +268,7 @@ fn failRequest(cmd: anytype) !void {
238268
239269 const page = bc .session .currentPage () orelse return error .PageNotLoaded ;
240270
241- var intercept_state = & bc .cdp . intercept_state ;
271+ var intercept_state = & bc .intercept_state ;
242272 const request_id = try idFromRequestId (params .requestId );
243273
244274 const transfer = intercept_state .remove (request_id ) orelse return error .RequestNotFound ;
0 commit comments