Emits a http_request_done internal notification.

With networking enabled, CDP listens to this event and emits a
`Network.loadingFinished` event. This is event is used by puppeteer to know that
details about the response (i.e. the body) can be queries.

Added dummy handling for the Network.getResponseBody message. Returns an
empty body. Needed because we emit the loadingFinished event which signals
to drivers that they can ask for the body.
This commit is contained in:
Karl Seguin
2025-08-20 19:32:19 +08:00
parent bade412d30
commit 6b001c50a4
4 changed files with 60 additions and 2 deletions

View File

@@ -455,12 +455,14 @@ pub fn BrowserContext(comptime CDP_T: type) type {
try self.cdp.browser.notification.register(.http_request_fail, self, onHttpRequestFail);
try self.cdp.browser.notification.register(.http_request_start, self, onHttpRequestStart);
try self.cdp.browser.notification.register(.http_headers_done, self, onHttpHeadersDone);
try self.cdp.browser.notification.register(.http_request_done, self, onHttpRequestDone);
}
pub fn networkDisable(self: *Self) void {
self.cdp.browser.notification.unregister(.http_request_fail, self);
self.cdp.browser.notification.unregister(.http_request_start, self);
self.cdp.browser.notification.unregister(.http_headers_done, self);
self.cdp.browser.notification.unregister(.http_request_done, self);
}
pub fn fetchEnable(self: *Self) !void {
@@ -516,6 +518,12 @@ pub fn BrowserContext(comptime CDP_T: type) type {
return @import("domains/network.zig").httpHeadersDone(self.notification_arena, self, data);
}
pub fn onHttpRequestDone(ctx: *anyopaque, data: *const Notification.RequestDone) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
defer self.resetNotificationArena();
return @import("domains/network.zig").httpRequestDone(self.notification_arena, self, data);
}
fn resetNotificationArena(self: *Self) void {
defer _ = self.cdp.notification_arena.reset(.{ .retain_with_limit = 1024 * 64 });
}

View File

@@ -36,6 +36,7 @@ pub fn processMessage(cmd: anytype) !void {
setCookie,
setCookies,
getCookies,
getResponseBody,
}, cmd.input.action) orelse return error.UnknownMethod;
switch (action) {
@@ -49,6 +50,7 @@ pub fn processMessage(cmd: anytype) !void {
.setCookie => return setCookie(cmd),
.setCookies => return setCookies(cmd),
.getCookies => return getCookies(cmd),
.getResponseBody => return getResponseBody(cmd),
}
}
@@ -202,6 +204,19 @@ fn getCookies(cmd: anytype) !void {
try cmd.sendResult(.{ .cookies = writer }, .{});
}
fn getResponseBody(cmd: anytype) !void {
const params = (try cmd.params(struct {
requestId: []const u8, // "REQ-{d}"
})) orelse return error.InvalidParams;
_ = params;
try cmd.sendResult(.{
.body = "TODO",
.base64Encoded = false,
}, .{});
}
pub fn httpRequestFail(arena: Allocator, bc: anytype, data: *const Notification.RequestFail) !void {
// It's possible that the request failed because we aborted when the client
// sent Target.closeTarget. In that case, bc.session_id will be cleared
@@ -264,6 +279,22 @@ pub fn httpHeadersDone(arena: Allocator, bc: anytype, data: *const Notification.
}, .{ .session_id = session_id });
}
pub fn httpRequestDone(arena: Allocator, bc: anytype, data: *const Notification.RequestDone) !void {
// Isn't possible to do a network request within a Browser (which our
// notification is tied to), without a page.
std.debug.assert(bc.session.page != null);
var cdp = bc.cdp;
// all unreachable because we _have_ to have a page.
const session_id = bc.session_id orelse unreachable;
try cdp.sendEvent("Network.loadingFinished", .{
.requestId = try std.fmt.allocPrint(arena, "REQ-{d}", .{data.transfer.id}),
.encodedDataLength = data.transfer.bytes_received,
}, .{ .session_id = session_id });
}
pub const TransferAsRequestWriter = struct {
transfer: *Transfer,