This commit is contained in:
sjorsdonkers
2025-08-12 14:40:23 +02:00
parent 03694b54f0
commit 77eee7f087
5 changed files with 31 additions and 24 deletions

View File

@@ -229,12 +229,15 @@ pub fn addFromElement(self: *ScriptManager, element: *parser.Element) !void {
errdefer pending_script.deinit();
var headers = try HttpClient.Headers.init();
try page.requestCookie(.{}).headersForRequest(self.allocator, remote_url.?, &headers);
try self.client.request(.{
.url = remote_url.?,
.ctx = pending_script,
.method = .GET,
.headers = try HttpClient.Headers.init(),
.cookie = page.requestCookie(.{}),
.headers = headers,
.cookie_jar = page.cookie_jar,
.start_callback = if (log.enabled(.http, .debug)) startCallback else null,
.header_done_callback = headerCallback,
.data_callback = dataCallback,
@@ -293,13 +296,16 @@ pub fn blockingGet(self: *ScriptManager, url: [:0]const u8) !BlockingResult {
.buffer_pool = &self.buffer_pool,
};
var headers = try HttpClient.Headers.init();
try self.page.requestCookie(.{}).headersForRequest(self.allocator, url, &headers);
var client = self.client;
try client.blockingRequest(.{
.url = url,
.method = .GET,
.headers = try HttpClient.Headers.init(),
.headers = headers,
.cookie_jar = self.page.cookie_jar,
.ctx = &blocking,
.cookie = self.page.requestCookie(.{}),
.start_callback = if (log.enabled(.http, .debug)) Blocking.startCallback else null,
.header_done_callback = Blocking.headerCallback,
.data_callback = Blocking.dataCallback,

View File

@@ -469,6 +469,7 @@ pub const Page = struct {
var headers = try HttpClient.Headers.init();
if (opts.header) |hdr| try headers.add(hdr);
try self.requestCookie(.{ .is_navigation = true }).headersForRequest(self.arena, owned_url, &headers);
self.http_client.request(.{
.ctx = self,
@@ -476,7 +477,7 @@ pub const Page = struct {
.method = opts.method,
.headers = headers,
.body = opts.body,
.cookie = self.requestCookie(.{ .is_navigation = true }),
.cookie_jar = self.cookie_jar,
.header_done_callback = pageHeaderDoneCallback,
.data_callback = pageDataCallback,
.done_callback = pageDoneCallback,

View File

@@ -374,6 +374,7 @@ pub const XMLHttpRequest = struct {
for (self.headers.items) |hdr| {
try headers.add(hdr);
}
try page.requestCookie(.{}).headersForRequest(self.arena, self.url.?, &headers);
try page.http_client.request(.{
.ctx = self,
@@ -381,7 +382,7 @@ pub const XMLHttpRequest = struct {
.method = self.method,
.headers = headers,
.body = self.request_body,
.cookie = page.requestCookie(.{}),
.cookie_jar = page.cookie_jar,
.start_callback = httpStartCallback,
.header_callback = httpHeaderCallback,
.header_done_callback = httpHeaderDoneCallback,

View File

@@ -22,6 +22,7 @@ const builtin = @import("builtin");
const Http = @import("Http.zig");
pub const Headers = Http.Headers;
const Notification = @import("../notification.zig").Notification;
const storage = @import("../browser/storage/storage.zig");
const c = Http.c;
@@ -271,20 +272,6 @@ fn makeRequest(self: *Client, handle: *Handle, req: Request) !void {
try conn.setBody(b);
}
// { // TODO move up to `fn request()`
// const aa = self.arena.allocator();
// var arr: std.ArrayListUnmanaged(u8) = .{};
// try req.cookie.forRequest(&uri, arr.writer(aa));
// if (arr.items.len > 0) {
// try arr.append(aa, 0); //null terminate
// // copies the value
// header_list = c.curl_slist_append(header_list, @ptrCast(arr.items.ptr));
// defer _ = self.arena.reset(.{ .retain_with_limit = 2048 });
// }
// }
try conn.secretHeaders(&header_list); // Add headers that must be hidden from intercepts
try errorCheck(c.curl_easy_setopt(easy, c.CURLOPT_HTTPHEADER, header_list.headers));
}
@@ -475,13 +462,24 @@ pub const RequestCookie = struct {
origin: *const std.Uri,
jar: *@import("../browser/storage/cookie.zig").Jar,
fn forRequest(self: *const RequestCookie, uri: *const std.Uri, writer: anytype) !void {
return self.jar.forRequest(uri, writer, .{
pub fn headersForRequest(self: *const RequestCookie, temp: Allocator, url: [:0]const u8, headers: *Headers) !void {
const uri = std.Uri.parse(url) catch |err| {
log.warn(.http, "invalid url", .{ .err = err, .url = url });
return error.InvalidUrl;
};
var arr: std.ArrayListUnmanaged(u8) = .{};
try self.jar.forRequest(&uri, arr.writer(temp), .{
.is_http = self.is_http,
.is_navigation = self.is_navigation,
.origin_uri = self.origin,
.prefix = "Cookie: ",
});
if (arr.items.len > 0) {
try arr.append(temp, 0); //null terminate
try headers.add(@ptrCast(arr.items.ptr));
}
}
};
@@ -491,7 +489,7 @@ pub const Request = struct {
url: [:0]const u8,
headers: Headers,
body: ?[]const u8 = null,
cookie: RequestCookie,
cookie_jar: *storage.CookieJar,
// arbitrary data that can be associated with this request
ctx: *anyopaque = undefined,
@@ -616,7 +614,7 @@ pub const Transfer = struct {
if (header.len > SET_COOKIE_LEN) {
if (std.ascii.eqlIgnoreCase(header[0..SET_COOKIE_LEN], "set-cookie:")) {
const value = std.mem.trimLeft(u8, header[SET_COOKIE_LEN..], " ");
transfer.req.cookie.jar.populateFromResponse(&transfer.uri, value) catch |err| {
transfer.req.cookie_jar.populateFromResponse(&transfer.uri, value) catch |err| {
log.err(.http, "set cookie", .{ .err = err, .req = transfer });
};
}

View File

@@ -224,6 +224,7 @@ pub const Headers = struct {
}
pub fn add(self: *Headers, header: [*c]const u8) !void {
// Copies the value
const updated_headers = c.curl_slist_append(self.headers, header);
if (updated_headers == null) return error.OutOfMemory;
self.headers = updated_headers;