@@ -620,13 +620,16 @@ pub const Transfer = struct {
620620 // redirectionCookies manages cookies during redirections handled by Curl.
621621 // It sets the cookies from the current response to the cookie jar.
622622 // It also immediately sets cookies for the following request.
623- fn redirectionCookies (arena : Allocator , easy : * c.CURL , cookie_jar : * CookieJar , origin : * const std.Uri ) ! void {
623+ fn redirectionCookies (transfer : * Transfer , easy : * c.CURL ) ! void {
624+ const req = & transfer .req ;
625+ const arena = transfer .arena .allocator ();
626+
624627 // retrieve cookies from the redirect's response.
625628 var i : usize = 0 ;
626629 while (true ) {
627630 const ct = getResponseHeader (easy , "set-cookie" , i );
628631 if (ct == null ) break ;
629- try cookie_jar .populateFromResponse (origin , ct .? .value );
632+ try req . cookie_jar .populateFromResponse (& transfer . uri , ct .? .value );
630633 i += 1 ;
631634 if (i >= ct .? .amount ) break ;
632635 }
@@ -644,10 +647,11 @@ pub const Transfer = struct {
644647 const uri = try std .Uri .parse (url );
645648
646649 var cookies : std .ArrayListUnmanaged (u8 ) = .{};
647- try cookie_jar .forRequest (& uri , cookies .writer (arena ), .{
650+ try req . cookie_jar .forRequest (& uri , cookies .writer (arena ), .{
648651 .is_http = true ,
649- .is_navigation = true ,
650- .origin_uri = origin ,
652+ .origin_uri = & transfer .uri ,
653+ // used to enforce samesite cookie rules
654+ .is_navigation = req .resource_type == .document ,
651655 });
652656 try cookies .append (arena , 0 ); //null terminate
653657 try errorCheck (c .curl_easy_setopt (easy , c .CURLOPT_COOKIE , @as ([* c ]const u8 , @ptrCast (cookies .items .ptr ))));
@@ -670,12 +674,7 @@ pub const Transfer = struct {
670674 if (transfer .response_header == null ) {
671675 if (transfer ._redirecting and buf_len == 2 ) {
672676 // parse and set cookies for the redirection.
673- redirectionCookies (
674- transfer .arena .allocator (),
675- easy ,
676- transfer .req .cookie_jar ,
677- & transfer .uri ,
678- ) catch | err | {
677+ redirectionCookies (transfer , easy ) catch | err | {
679678 log .debug (.http , "redirection cookies" , .{ .err = err });
680679 return 0 ;
681680 };
0 commit comments