Skip to content

Commit f1672dd

Browse files
committed
setExtraHTTPHeaders
1 parent fff0a8a commit f1672dd

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

src/cdp/cdp.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ pub fn CDPT(comptime TypeProvider: type) type {
7171
// Used for processing notifications within a browser context.
7272
notification_arena: std.heap.ArenaAllocator,
7373

74+
// Extra headers to add to all requests. TBD under which conditions this should be reset.
75+
extra_headers: std.ArrayListUnmanaged(std.http.Header) = .empty,
76+
7477
const Self = @This();
7578

7679
pub fn init(app: *App, client: TypeProvider.Client) !Self {

src/cdp/domains/network.zig

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
const std = @import("std");
2020
const Notification = @import("../../notification.zig").Notification;
21+
const log = @import("../../log.zig");
2122

2223
const Allocator = std.mem.Allocator;
2324

@@ -26,12 +27,14 @@ pub fn processMessage(cmd: anytype) !void {
2627
enable,
2728
disable,
2829
setCacheDisabled,
30+
setExtraHTTPHeaders,
2931
}, cmd.input.action) orelse return error.UnknownMethod;
3032

3133
switch (action) {
3234
.enable => return enable(cmd),
3335
.disable => return disable(cmd),
3436
.setCacheDisabled => return cmd.sendResult(null, .{}),
37+
.setExtraHTTPHeaders => return setExtraHTTPHeaders(cmd),
3538
}
3639
}
3740

@@ -47,6 +50,27 @@ fn disable(cmd: anytype) !void {
4750
return cmd.sendResult(null, .{});
4851
}
4952

53+
fn setExtraHTTPHeaders(cmd: anytype) !void {
54+
const params = (try cmd.params(struct {
55+
headers: std.json.ArrayHashMap([]const u8),
56+
})) orelse return error.InvalidParams;
57+
58+
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
59+
60+
// Copy the headers onto the browser context arena
61+
const arena = bc.arena;
62+
const extra_headers = &bc.cdp.extra_headers;
63+
64+
extra_headers.clearRetainingCapacity();
65+
try extra_headers.ensureTotalCapacity(arena, params.headers.map.count());
66+
var it = params.headers.map.iterator();
67+
while (it.next()) |header| {
68+
extra_headers.appendAssumeCapacity(.{ .name = try arena.dupe(u8, header.key_ptr.*), .value = try arena.dupe(u8, header.value_ptr.*) });
69+
}
70+
71+
return cmd.sendResult(null, .{});
72+
}
73+
5074
pub fn httpRequestStart(arena: Allocator, bc: anytype, request: *const Notification.RequestStart) !void {
5175
// Isn't possible to do a network request within a Browser (which our
5276
// notification is tied to), without a page.
@@ -59,6 +83,21 @@ pub fn httpRequestStart(arena: Allocator, bc: anytype, request: *const Notificat
5983
const target_id = bc.target_id orelse unreachable;
6084
const page = bc.session.currentPage() orelse unreachable;
6185

86+
// Modify request with extra CDP headers
87+
const original_len = request.headers.items.len;
88+
try request.headers.ensureTotalCapacity(arena, original_len + cdp.extra_headers.items.len);
89+
outer: for (cdp.extra_headers.items) |extra| {
90+
for (request.headers.items[0..original_len]) |*existing_header| {
91+
if (std.mem.eql(u8, existing_header.name, extra.name)) {
92+
// If the header already exists, we overwrite it
93+
log.debug(.cdp, "request header overwritten", .{ .name = extra.name });
94+
existing_header.value = extra.value;
95+
continue :outer;
96+
}
97+
}
98+
request.headers.appendAssumeCapacity(extra);
99+
}
100+
62101
const document_url = try urlToString(arena, &page.url.uri, .{
63102
.scheme = true,
64103
.authentication = true,
@@ -80,8 +119,8 @@ pub fn httpRequestStart(arena: Allocator, bc: anytype, request: *const Notificat
80119
});
81120

82121
var headers: std.StringArrayHashMapUnmanaged([]const u8) = .empty;
83-
try headers.ensureTotalCapacity(arena, request.headers.len);
84-
for (request.headers) |header| {
122+
try headers.ensureTotalCapacity(arena, request.headers.items.len);
123+
for (request.headers.items) |header| {
85124
headers.putAssumeCapacity(header.name, header.value);
86125
}
87126

@@ -129,13 +168,13 @@ pub fn httpRequestComplete(arena: Allocator, bc: anytype, request: *const Notifi
129168
// We're missing a bunch of fields, but, for now, this seems like enough
130169
try cdp.sendEvent("Network.responseReceived", .{
131170
.requestId = try std.fmt.allocPrint(arena, "REQ-{d}", .{request.id}),
132-
.frameId = target_id,
133171
.loaderId = bc.loader_id,
134172
.response = .{
135173
.url = url,
136174
.status = request.status,
137175
.headers = std.json.ArrayHashMap([]const u8){ .map = headers },
138176
},
177+
.frameId = target_id,
139178
}, .{ .session_id = session_id });
140179
}
141180

@@ -144,3 +183,26 @@ fn urlToString(arena: Allocator, url: *const std.Uri, opts: std.Uri.WriteToStrea
144183
try url.writeToStream(opts, buf.writer(arena));
145184
return buf.items;
146185
}
186+
187+
const testing = @import("../testing.zig");
188+
test "cdp.network setExtraHTTPHeaders" {
189+
var ctx = testing.context();
190+
defer ctx.deinit();
191+
192+
// _ = try ctx.loadBrowserContext(.{ .id = "NID-A", .session_id = "NESI-A" });
193+
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about/blank" } });
194+
195+
try ctx.processMessage(.{
196+
.id = 3,
197+
.method = "Network.setExtraHTTPHeaders",
198+
.params = .{ .headers = .{ .foo = "bar" } },
199+
});
200+
201+
try ctx.processMessage(.{
202+
.id = 4,
203+
.method = "Network.setExtraHTTPHeaders",
204+
.params = .{ .headers = .{ .food = "bars" } },
205+
});
206+
207+
try testing.expectEqual(ctx.cdp_.?.browser_context.?.cdp.extra_headers.items.len, 1);
208+
}

src/http/client.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,10 +736,11 @@ pub const Request = struct {
736736
}
737737
self._notified_start = true;
738738
notification.dispatch(.http_request_start, &.{
739+
.arena = self.arena,
739740
.id = self.id,
740741
.url = self.request_uri,
741742
.method = self.method,
742-
.headers = self.headers.items,
743+
.headers = &self.headers,
743744
.has_body = self.body != null,
744745
});
745746
}

src/notification.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ pub const Notification = struct {
8989
};
9090

9191
pub const RequestStart = struct {
92+
arena: Allocator,
9293
id: usize,
9394
url: *const std.Uri,
9495
method: http_client.Request.Method,
95-
headers: []std.http.Header,
96+
headers: *std.ArrayListUnmanaged(std.http.Header),
9697
has_body: bool,
9798
};
9899

0 commit comments

Comments
 (0)