Skip to content

Commit 6418203

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 a707e10 commit 6418203

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/http/Client.zig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,18 @@ pub const Transfer = struct {
602602

603603
const header = buffer[0 .. buf_len - 2];
604604

605+
{
606+
const SET_COOKIE_LEN = "set-cookie:".len;
607+
if (header.len > SET_COOKIE_LEN) {
608+
if (std.ascii.eqlIgnoreCase(header[0..SET_COOKIE_LEN], "set-cookie:")) {
609+
const value = std.mem.trimLeft(u8, header[SET_COOKIE_LEN..], " ");
610+
transfer.req.cookie_jar.populateFromResponse(&transfer.uri, value) catch |err| {
611+
log.err(.http, "set cookie", .{ .err = err, .req = transfer });
612+
};
613+
}
614+
}
615+
}
616+
605617
if (transfer.response_header == null) {
606618
if (buf_len < 13 or std.mem.startsWith(u8, header, "HTTP/") == false) {
607619
if (transfer._redirecting) {
@@ -655,18 +667,6 @@ pub const Transfer = struct {
655667
}
656668
}
657669

658-
{
659-
const SET_COOKIE_LEN = "set-cookie:".len;
660-
if (header.len > SET_COOKIE_LEN) {
661-
if (std.ascii.eqlIgnoreCase(header[0..SET_COOKIE_LEN], "set-cookie:")) {
662-
const value = std.mem.trimLeft(u8, header[SET_COOKIE_LEN..], " ");
663-
transfer.req.cookie_jar.populateFromResponse(&transfer.uri, value) catch |err| {
664-
log.err(.http, "set cookie", .{ .err = err, .req = transfer });
665-
};
666-
}
667-
}
668-
}
669-
670670
if (buf_len == 2) {
671671
if (getResponseHeader(easy, "content-type")) |value| {
672672
const len = @min(value.len, hdr._content_type.len);

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)