Merge pull request #953 from lightpanda-io/is_navigation_and_arena

Use Transfer.arena in a few more places, correctly set is_navigation …
This commit is contained in:
Pierre Tachoire
2025-08-20 08:59:32 +02:00
committed by GitHub
2 changed files with 17 additions and 14 deletions

View File

@@ -244,21 +244,25 @@ fn continueRequest(cmd: anytype) !void {
.new_url = params.url, .new_url = params.url,
}); });
const arena = transfer.arena.allocator();
// Update the request with the new parameters // Update the request with the new parameters
if (params.url) |url| { if (params.url) |url| {
try transfer.updateURL(try page.arena.dupeZ(u8, url)); try transfer.updateURL(try arena.dupeZ(u8, url));
} }
if (params.method) |method| { if (params.method) |method| {
transfer.req.method = std.meta.stringToEnum(Http.Method, method) orelse return error.InvalidParams; transfer.req.method = std.meta.stringToEnum(Http.Method, method) orelse return error.InvalidParams;
} }
if (params.headers) |headers| { if (params.headers) |headers| {
// Not obvious, but cmd.arena is safe here, since the headers will get
// duped by libcurl. transfer.arena is more obvious/safe, but cmd.arena
// is more efficient (it's re-used)
try transfer.replaceRequestHeaders(cmd.arena, headers); try transfer.replaceRequestHeaders(cmd.arena, headers);
} }
if (params.postData) |b| { if (params.postData) |b| {
const decoder = std.base64.standard.Decoder; const decoder = std.base64.standard.Decoder;
const body = try bc.arena.alloc(u8, try decoder.calcSizeForSlice(b)); const body = try arena.alloc(u8, try decoder.calcSizeForSlice(b));
try decoder.decode(body, b); try decoder.decode(body, b);
transfer.req.body = body; transfer.req.body = body;
} }
@@ -304,7 +308,7 @@ fn fulfillRequest(cmd: anytype) !void {
var body: ?[]const u8 = null; var body: ?[]const u8 = null;
if (params.body) |b| { if (params.body) |b| {
const decoder = std.base64.standard.Decoder; const decoder = std.base64.standard.Decoder;
const buf = try cmd.arena.alloc(u8, try decoder.calcSizeForSlice(b)); const buf = try transfer.arena.allocator().alloc(u8, try decoder.calcSizeForSlice(b));
try decoder.decode(buf, b); try decoder.decode(buf, b);
body = buf; body = buf;
} }

View File

@@ -620,13 +620,16 @@ pub const Transfer = struct {
// redirectionCookies manages cookies during redirections handled by Curl. // redirectionCookies manages cookies during redirections handled by Curl.
// It sets the cookies from the current response to the cookie jar. // It sets the cookies from the current response to the cookie jar.
// It also immediately sets cookies for the following request. // It also immediately sets cookies for the following request.
fn redirectionCookies(arena: Allocator, easy: *c.CURL, cookie_jar: *CookieJar, origin: *const std.Uri) !void { fn redirectionCookies(transfer: *Transfer, easy: *c.CURL) !void {
const req = &transfer.req;
const arena = transfer.arena.allocator();
// retrieve cookies from the redirect's response. // retrieve cookies from the redirect's response.
var i: usize = 0; var i: usize = 0;
while (true) { while (true) {
const ct = getResponseHeader(easy, "set-cookie", i); const ct = getResponseHeader(easy, "set-cookie", i);
if (ct == null) break; if (ct == null) break;
try cookie_jar.populateFromResponse(origin, ct.?.value); try req.cookie_jar.populateFromResponse(&transfer.uri, ct.?.value);
i += 1; i += 1;
if (i >= ct.?.amount) break; if (i >= ct.?.amount) break;
} }
@@ -644,10 +647,11 @@ pub const Transfer = struct {
const uri = try std.Uri.parse(url); const uri = try std.Uri.parse(url);
var cookies: std.ArrayListUnmanaged(u8) = .{}; var cookies: std.ArrayListUnmanaged(u8) = .{};
try cookie_jar.forRequest(&uri, cookies.writer(arena), .{ try req.cookie_jar.forRequest(&uri, cookies.writer(arena), .{
.is_http = true, .is_http = true,
.is_navigation = true, .origin_uri = &transfer.uri,
.origin_uri = origin, // used to enforce samesite cookie rules
.is_navigation = req.resource_type == .document,
}); });
try cookies.append(arena, 0); //null terminate try cookies.append(arena, 0); //null terminate
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_COOKIE, @as([*c]const u8, @ptrCast(cookies.items.ptr)))); 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 {
if (transfer.response_header == null) { if (transfer.response_header == null) {
if (transfer._redirecting and buf_len == 2) { if (transfer._redirecting and buf_len == 2) {
// parse and set cookies for the redirection. // parse and set cookies for the redirection.
redirectionCookies( redirectionCookies(transfer, easy) catch |err| {
transfer.arena.allocator(),
easy,
transfer.req.cookie_jar,
&transfer.uri,
) catch |err| {
log.debug(.http, "redirection cookies", .{ .err = err }); log.debug(.http, "redirection cookies", .{ .err = err });
return 0; return 0;
}; };