Optimize Network.responseReceived

Add a header iterator to the transfer. This removes the need for NetworkState,
duping header name/values, and the http_header_received event.
This commit is contained in:
Karl Seguin
2025-08-14 15:50:56 +08:00
parent 5100e06f38
commit 96b10f4b85
5 changed files with 81 additions and 80 deletions

View File

@@ -77,7 +77,6 @@ pub fn CDPT(comptime TypeProvider: type) type {
// Extra headers to add to all requests. TBD under which conditions this should be reset.
extra_headers: std.ArrayListUnmanaged([*c]const u8) = .empty,
network_state: NetworkState,
intercept_state: InterceptState,
const Self = @This();
@@ -94,7 +93,6 @@ pub fn CDPT(comptime TypeProvider: type) type {
.browser_context = null,
.message_arena = std.heap.ArenaAllocator.init(allocator),
.notification_arena = std.heap.ArenaAllocator.init(allocator),
.network_state = try NetworkState.init(allocator),
.intercept_state = try InterceptState.init(allocator), // TBD or browser session arena?
};
}
@@ -104,7 +102,6 @@ pub fn CDPT(comptime TypeProvider: type) type {
bc.deinit();
}
self.intercept_state.deinit(); // TBD Should this live in BC?
self.network_state.deinit();
self.browser.deinit();
self.message_arena.deinit();
self.notification_arena.deinit();
@@ -451,15 +448,13 @@ pub fn BrowserContext(comptime CDP_T: type) type {
pub fn networkEnable(self: *Self) !void {
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_header_received, self, onHttpHeaderReceived);
try self.cdp.browser.notification.register(.http_headers_done_receiving, self, onHttpHeadersDoneReceiving);
try self.cdp.browser.notification.register(.http_headers_done, self, onHttpHeadersDone);
}
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_header_received, self);
self.cdp.browser.notification.unregister(.http_headers_done_receiving, self);
self.cdp.browser.notification.unregister(.http_headers_done, self);
}
pub fn fetchEnable(self: *Self) !void {
@@ -473,7 +468,6 @@ pub fn BrowserContext(comptime CDP_T: type) type {
pub fn onPageRemove(ctx: *anyopaque, _: Notification.PageRemove) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
try @import("domains/page.zig").pageRemove(self);
try @import("domains/network.zig").pageRemove(self);
}
pub fn onPageCreated(ctx: *anyopaque, page: *Page) !void {
@@ -504,22 +498,16 @@ pub fn BrowserContext(comptime CDP_T: type) type {
try @import("domains/fetch.zig").requestPaused(self.notification_arena, self, data);
}
pub fn onHttpHeaderReceived(ctx: *anyopaque, data: *const Notification.ResponseHeader) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
defer self.resetNotificationArena();
try self.cdp.network_state.putOrAppendReceivedHeader(data.request_id, data.status, data.header);
}
pub fn onHttpRequestFail(ctx: *anyopaque, data: *const Notification.RequestFail) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
defer self.resetNotificationArena();
return @import("domains/network.zig").httpRequestFail(self.notification_arena, self, data);
}
pub fn onHttpHeadersDoneReceiving(ctx: *anyopaque, data: *const Notification.ResponseHeadersDone) !void {
pub fn onHttpHeadersDone(ctx: *anyopaque, data: *const Notification.ResponseHeadersDone) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
defer self.resetNotificationArena();
return @import("domains/network.zig").httpHeadersDoneReceiving(self.notification_arena, self, data);
return @import("domains/network.zig").httpHeadersDone(self.notification_arena, self, data);
}
fn resetNotificationArena(self: *Self) void {