Skip to content

Commit 16c85c5

Browse files
committed
Use Transfer.arena in a few more places, correctly set is_navigation on redirect
Following up to Request Interception PR (1) and Cookie Redirect PR (2) which both introduced features that were useful to the other. This PR closes that loop. (1) #946 (2) #948
1 parent 2c7b399 commit 16c85c5

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/cdp/domains/fetch.zig

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,21 +244,25 @@ fn continueRequest(cmd: anytype) !void {
244244
.new_url = params.url,
245245
});
246246

247+
const arena = transfer.arena.allocator();
247248
// Update the request with the new parameters
248249
if (params.url) |url| {
249-
try transfer.updateURL(try page.arena.dupeZ(u8, url));
250+
try transfer.updateURL(try arena.dupeZ(u8, url));
250251
}
251252
if (params.method) |method| {
252253
transfer.req.method = std.meta.stringToEnum(Http.Method, method) orelse return error.InvalidParams;
253254
}
254255

255256
if (params.headers) |headers| {
257+
// Not obvious, but cmd.arena is safe here, since the headers will get
258+
// duped by libcurl. transfer.arena is more obvious/safe, but cmd.arena
259+
// is more efficient (it's re-used)
256260
try transfer.replaceRequestHeaders(cmd.arena, headers);
257261
}
258262

259263
if (params.postData) |b| {
260264
const decoder = std.base64.standard.Decoder;
261-
const body = try bc.arena.alloc(u8, try decoder.calcSizeForSlice(b));
265+
const body = try arena.alloc(u8, try decoder.calcSizeForSlice(b));
262266
try decoder.decode(body, b);
263267
transfer.req.body = body;
264268
}
@@ -304,7 +308,7 @@ fn fulfillRequest(cmd: anytype) !void {
304308
var body: ?[]const u8 = null;
305309
if (params.body) |b| {
306310
const decoder = std.base64.standard.Decoder;
307-
const buf = try cmd.arena.alloc(u8, try decoder.calcSizeForSlice(b));
311+
const buf = try transfer.arena.allocator().alloc(u8, try decoder.calcSizeForSlice(b));
308312
try decoder.decode(buf, b);
309313
body = buf;
310314
}

src/http/Client.zig

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)