Skip to content

Commit b2f645a

Browse files
committed
enable curl cookie engine
Enabling Curl cookie engine brings advantage: * handle cookies during a redirection: when a srv redirects including cookies, curl sends back the cookies correctly during the next request
1 parent 6a29d67 commit b2f645a

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/http/Client.zig

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,22 @@ pub const Transfer = struct {
603603
const header = buffer[0 .. buf_len - 2];
604604

605605
if (transfer.response_header == null) {
606+
if (transfer._redirecting and buf_len == 2) {
607+
// retrieve cookies from the redirect's response.
608+
var i: usize = 0;
609+
while (true) {
610+
const ct = getResponseHeader(easy, "set-cookie", i);
611+
if (ct == null) break;
612+
transfer.req.cookie_jar.populateFromResponse(&transfer.uri, ct.?.value) catch |err| {
613+
log.err(.http, "set cookie", .{ .err = err, .req = transfer });
614+
};
615+
i += 1;
616+
if (i >= ct.?.amount) break;
617+
}
618+
619+
return buf_len;
620+
}
621+
606622
if (buf_len < 13 or std.mem.startsWith(u8, header, "HTTP/") == false) {
607623
if (transfer._redirecting) {
608624
return buf_len;
@@ -641,18 +657,6 @@ pub const Transfer = struct {
641657
return buf_len;
642658
}
643659

644-
{
645-
const SET_COOKIE_LEN = "set-cookie:".len;
646-
if (header.len > SET_COOKIE_LEN) {
647-
if (std.ascii.eqlIgnoreCase(header[0..SET_COOKIE_LEN], "set-cookie:")) {
648-
const value = std.mem.trimLeft(u8, header[SET_COOKIE_LEN..], " ");
649-
transfer.req.cookie_jar.populateFromResponse(&transfer.uri, value) catch |err| {
650-
log.err(.http, "set cookie", .{ .err = err, .req = transfer });
651-
};
652-
}
653-
}
654-
}
655-
656660
if (buf_len == 2) {
657661
if (getResponseHeader(easy, "content-type", 0)) |ct| {
658662
var hdr = &transfer.response_header.?;
@@ -662,6 +666,17 @@ pub const Transfer = struct {
662666
@memcpy(hdr._content_type[0..len], value[0..len]);
663667
}
664668

669+
var i: usize = 0;
670+
while (true) {
671+
const ct = getResponseHeader(easy, "set-cookie", i);
672+
if (ct == null) break;
673+
transfer.req.cookie_jar.populateFromResponse(&transfer.uri, ct.?.value) catch |err| {
674+
log.err(.http, "set cookie", .{ .err = err, .req = transfer });
675+
};
676+
i += 1;
677+
if (i >= ct.?.amount) break;
678+
}
679+
665680
transfer.req.header_done_callback(transfer) catch |err| {
666681
log.err(.http, "header_done_callback", .{ .err = err, .req = transfer });
667682
// returning < buf_len terminates the request

src/http/Http.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ pub const Connection = struct {
110110
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_FOLLOWLOCATION, @as(c_long, 2)));
111111
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_REDIR_PROTOCOLS_STR, "HTTP,HTTPS")); // remove FTP and FTPS from the default
112112

113+
// enable cookie engine for redirections handled directly by Curl.
114+
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_COOKIEFILE, ""));
115+
113116
// proxy
114117
if (opts.http_proxy) |proxy| {
115118
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_PROXY, proxy.ptr));

0 commit comments

Comments
 (0)