Lightpanda: the headless browser designed for AI and automation
cdp: fix CDP's failRequest error handler
Karl Seguin89ba7226f63c
diff --git a/src/browser/tests/net/xhr.html b/src/browser/tests/net/xhr.htmlindex 143c5b925..b67881e14 100644--- a/src/browser/tests/net/xhr.html+++ b/src/browser/tests/net/xhr.html@@ -255,7 +255,7 @@await state.done(() => {testing.expectEqual(true, abortFired);- testing.expectEqual(true, errorFired);+ testing.expectEqual(false, errorFired);testing.expectEqual(true, loadEndFired);testing.expectEqual(XMLHttpRequest.UNSENT, req.readyState);});@@ -285,7 +285,7 @@await state.done(() => {testing.expectEqual(true, abortFired);- testing.expectEqual(true, errorFired);+ testing.expectEqual(false, errorFired);testing.expectEqual(true, loadEndFired);testing.expectEqual(XMLHttpRequest.UNSENT, req.readyState);});@@ -315,7 +315,7 @@await state.done(() => {testing.expectEqual(true, abortFired);- testing.expectEqual(true, errorFired);+ testing.expectEqual(false, errorFired);testing.expectEqual(true, loadEndFired);testing.expectEqual(XMLHttpRequest.UNSENT, req.readyState);});diff --git a/src/browser/webapi/net/XMLHttpRequest.zig b/src/browser/webapi/net/XMLHttpRequest.zigindex 849d6c111..1426c69b1 100644--- a/src/browser/webapi/net/XMLHttpRequest.zig+++ b/src/browser/webapi/net/XMLHttpRequest.zig@@ -819,8 +819,7 @@ fn _handleError(self: *XMLHttpRequest, err: anyerror) !void {try self._proto.dispatch(.abort, null, exec);} else if (is_timeout) {try self._proto.dispatch(.timeout, null, exec);- }- if (!is_timeout) {+ } else {try self._proto.dispatch(.err, null, exec);}try self._proto.dispatch(.load_end, null, exec);diff --git a/src/server/cdp/domains/fetch.zig b/src/server/cdp/domains/fetch.zigindex 177291742..91964255a 100644--- a/src/server/cdp/domains/fetch.zig+++ b/src/server/cdp/domains/fetch.zig@@ -89,22 +89,6 @@ const EnableParam = struct {patterns: ?[]RequestPattern = null,handleAuthRequests: bool = false,};-const ErrorReason = enum {- Failed,- Aborted,- TimedOut,- AccessDenied,- ConnectionClosed,- ConnectionReset,- ConnectionRefused,- ConnectionAborted,- ConnectionFailed,- NameNotResolved,- InternetDisconnected,- AddressUnreachable,- BlockedByClient,- BlockedByResponse,-};fn commandSessionId(cmd: *CDP.Command, bc: *CDP.BrowserContext) ![]const u8 {if (cmd.input.session_id) |session_id| {@@ -334,7 +318,7 @@ fn failRequest(cmd: *CDP.Command) !void {const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;const params = (try cmd.params(struct {requestId: []const u8, // "INT-{d}"- errorReason: ErrorReason,+ errorReason: network.ErrorReason,})) orelse return error.InvalidParams;const client = &bc.cdp.browser.http_client;@@ -347,9 +331,7 @@ fn failRequest(cmd: *CDP.Command) !void {return cmd.sendResult(null, .{});};- // The client is cancelling the request: consumers see it as a cancel- // (XHR fires abort, not error), like any other Transfer.cancel().- defer transfer.abortParked(error.TransferCanceled);+ defer transfer.abortParked(params.errorReason.toError());log.info(.cdp, "request intercept", .{.state = "fail",diff --git a/src/server/cdp/domains/network.zig b/src/server/cdp/domains/network.zigindex 39f6e0604..6912ae503 100644--- a/src/server/cdp/domains/network.zig+++ b/src/server/cdp/domains/network.zig@@ -48,6 +48,76 @@ pub const BufferLimits = struct {post_data: usize = max_post_data_size,};+// Why Fetch.failRequest fails an intercepted request.+pub const ErrorReason = enum {+ Failed,+ Aborted,+ TimedOut,+ AccessDenied,+ ConnectionClosed,+ ConnectionReset,+ ConnectionRefused,+ ConnectionAborted,+ ConnectionFailed,+ NameNotResolved,+ InternetDisconnected,+ AddressUnreachable,+ BlockedByClient,+ BlockedByResponse,++ const Failure = error{+ Failed,+ AccessDenied,+ ConnectionClosed,+ ConnectionReset,+ ConnectionRefused,+ ConnectionAborted,+ ConnectionFailed,+ NameNotResolved,+ InternetDisconnected,+ AddressUnreachable,+ BlockedByClient,+ BlockedByResponse,+ };++ pub fn toError(self: ErrorReason) anyerror {+ return switch (self) {+ .Aborted => error.TransferCanceled, // Like Chrome, Only abort cancels+ .TimedOut => error.OperationTimedout,+ inline else => |reason| @field(Failure, @tagName(reason)),+ };+ }++ fn netError(self: ErrorReason) []const u8 {+ return switch (self) {+ .Failed => "net::ERR_FAILED",+ .Aborted => "net::ERR_ABORTED",+ .TimedOut => "net::ERR_TIMED_OUT",+ .AccessDenied => "net::ERR_ACCESS_DENIED",+ .ConnectionClosed => "net::ERR_CONNECTION_CLOSED",+ .ConnectionReset => "net::ERR_CONNECTION_RESET",+ .ConnectionRefused => "net::ERR_CONNECTION_REFUSED",+ .ConnectionAborted => "net::ERR_CONNECTION_ABORTED",+ .ConnectionFailed => "net::ERR_CONNECTION_FAILED",+ .NameNotResolved => "net::ERR_NAME_NOT_RESOLVED",+ .InternetDisconnected => "net::ERR_INTERNET_DISCONNECTED",+ .AddressUnreachable => "net::ERR_ADDRESS_UNREACHABLE",+ .BlockedByClient => "net::ERR_BLOCKED_BY_CLIENT",+ .BlockedByResponse => "net::ERR_BLOCKED_BY_RESPONSE",+ };+ }+};++// Chrome's net error for an ErrorReason's error, else the error's name.+pub fn errorText(err: anyerror) []const u8 {+ inline for (comptime std.enums.values(ErrorReason)) |reason| {+ if (err == reason.toError()) {+ return reason.netError();+ }+ }+ return @errorName(err);+}+pub fn processMessage(cmd: *CDP.Command) !void {const action = std.meta.stringToEnum(enum {enable,@@ -413,7 +483,6 @@ pub fn httpRequestFail(bc: *CDP.BrowserContext, msg: *const Notification.Request// Consumer-side cancel (stopLoading, xhr.abort, AbortController, ...):const canceled = msg.err == error.TransferCanceled;- const error_text: []const u8 = if (canceled) "net::ERR_ABORTED" else @errorName(msg.err);// We're missing a bunch of fields, but, for now, this seems like enoughtry bc.cdp.sendEvent("Network.loadingFailed", .{@@ -421,7 +490,7 @@ pub fn httpRequestFail(bc: *CDP.BrowserContext, msg: *const Notification.Request.timestamp = lp.datetime.timestamp(.boot),// Seems to be what chrome answers with. I assume it depends on the type of error?.type = "Ping",- .errorText = error_text,+ .errorText = errorText(msg.err),.canceled = canceled,.blockedReason = msg.blocked_reason,}, .{ .session_id = session_id });@@ -752,6 +821,14 @@ fn keyFromRequestId(request_id: []const u8) !CDP.BrowserContext.CapturedKey {}const testing = @import("../testing.zig");+test "cdp.network: errorText" {+ try testing.expectEqualSlices(u8, "net::ERR_ABORTED", errorText(error.TransferCanceled));+ try testing.expectEqualSlices(u8, "net::ERR_TIMED_OUT", errorText(error.OperationTimedout));+ try testing.expectEqualSlices(u8, "net::ERR_FAILED", errorText(ErrorReason.Failed.toError()));+ try testing.expectEqualSlices(u8, "net::ERR_NAME_NOT_RESOLVED", errorText(ErrorReason.NameNotResolved.toError()));+ try testing.expectEqualSlices(u8, "CouldntConnect", errorText(error.CouldntConnect));+}+test "cdp.network emulateNetworkConditions and setBypassServiceWorker" {testing.silenceLog(&.{.not_implemented});var ctx = try testing.context();diff --git a/src/server/cdp/domains/page.zig b/src/server/cdp/domains/page.zigindex 93d27972f..4f49cea65 100644--- a/src/server/cdp/domains/page.zig+++ b/src/server/cdp/domains/page.zig@@ -23,6 +23,7 @@ const lp = @import("lightpanda");const id = @import("../id.zig");const CDP = @import("../CDP.zig");const target = @import("target.zig");+const network = @import("network.zig");const js = @import("../../../browser/js/js.zig");const URL = @import("../../../browser/URL.zig");@@ -638,10 +639,7 @@ pub fn frameNavigateFailed(bc: *CDP.BrowserContext, event: *const Notification.F.result = .{.frameId = &id.toFrameId(event.frame_id),.loaderId = &id.toLoaderId(event.loader_id),- .errorText = switch (event.err) {- error.TransferCanceled => "net::ERR_ABORTED",- else => |err| @errorName(err),- },+ .errorText = network.errorText(event.err),},.sessionId = session_id,}, .{});@@ -2215,6 +2213,53 @@ test "cdp.page: stopLoading cancels an uncommitted root navigation" {try testing.expect(v.toBool());}+test "cdp.page: Fetch.failRequest on a script navigation" {+ testing.silenceLog(&.{.frame});++ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{ .id = "BID-FR", .session_id = "SID-FR", .target_id = "TID-FR-0000000".*, .url = "hi.html" });+ const live = bc.mainPage() orelse unreachable;++ try ctx.processMessage(.{ .id = 1, .method = "Network.enable" });+ try ctx.expectSentResult(null, .{ .id = 1 });+ try ctx.processMessage(.{ .id = 2, .method = "Fetch.enable" });+ try ctx.expectSentResult(null, .{ .id = 2 });++ {+ // Aborted is a cancel: the live page stays.+ const frame = bc.mainFrame() orelse unreachable;+ var ls: js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();+ _ = try ls.local.exec("location.href = 'hi.html?aborted'", null);+ }+ try ctx.expectSentEvent("Fetch.requestPaused", .{ .requestId = "INT-0000000001" }, .{});+ try ctx.processMessage(.{ .id = 3, .method = "Fetch.failRequest", .params = .{ .requestId = "INT-0000000001", .errorReason = "Aborted" } });+ try ctx.expectSentResult(null, .{ .id = 3 });+ try ctx.expectSentEvent("Network.loadingFailed", .{ .errorText = "net::ERR_ABORTED", .canceled = true }, .{});+ try testing.waitForPage(bc);+ try testing.expectEqual(null, bc.session.replacementOf(live));+ try testing.expect(live == bc.mainPage().?);++ {+ const frame = bc.mainFrame() orelse unreachable;+ var ls: js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();+ _ = try ls.local.exec("location.href = 'hi.html?failed'", null);+ }+ try ctx.expectSentEvent("Fetch.requestPaused", .{ .requestId = "INT-0000000002" }, .{});+ try ctx.processMessage(.{ .id = 4, .method = "Fetch.failRequest", .params = .{ .requestId = "INT-0000000002", .errorReason = "Failed" } });+ try ctx.expectSentResult(null, .{ .id = 4 });++ // Any other reason is a network error: like Chrome, an error document+ // commits, which a client waiting on the navigation sees.+ try ctx.expectSentEvent("Network.loadingFailed", .{ .errorText = "net::ERR_FAILED", .canceled = false }, .{});+ try ctx.expectSentEvent("Page.frameNavigated", .{ .frame = .{ .url = "http://127.0.0.1:9582/src/browser/tests/hi.html?failed" } }, .{});+}+test "cdp.frame: reload replays POST navigation" {var ctx = try testing.context();defer ctx.deinit();