Skip to content

Commit 7c02554

Browse files
committed
use CURLOPT_COOKIE to set cookies
1 parent 6418203 commit 7c02554

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/http/Client.zig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,14 @@ fn makeRequest(self: *Client, handle: *Handle, transfer: *Transfer) !void {
319319
var header_list = req.headers;
320320
try conn.secretHeaders(&header_list); // Add headers that must be hidden from intercepts
321321
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_HTTPHEADER, header_list.headers));
322+
323+
// Add cookies.
324+
// Clear cookies from Curl's engine.
325+
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_COOKIELIST, "ALL"));
326+
if (header_list.cookies) |cookies| {
327+
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_COOKIE, cookies));
328+
}
329+
322330
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_PRIVATE, transfer));
323331
}
324332

@@ -506,12 +514,11 @@ pub const RequestCookie = struct {
506514
.is_http = self.is_http,
507515
.is_navigation = self.is_navigation,
508516
.origin_uri = self.origin,
509-
.prefix = "Cookie: ",
510517
});
511518

512519
if (arr.items.len > 0) {
513520
try arr.append(temp, 0); //null terminate
514-
try headers.add(@ptrCast(arr.items.ptr));
521+
headers.cookies = @ptrCast(arr.items.ptr);
515522
}
516523
}
517524
};

src/http/Http.zig

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ pub const Connection = struct {
204204
try self.secretHeaders(&header_list);
205205
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_HTTPHEADER, header_list.headers));
206206

207+
// Add cookies.
208+
// Clear cookies from Curl's engine.
209+
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_COOKIELIST, "ALL"));
210+
if (header_list.cookies) |cookies| {
211+
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_COOKIE, cookies));
212+
}
213+
207214
try errorCheck(c.curl_easy_perform(easy));
208215
var http_code: c_long = undefined;
209216
try errorCheck(c.curl_easy_getinfo(easy, c.CURLINFO_RESPONSE_CODE, &http_code));
@@ -216,11 +223,12 @@ pub const Connection = struct {
216223

217224
pub const Headers = struct {
218225
headers: *c.curl_slist,
226+
cookies: ?[*c]const u8,
219227

220228
pub fn init() !Headers {
221229
const header_list = c.curl_slist_append(null, "User-Agent: Lightpanda/1.0");
222230
if (header_list == null) return error.OutOfMemory;
223-
return .{ .headers = header_list };
231+
return .{ .headers = header_list, .cookies = null };
224232
}
225233

226234
pub fn deinit(self: *const Headers) void {
@@ -245,6 +253,10 @@ pub const Headers = struct {
245253
list.putAssumeCapacity(header.name, header.value);
246254
current = node.*.next;
247255
}
256+
// special case for cookies
257+
if (self.cookies) |v| {
258+
list.putAssumeCapacity("Cookie", std.mem.span(@as([*:0]const u8, @ptrCast(v))));
259+
}
248260
return list;
249261
}
250262

@@ -264,6 +276,10 @@ pub const Headers = struct {
264276
num += 1;
265277
current = node.*.next;
266278
}
279+
// special case for cookies
280+
if (self.cookies != null) {
281+
num += 1;
282+
}
267283
return num;
268284
}
269285
};

0 commit comments

Comments
 (0)