Skip to content

Commit 0dde140

Browse files
committed
use CURLOPT_COOKIE to set cookies
1 parent 3db5af7 commit 0dde140

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
@@ -320,6 +320,14 @@ fn makeRequest(self: *Client, handle: *Handle, transfer: *Transfer) !void {
320320
var header_list = req.headers;
321321
try conn.secretHeaders(&header_list); // Add headers that must be hidden from intercepts
322322
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_HTTPHEADER, header_list.headers));
323+
324+
// Add cookies.
325+
// Clear cookies from Curl's engine.
326+
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_COOKIELIST, "ALL"));
327+
if (header_list.cookies) |cookies| {
328+
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_COOKIE, cookies));
329+
}
330+
323331
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_PRIVATE, transfer));
324332
}
325333

@@ -507,12 +515,11 @@ pub const RequestCookie = struct {
507515
.is_http = self.is_http,
508516
.is_navigation = self.is_navigation,
509517
.origin_uri = self.origin,
510-
.prefix = "Cookie: ",
511518
});
512519

513520
if (arr.items.len > 0) {
514521
try arr.append(temp, 0); //null terminate
515-
try headers.add(@ptrCast(arr.items.ptr));
522+
headers.cookies = @ptrCast(arr.items.ptr);
516523
}
517524
}
518525
};

src/http/Http.zig

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

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

216223
pub const Headers = struct {
217224
headers: *c.curl_slist,
225+
cookies: ?[*c]const u8,
218226

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

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

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

0 commit comments

Comments
 (0)