Eliminates duplication in the creation of HTTP headers

This commit is contained in:
Nikolay Govorov
2026-02-04 09:08:57 +00:00
parent f71aa1cad2
commit a72782f91e
8 changed files with 107 additions and 92 deletions

View File

@@ -97,8 +97,7 @@ http_proxy: ?[:0]const u8 = null,
// CDP.
use_proxy: bool,
// The complete user-agent header line
user_agent: [:0]const u8,
config: *const Config,
cdp_client: ?CDPClient = null,
@@ -134,13 +133,7 @@ pub fn init(allocator: Allocator, ca_blob: ?c.curl_blob, config: *const Config)
try errorMCheck(c.curl_multi_setopt(multi, c.CURLMOPT_MAX_HOST_CONNECTIONS, @as(c_long, config.httpMaxHostOpen())));
const user_agent = try config.userAgent(allocator);
var proxy_bearer_header: ?[:0]const u8 = null;
if (config.proxyBearerToken()) |bt| {
proxy_bearer_header = try std.fmt.allocPrintSentinel(allocator, "Proxy-Authorization: Bearer {s}", .{bt}, 0);
}
var handles = try Handles.init(allocator, client, ca_blob, config, user_agent, proxy_bearer_header);
var handles = try Handles.init(allocator, client, ca_blob, config);
errdefer handles.deinit(allocator);
const http_proxy = config.httpProxy();
@@ -154,7 +147,7 @@ pub fn init(allocator: Allocator, ca_blob: ?c.curl_blob, config: *const Config)
.allocator = allocator,
.http_proxy = http_proxy,
.use_proxy = http_proxy != null,
.user_agent = user_agent,
.config = config,
.transfer_pool = transfer_pool,
};
@@ -172,7 +165,7 @@ pub fn deinit(self: *Client) void {
}
pub fn newHeaders(self: *const Client) !Http.Headers {
return Http.Headers.init(self.user_agent);
return Http.Headers.init(self.config.http_headers.user_agent_header);
}
pub fn abort(self: *Client) void {
@@ -660,8 +653,6 @@ const Handles = struct {
client: *Client,
ca_blob: ?c.curl_blob,
config: *const Config,
user_agent: [:0]const u8,
proxy_bearer_header: ?[:0]const u8,
) !Handles {
const count: usize = config.httpMaxConcurrent();
if (count == 0) return error.InvalidMaxConcurrent;
@@ -671,7 +662,7 @@ const Handles = struct {
var available: HandleList = .{};
for (0..count) |i| {
handles[i] = try Handle.init(client, ca_blob, config, user_agent, proxy_bearer_header);
handles[i] = try Handle.init(client, ca_blob, config);
available.append(&handles[i].node);
}
@@ -722,10 +713,8 @@ pub const Handle = struct {
client: *Client,
ca_blob: ?c.curl_blob,
config: *const Config,
user_agent: [:0]const u8,
proxy_bearer_header: ?[:0]const u8,
) !Handle {
const conn = try Http.Connection.init(ca_blob, config, user_agent, proxy_bearer_header);
const conn = try Http.Connection.init(ca_blob, config);
errdefer conn.deinit();
const easy = conn.easy;

View File

@@ -45,8 +45,6 @@ config: *const Config,
client: *Client,
ca_blob: ?c.curl_blob,
arena: ArenaAllocator,
user_agent: [:0]const u8,
proxy_bearer_header: ?[:0]const u8,
pub fn init(allocator: Allocator, config: *const Config) !Http {
try errorCheck(c.curl_global_init(c.CURL_GLOBAL_SSL));
@@ -59,13 +57,6 @@ pub fn init(allocator: Allocator, config: *const Config) !Http {
var arena = ArenaAllocator.init(allocator);
errdefer arena.deinit();
const user_agent = try config.userAgent(arena.allocator());
var proxy_bearer_header: ?[:0]const u8 = null;
if (config.proxyBearerToken()) |bt| {
proxy_bearer_header = try std.fmt.allocPrintSentinel(arena.allocator(), "Proxy-Authorization: Bearer {s}", .{bt}, 0);
}
var ca_blob: ?c.curl_blob = null;
if (config.tlsVerifyHost()) {
ca_blob = try loadCerts(allocator, arena.allocator());
@@ -79,8 +70,6 @@ pub fn init(allocator: Allocator, config: *const Config) !Http {
.client = client,
.ca_blob = ca_blob,
.config = config,
.user_agent = user_agent,
.proxy_bearer_header = proxy_bearer_header,
};
}
@@ -107,23 +96,16 @@ pub fn removeCDPClient(self: *Http) void {
}
pub fn newConnection(self: *Http) !Connection {
return Connection.init(self.ca_blob, self.config, self.user_agent, self.proxy_bearer_header);
}
pub fn newHeaders(self: *const Http) Headers {
return Headers.init(self.user_agent);
return Connection.init(self.ca_blob, self.config);
}
pub const Connection = struct {
easy: *c.CURL,
user_agent: [:0]const u8,
proxy_bearer_header: ?[:0]const u8,
http_headers: *const Config.HttpHeaders,
pub fn init(
ca_blob_: ?c.curl_blob,
config: *const Config,
user_agent: [:0]const u8,
proxy_bearer_header: ?[:0]const u8,
) !Connection {
const easy = c.curl_easy_init() orelse return error.FailedToInitializeEasy;
errdefer _ = c.curl_easy_cleanup(easy);
@@ -188,8 +170,7 @@ pub const Connection = struct {
return .{
.easy = easy,
.user_agent = user_agent,
.proxy_bearer_header = proxy_bearer_header,
.http_headers = &config.http_headers,
};
}
@@ -243,7 +224,7 @@ pub const Connection = struct {
// These are headers that may not be send to the users for inteception.
pub fn secretHeaders(self: *const Connection, headers: *Headers) !void {
if (self.proxy_bearer_header) |hdr| {
if (self.http_headers.proxy_bearer_header) |hdr| {
try headers.add(hdr);
}
}
@@ -251,7 +232,7 @@ pub const Connection = struct {
pub fn request(self: *const Connection) !u16 {
const easy = self.easy;
var header_list = try Headers.init(self.user_agent);
var header_list = try Headers.init(self.http_headers.user_agent_header);
defer header_list.deinit();
try self.secretHeaders(&header_list);
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_HTTPHEADER, header_list.headers));