extra header feedback

This commit is contained in:
sjorsdonkers
2025-06-06 10:33:15 +02:00
parent f1672dd6d2
commit bacef41a3b
2 changed files with 25 additions and 13 deletions

View File

@@ -71,6 +71,19 @@ fn setExtraHTTPHeaders(cmd: anytype) !void {
return cmd.sendResult(null, .{}); return cmd.sendResult(null, .{});
} }
// Upsert a header into the headers array.
// returns true if the header was added, false if it was updated
fn putAssumeCapacity(headers: *std.ArrayListUnmanaged(std.http.Header), extra: std.http.Header) bool {
for (headers.items) |*header| {
if (std.mem.eql(u8, header.name, extra.name)) {
header.value = extra.value;
return false;
}
}
headers.appendAssumeCapacity(extra);
return true;
}
pub fn httpRequestStart(arena: Allocator, bc: anytype, request: *const Notification.RequestStart) !void { pub fn httpRequestStart(arena: Allocator, bc: anytype, request: *const Notification.RequestStart) !void {
// Isn't possible to do a network request within a Browser (which our // Isn't possible to do a network request within a Browser (which our
// notification is tied to), without a page. // notification is tied to), without a page.
@@ -84,18 +97,10 @@ pub fn httpRequestStart(arena: Allocator, bc: anytype, request: *const Notificat
const page = bc.session.currentPage() orelse unreachable; const page = bc.session.currentPage() orelse unreachable;
// Modify request with extra CDP headers // Modify request with extra CDP headers
const original_len = request.headers.items.len; try request.headers.ensureTotalCapacity(request.arena, request.headers.items.len + cdp.extra_headers.items.len);
try request.headers.ensureTotalCapacity(arena, original_len + cdp.extra_headers.items.len); for (cdp.extra_headers.items) |extra| {
outer: for (cdp.extra_headers.items) |extra| { const new = putAssumeCapacity(request.headers, extra);
for (request.headers.items[0..original_len]) |*existing_header| { if (!new) log.debug(.cdp, "request header overwritten", .{ .name = extra.name });
if (std.mem.eql(u8, existing_header.name, extra.name)) {
// If the header already exists, we overwrite it
log.debug(.cdp, "request header overwritten", .{ .name = extra.name });
existing_header.value = extra.value;
continue :outer;
}
}
request.headers.appendAssumeCapacity(extra);
} }
const document_url = try urlToString(arena, &page.url.uri, .{ const document_url = try urlToString(arena, &page.url.uri, .{
@@ -204,5 +209,9 @@ test "cdp.network setExtraHTTPHeaders" {
.params = .{ .headers = .{ .food = "bars" } }, .params = .{ .headers = .{ .food = "bars" } },
}); });
try testing.expectEqual(ctx.cdp_.?.browser_context.?.cdp.extra_headers.items.len, 1); const bc = ctx.cdp().browser_context.?;
try testing.expectEqual(bc.cdp.extra_headers.items.len, 1);
try ctx.processMessage(.{ .id = 5, .method = "Target.attachToTarget", .params = .{ .targetId = bc.target_id.? } });
try testing.expectEqual(bc.cdp.extra_headers.items.len, 0);
} }

View File

@@ -389,6 +389,9 @@ fn doAttachtoTarget(cmd: anytype, target_id: []const u8) !void {
std.debug.assert(bc.session_id == null); std.debug.assert(bc.session_id == null);
const session_id = cmd.cdp.session_id_gen.next(); const session_id = cmd.cdp.session_id_gen.next();
// extra_headers should not be kept on a new page or tab, currently we have only 1 page, we clear it just in case
bc.cdp.extra_headers.clearRetainingCapacity();
try cmd.sendEvent("Target.attachedToTarget", AttachToTarget{ try cmd.sendEvent("Target.attachedToTarget", AttachToTarget{
.sessionId = session_id, .sessionId = session_id,
.targetInfo = TargetInfo{ .targetInfo = TargetInfo{