Skip to content

Commit 2655012

Browse files
committed
Add --user_agent_suffix argument
Allows appending a value (separated by a space) to the existing Lightpanda/X.Y user agent.
1 parent b7d26cf commit 2655012

File tree

8 files changed

+58
-7
lines changed

8 files changed

+58
-7
lines changed

src/app.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub const App = struct {
3636
http_connect_timeout_ms: ?u31 = null,
3737
http_max_host_open: ?u8 = null,
3838
http_max_concurrent: ?u8 = null,
39+
user_agent: [:0]const u8,
3940
};
4041

4142
pub fn init(allocator: Allocator, config: Config) !*App {
@@ -53,6 +54,7 @@ pub const App = struct {
5354
.http_proxy = config.http_proxy,
5455
.tls_verify_host = config.tls_verify_host,
5556
.proxy_bearer_token = config.proxy_bearer_token,
57+
.user_agent = config.user_agent,
5658
});
5759
errdefer http.deinit();
5860

src/browser/ScriptManager.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub fn addFromElement(self: *ScriptManager, element: *parser.Element) !void {
205205

206206
errdefer pending_script.deinit();
207207

208-
var headers = try Http.Headers.init();
208+
var headers = try self.client.newHeaders();
209209
try page.requestCookie(.{}).headersForRequest(page.arena, remote_url.?, &headers);
210210

211211
try self.client.request(.{
@@ -273,7 +273,7 @@ pub fn blockingGet(self: *ScriptManager, url: [:0]const u8) !BlockingResult {
273273
.buffer_pool = &self.buffer_pool,
274274
};
275275

276-
var headers = try Http.Headers.init();
276+
var headers = try self.client.newHeaders();
277277
try self.page.requestCookie(.{}).headersForRequest(self.page.arena, url, &headers);
278278

279279
var client = self.client;

src/browser/page.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ pub const Page = struct {
548548
const owned_url = try self.arena.dupeZ(u8, request_url);
549549
self.url = try URL.parse(owned_url, null);
550550

551-
var headers = try Http.Headers.init();
551+
var headers = try self.http_client.newHeaders();
552552
if (opts.header) |hdr| try headers.add(hdr);
553553
try self.requestCookie(.{ .is_navigation = true }).headersForRequest(self.arena, owned_url, &headers);
554554

src/browser/xhr/xhr.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ pub const XMLHttpRequest = struct {
370370
}
371371
}
372372

373-
var headers = try Http.Headers.init();
373+
var headers = try page.http_client.newHeaders();
374374
for (self.headers.items) |hdr| {
375375
try headers.add(hdr);
376376
}

src/http/Client.zig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ notification: ?*Notification = null,
9696
// restoring, this originally-configured value is what it goes to.
9797
http_proxy: ?[:0]const u8 = null,
9898

99+
// The complete user-agent header line
100+
user_agent: [:0]const u8,
101+
99102
// libcurl can monitor arbitrary sockets. Currently, we ever [maybe] want to
100103
// monitor the CDP client socket, so we've done the simplest thing possible
101104
// by having this single optional field
@@ -130,6 +133,7 @@ pub fn init(allocator: Allocator, ca_blob: ?c.curl_blob, opts: Http.Opts) !*Clie
130133
.blocking = blocking,
131134
.allocator = allocator,
132135
.http_proxy = opts.http_proxy,
136+
.user_agent = opts.user_agent,
133137
.transfer_pool = transfer_pool,
134138
};
135139

@@ -147,6 +151,10 @@ pub fn deinit(self: *Client) void {
147151
self.allocator.destroy(self);
148152
}
149153

154+
pub fn newHeaders(self: *const Client) !Http.Headers {
155+
return Http.Headers.init(self.user_agent);
156+
}
157+
150158
pub fn abort(self: *Client) void {
151159
while (self.handles.in_use.first) |node| {
152160
const handle: *Handle = @fieldParentPtr("node", node);
@@ -796,7 +804,7 @@ pub const Transfer = struct {
796804
self.req.headers.deinit();
797805

798806
var buf: std.ArrayListUnmanaged(u8) = .empty;
799-
var new_headers = try Http.Headers.init();
807+
var new_headers = try self.client.newHeaders();
800808
for (headers) |hdr| {
801809
// safe to re-use this buffer, because Headers.add because curl copies
802810
// the value we pass into curl_slist_append.

src/http/Http.zig

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ pub fn newConnection(self: *Http) !Connection {
102102
return Connection.init(self.ca_blob, &self.opts);
103103
}
104104

105+
pub fn newHeaders(self: *const Http) Headers {
106+
return Headers.init(self.opts.user_agent);
107+
}
108+
105109
pub const Connection = struct {
106110
easy: *c.CURL,
107111
opts: Connection.Opts,
@@ -259,8 +263,8 @@ pub const Headers = struct {
259263
headers: *c.curl_slist,
260264
cookies: ?[*c]const u8,
261265

262-
pub fn init() !Headers {
263-
const header_list = c.curl_slist_append(null, "User-Agent: Lightpanda/1.0");
266+
pub fn init(user_agent: [:0]const u8) !Headers {
267+
const header_list = c.curl_slist_append(null, user_agent);
264268
if (header_list == null) return error.OutOfMemory;
265269
return .{ .headers = header_list, .cookies = null };
266270
}
@@ -337,6 +341,7 @@ pub const Opts = struct {
337341
tls_verify_host: bool = true,
338342
http_proxy: ?[:0]const u8 = null,
339343
proxy_bearer_token: ?[:0]const u8 = null,
344+
user_agent: [:0]const u8,
340345
};
341346

342347
pub const Method = enum {

src/main.zig

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ fn run(alloc: Allocator) !void {
108108
log.opts.filter_scopes = lfs;
109109
}
110110

111+
const user_agent = blk: {
112+
const USER_AGENT = "User-Agent: Lightpanda/1.0";
113+
if (args.userAgentSuffix()) |suffix| {
114+
break :blk try std.fmt.allocPrintSentinel(args_arena.allocator(), "{s} {s}", .{ USER_AGENT, suffix }, 0);
115+
}
116+
break :blk USER_AGENT;
117+
};
118+
111119
// _app is global to handle graceful shutdown.
112120
_app = try App.init(alloc, .{
113121
.run_mode = args.mode,
@@ -118,6 +126,7 @@ fn run(alloc: Allocator) !void {
118126
.http_connect_timeout_ms = args.httpConnectTiemout(),
119127
.http_max_host_open = args.httpMaxHostOpen(),
120128
.http_max_concurrent = args.httpMaxConcurrent(),
129+
.user_agent = user_agent,
121130
});
122131

123132
const app = _app.?;
@@ -260,6 +269,13 @@ const Command = struct {
260269
};
261270
}
262271

272+
fn userAgentSuffix(self: *const Command) ?[]const u8 {
273+
return switch (self.mode) {
274+
inline .serve, .fetch => |opts| opts.common.user_agent_suffix,
275+
else => unreachable,
276+
};
277+
}
278+
263279
const Mode = union(App.RunMode) {
264280
help: bool, // false when being printed because of an error
265281
fetch: Fetch,
@@ -293,6 +309,7 @@ const Command = struct {
293309
log_level: ?log.Level = null,
294310
log_format: ?log.Format = null,
295311
log_filter_scopes: ?[]log.Scope = null,
312+
user_agent_suffix: ?[]const u8 = null,
296313
};
297314

298315
fn printUsageAndExit(self: *const Command, success: bool) void {
@@ -339,6 +356,9 @@ const Command = struct {
339356
\\ Defaults to
340357
++ (if (builtin.mode == .Debug) " pretty." else " logfmt.") ++
341358
\\
359+
\\ --user_agent_suffix
360+
\\ Suffix to append to the Lightpanda/X.Y User-Agent
361+
\\
342362
;
343363

344364
// MAX_HELP_LEN|
@@ -713,6 +733,21 @@ fn parseCommonArg(
713733
return true;
714734
}
715735

736+
if (std.mem.eql(u8, "--user_agent_suffix", opt)) {
737+
const str = args.next() orelse {
738+
log.fatal(.app, "missing argument value", .{ .arg = "--user_agent_suffix" });
739+
return error.InvalidArgument;
740+
};
741+
for (str) |c| {
742+
if (!std.ascii.isPrint(c)) {
743+
log.fatal(.app, "not printable character", .{ .arg = "--user_agent_suffix" });
744+
return error.InvalidArgument;
745+
}
746+
}
747+
common.user_agent_suffix = try allocator.dupe(u8, str);
748+
return true;
749+
}
750+
716751
return false;
717752
}
718753

src/testing.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ pub fn setup() !void {
493493
test_app = try App.init(gpa.allocator(), .{
494494
.run_mode = .serve,
495495
.tls_verify_host = false,
496+
.user_agent = "User-Agent: Lightpanda/1.0 internal-tester",
496497
});
497498
errdefer test_app.deinit();
498499

0 commit comments

Comments
 (0)