http: move use_proxy from connection to client

This commit is contained in:
Pierre Tachoire
2025-08-22 11:58:48 +02:00
parent e61d787ff0
commit a7516061d0
2 changed files with 7 additions and 11 deletions

View File

@@ -89,6 +89,9 @@ notification: ?*Notification = null,
// restoring, this originally-configured value is what it goes to.
http_proxy: ?[:0]const u8 = null,
// does the client use a proxy?
use_proxy: bool = false,
const TransferQueue = std.DoublyLinkedList(*Transfer);
pub fn init(allocator: Allocator, ca_blob: ?c.curl_blob, opts: Http.Opts) !*Client {
@@ -120,6 +123,7 @@ pub fn init(allocator: Allocator, ca_blob: ?c.curl_blob, opts: Http.Opts) !*Clie
.blocking = blocking,
.allocator = allocator,
.http_proxy = opts.http_proxy,
.use_proxy = opts.http_proxy != null,
.transfer_pool = transfer_pool,
.queue_node_pool = queue_node_pool,
};
@@ -242,6 +246,7 @@ fn makeTransfer(self: *Client, req: Request) !*Transfer {
.req = req,
.ctx = req.ctx,
.client = self,
._use_proxy = self.use_proxy,
};
return transfer;
}
@@ -278,11 +283,10 @@ fn requestFailed(self: *Client, transfer: *Transfer, err: anyerror) void {
pub fn changeProxy(self: *Client, proxy: [:0]const u8) !void {
try self.ensureNoActiveConnection();
self.use_proxy = true;
for (self.handles.handles) |*h| {
h.conn.opts.use_proxy = true;
try errorCheck(c.curl_easy_setopt(h.conn.easy, c.CURLOPT_PROXY, proxy.ptr));
}
self.blocking.conn.opts.use_proxy = true;
try errorCheck(c.curl_easy_setopt(self.blocking.conn.easy, c.CURLOPT_PROXY, proxy.ptr));
}
@@ -291,13 +295,12 @@ pub fn changeProxy(self: *Client, proxy: [:0]const u8) !void {
pub fn restoreOriginalProxy(self: *Client) !void {
try self.ensureNoActiveConnection();
self.use_proxy = self.http_proxy != null;
const proxy = if (self.http_proxy) |p| p.ptr else null;
for (self.handles.handles) |*h| {
h.conn.opts.use_proxy = proxy != null;
try errorCheck(c.curl_easy_setopt(h.conn.easy, c.CURLOPT_PROXY, proxy));
}
try errorCheck(c.curl_easy_setopt(self.blocking.conn.easy, c.CURLOPT_PROXY, proxy));
self.blocking.conn.opts.use_proxy = proxy != null;
}
fn makeRequest(self: *Client, handle: *Handle, transfer: *Transfer) !void {
@@ -309,9 +312,6 @@ fn makeRequest(self: *Client, handle: *Handle, transfer: *Transfer) !void {
transfer._handle = handle;
errdefer transfer.deinit();
// Store the proxy's information in transfer to ease headers parsing.
transfer._use_proxy = conn.opts.use_proxy;
try conn.setURL(req.url);
try conn.setMethod(req.method);
if (req.body) |b| {

View File

@@ -94,7 +94,6 @@ pub const Connection = struct {
opts: Connection.Opts,
const Opts = struct {
use_proxy: bool,
proxy_bearer_token: ?[:0]const u8,
};
@@ -113,10 +112,8 @@ pub const Connection = struct {
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_REDIR_PROTOCOLS_STR, "HTTP,HTTPS")); // remove FTP and FTPS from the default
// proxy
var use_proxy = false;
if (opts.http_proxy) |proxy| {
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_PROXY, proxy.ptr));
use_proxy = true;
}
// tls
@@ -158,7 +155,6 @@ pub const Connection = struct {
return .{
.easy = easy,
.opts = .{
.use_proxy = use_proxy,
.proxy_bearer_token = opts.proxy_bearer_token,
},
};