Lightpanda: the headless browser designed for AI and automation
cdp: adds support for interceptResponse on continueRequest
Karl Seguin520147895992
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zigindex eb1b5f21e..d5a466aec 100644--- a/src/network/HttpClient.zig+++ b/src/network/HttpClient.zig@@ -2632,6 +2632,9 @@ pub const Transfer = struct {// need to restore (and hence capture) the original headers._intercept_original_headers: ?[]const RequestHeader = null,+ // Fetch.continueRequest's interceptResponse override.+ _intercept_response: ?bool = null,+// Linked into client.pending_queue while .queued and client.delayed_queue// while .delayed; reused to link the retired transfer into// client.graveyard (deinit unlinks it from those queues first, so the@@ -3873,6 +3876,7 @@ pub const Transfer = struct {const arena = transfer.arena;transfer._redirect_count += 1;+ transfer._intercept_response = null;if (transfer._redirect_count > transfer.client.network.config.httpMaxRedirects()) {return error.TooManyRedirects;}diff --git a/src/server/cdp/domains/fetch.zig b/src/server/cdp/domains/fetch.zigindex a1020e914..14401cf5a 100644--- a/src/server/cdp/domains/fetch.zig+++ b/src/server/cdp/domains/fetch.zig@@ -157,7 +157,9 @@ pub fn responseIntercept(arena: Allocator, bc: *CDP.BrowserContext, intercept: *const session_id = bc.fetch_session_id orelse return;const transfer = intercept.transfer;- if (!bc.intercept_state.matches(.Response, transfer.req.url, transfer.req.resource_type)) {+ // continueRequest's interceptResponse overrides the patterns for this hop.+ const pause = transfer._intercept_response orelse bc.intercept_state.matches(.Response, transfer.req.url, transfer.req.resource_type);+ if (pause == false) {// Leaves wait_for_interception false: the response is delivered untouched.return;}@@ -275,13 +277,9 @@ fn continueRequest(cmd: *CDP.Command) !void {method: ?[]const u8 = null,postData: ?[]const u8 = null,headers: ?[]const http.Header = null,- interceptResponse: bool = false,+ interceptResponse: ?bool = null,})) orelse return error.InvalidParams;- if (params.interceptResponse) {- return error.NotImplemented;- }-if (try pausedResponse(bc, params.requestId)) |transfer| {// The request has already been sent: like Chrome, its overrides are// ignored, except that a url on a redirect replaces its Location.@@ -335,6 +333,10 @@ fn continueRequest(cmd: *CDP.Command) !void {request.body = body;}+ if (params.interceptResponse) |intercept_response| {+ transfer._intercept_response = intercept_response;+ }+try client.continueIntercepted(transfer);return cmd.sendResult(null, .{});}@@ -1189,6 +1191,104 @@ test "cdp.Fetch: response stage continueRequest ignores request overrides, but atry testing.expectEqual(0, bc.cdp.browser.http_client.intercepted);}+test "cdp.Fetch: continueRequest interceptResponse pauses the response without a Response pattern" {+ var ctx = try testing.context();+ defer ctx.deinit();+ const bc = try ctx.loadBrowserContext(.{ .id = "BID-RS", .session_id = "SID-RS" });+ try ctx.processMessage(.{ .id = 1, .method = "Fetch.enable", .params = .{ .patterns = .{.{ .urlPattern = "*" }} } });+ try ctx.expectSentResult(null, .{ .id = 1 });++ var probe: ResponseStageProbe = .{};+ defer probe.deinit();+ try probe.start(bc, "http://127.0.0.1:9582/xhr");+ try ResponseStageProbe.tickUntilPaused(bc, 1);++ try ctx.processMessage(.{ .id = 2, .method = "Fetch.continueRequest", .params = .{ .requestId = "INT-0000000001", .interceptResponse = true } });+ try ctx.expectSentResult(null, .{ .id = 2 });+ try ResponseStageProbe.tickUntilPaused(bc, 1);+ try ctx.expectSentEvent("Fetch.requestPaused", .{+ .requestId = "INT-0000000002",+ .request = .{ .url = "http://127.0.0.1:9582/xhr" },+ .responseStatusCode = 200,+ }, .{ .session_id = "SID-RS" });+ try testing.expectEqual(false, probe.done);++ try ctx.processMessage(.{ .id = 3, .method = "Fetch.getResponseBody", .params = .{ .requestId = "INT-0000000002" } });+ try ctx.expectSentResult(.{ .body = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MA==", .base64Encoded = true }, .{ .id = 3 });++ try ctx.processMessage(.{ .id = 4, .method = "Fetch.continueResponse", .params = .{ .requestId = "INT-0000000002" } });+ try ctx.expectSentResult(null, .{ .id = 4 });+ try probe.tickUntilSettled(bc);+ try testing.expectEqual(null, probe.err);+ try testing.expectEqualSlices(u8, repeat("1234567890", 10), probe.body.items);+ try testing.expectEqual(0, bc.cdp.browser.http_client.intercepted);+}++test "cdp.Fetch: continueRequest interceptResponse covers one hop, its redirect included" {+ var ctx = try testing.context();+ defer ctx.deinit();+ const bc = try ctx.loadBrowserContext(.{ .id = "BID-RS", .session_id = "SID-RS" });+ try ctx.processMessage(.{ .id = 1, .method = "Fetch.enable", .params = .{ .patterns = .{.{ .urlPattern = "*" }} } });+ try ctx.expectSentResult(null, .{ .id = 1 });++ var probe: ResponseStageProbe = .{};+ defer probe.deinit();+ try probe.start(bc, "http://127.0.0.1:9582/xhr/redirect");+ try ResponseStageProbe.tickUntilPaused(bc, 1);++ try ctx.processMessage(.{ .id = 2, .method = "Fetch.continueRequest", .params = .{ .requestId = "INT-0000000001", .interceptResponse = true } });+ try ctx.expectSentResult(null, .{ .id = 2 });+ try ResponseStageProbe.tickUntilPaused(bc, 1);+ try ctx.expectSentEvent("Fetch.requestPaused", .{+ .requestId = "INT-0000000002",+ .request = .{ .url = "http://127.0.0.1:9582/xhr/redirect" },+ .responseStatusCode = 302,+ }, .{ .session_id = "SID-RS" });++ // Following the redirect starts a new hop: its request pauses, and+ // without a new interceptResponse its response does not.+ try ctx.processMessage(.{ .id = 3, .method = "Fetch.continueResponse", .params = .{ .requestId = "INT-0000000002" } });+ try ctx.expectSentResult(null, .{ .id = 3 });+ try ResponseStageProbe.tickUntilPaused(bc, 1);+ try ctx.expectSentEvent("Fetch.requestPaused", .{+ .requestId = "INT-0000000003",+ .request = .{ .url = "http://127.0.0.1:9582/xhr" },+ }, .{ .session_id = "SID-RS" });+ try testing.expectEqual(null, (try sentPausedParams(&ctx, "INT-0000000003")).get("responseStatusCode"));++ try ctx.processMessage(.{ .id = 4, .method = "Fetch.continueRequest", .params = .{ .requestId = "INT-0000000003" } });+ try ctx.expectSentResult(null, .{ .id = 4 });+ try probe.tickUntilSettled(bc);+ try testing.expectEqual(null, probe.err);+ try testing.expectEqualSlices(u8, repeat("1234567890", 10), probe.body.items);+ try testing.expectEqual(error.PausedEventNotSent, sentPausedParams(&ctx, "INT-0000000004"));+ try testing.expectEqual(0, bc.cdp.browser.http_client.intercepted);+}++test "cdp.Fetch: continueRequest interceptResponse false skips a matching Response pattern" {+ var ctx = try testing.context();+ defer ctx.deinit();+ const bc = try ctx.loadBrowserContext(.{ .id = "BID-RS", .session_id = "SID-RS" });+ try ctx.processMessage(.{ .id = 1, .method = "Fetch.enable", .params = .{ .patterns = .{+ .{ .urlPattern = "*" },+ .{ .urlPattern = "*", .requestStage = "Response" },+ } } });+ try ctx.expectSentResult(null, .{ .id = 1 });++ var probe: ResponseStageProbe = .{};+ defer probe.deinit();+ try probe.start(bc, "http://127.0.0.1:9582/xhr");+ try ResponseStageProbe.tickUntilPaused(bc, 1);++ try ctx.processMessage(.{ .id = 2, .method = "Fetch.continueRequest", .params = .{ .requestId = "INT-0000000001", .interceptResponse = false } });+ try ctx.expectSentResult(null, .{ .id = 2 });+ try probe.tickUntilSettled(bc);+ try testing.expectEqual(null, probe.err);+ try testing.expectEqualSlices(u8, repeat("1234567890", 10), probe.body.items);+ try testing.expectEqual(error.PausedEventNotSent, sentPausedParams(&ctx, "INT-0000000002"));+ try testing.expectEqual(0, bc.cdp.browser.http_client.intercepted);+}+test "cdp.Fetch: response stage pauses a network failure with responseErrorReason" {var ctx = try testing.context();defer ctx.deinit();