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

@@ -204,6 +204,13 @@ pub const Connection = struct {
try self.secretHeaders(&header_list);
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));
var http_code: c_long = undefined;
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 {
headers: *c.curl_slist,
cookies: ?[*c]const u8,
pub fn init() !Headers {
const header_list = c.curl_slist_append(null, "User-Agent: Lightpanda/1.0");
if (header_list == null) return error.OutOfMemory;
return .{ .headers = header_list };
return .{ .headers = header_list, .cookies = null };
}
pub fn deinit(self: *const Headers) void {
@@ -245,6 +253,10 @@ pub const Headers = struct {
list.putAssumeCapacity(header.name, header.value);
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;
}
@@ -264,6 +276,10 @@ pub const Headers = struct {
num += 1;
current = node.*.next;
}
// special case for cookies
if (self.cookies != null) {
num += 1;
}
return num;
}
};