use CURLOPT_COOKIE to set cookies

This commit is contained in:
Pierre Tachoire
2025-08-13 17:51:05 +02:00
parent b2f645a5ce
commit 7d0e4b6270
2 changed files with 26 additions and 3 deletions

View File

@@ -319,6 +319,14 @@ fn makeRequest(self: *Client, handle: *Handle, transfer: *Transfer) !void {
var header_list = req.headers; var header_list = req.headers;
try conn.secretHeaders(&header_list); // Add headers that must be hidden from intercepts try conn.secretHeaders(&header_list); // Add headers that must be hidden from intercepts
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_HTTPHEADER, header_list.headers)); try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_HTTPHEADER, header_list.headers));
// Add cookies.
// Clear cookies from Curl's engine.
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_COOKIELIST, "ALL"));
if (header_list.cookies) |cookies| {
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_COOKIE, cookies));
}
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_PRIVATE, transfer)); try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_PRIVATE, transfer));
} }
@@ -506,12 +514,11 @@ pub const RequestCookie = struct {
.is_http = self.is_http, .is_http = self.is_http,
.is_navigation = self.is_navigation, .is_navigation = self.is_navigation,
.origin_uri = self.origin, .origin_uri = self.origin,
.prefix = "Cookie: ",
}); });
if (arr.items.len > 0) { if (arr.items.len > 0) {
try arr.append(temp, 0); //null terminate try arr.append(temp, 0); //null terminate
try headers.add(@ptrCast(arr.items.ptr)); headers.cookies = @ptrCast(arr.items.ptr);
} }
} }
}; };

View File

@@ -204,6 +204,13 @@ pub const Connection = struct {
try self.secretHeaders(&header_list); try self.secretHeaders(&header_list);
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_HTTPHEADER, header_list.headers)); try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_HTTPHEADER, header_list.headers));
// Add cookies.
// Clear cookies from Curl's engine.
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_COOKIELIST, "ALL"));
if (header_list.cookies) |cookies| {
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_COOKIE, cookies));
}
try errorCheck(c.curl_easy_perform(easy)); try errorCheck(c.curl_easy_perform(easy));
var http_code: c_long = undefined; var http_code: c_long = undefined;
try errorCheck(c.curl_easy_getinfo(easy, c.CURLINFO_RESPONSE_CODE, &http_code)); try errorCheck(c.curl_easy_getinfo(easy, c.CURLINFO_RESPONSE_CODE, &http_code));
@@ -216,11 +223,12 @@ pub const Connection = struct {
pub const Headers = struct { pub const Headers = struct {
headers: *c.curl_slist, headers: *c.curl_slist,
cookies: ?[*c]const u8,
pub fn init() !Headers { pub fn init() !Headers {
const header_list = c.curl_slist_append(null, "User-Agent: Lightpanda/1.0"); const header_list = c.curl_slist_append(null, "User-Agent: Lightpanda/1.0");
if (header_list == null) return error.OutOfMemory; if (header_list == null) return error.OutOfMemory;
return .{ .headers = header_list }; return .{ .headers = header_list, .cookies = null };
} }
pub fn deinit(self: *const Headers) void { pub fn deinit(self: *const Headers) void {
@@ -245,6 +253,10 @@ pub const Headers = struct {
list.putAssumeCapacity(header.name, header.value); list.putAssumeCapacity(header.name, header.value);
current = node.*.next; current = node.*.next;
} }
// special case for cookies
if (self.cookies) |v| {
list.putAssumeCapacity("Cookie", std.mem.span(@as([*:0]const u8, @ptrCast(v))));
}
return list; return list;
} }
@@ -264,6 +276,10 @@ pub const Headers = struct {
num += 1; num += 1;
current = node.*.next; current = node.*.next;
} }
// special case for cookies
if (self.cookies != null) {
num += 1;
}
return num; return num;
} }
}; };