From 3554634c1c1a6851aaedf26f23ca193b46a2d01d Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Wed, 6 Aug 2025 13:14:58 +0800 Subject: [PATCH] cleanup optional request headers --- src/browser/page.zig | 4 ++++ src/browser/storage/cookie.zig | 8 +++++++- src/browser/xhr/xhr.zig | 1 - src/http/Client.zig | 14 ++++++-------- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/browser/page.zig b/src/browser/page.zig index 54571262..a368409d 100644 --- a/src/browser/page.zig +++ b/src/browser/page.zig @@ -472,6 +472,7 @@ pub const Page = struct { .url = owned_url, .method = opts.method, .body = opts.body, + .header = opts.header, .cookie = self.requestCookie(.{ .is_navigation = true }), .header_done_callback = pageHeaderDoneCallback, .data_callback = pageDataCallback, @@ -931,6 +932,8 @@ pub const Page = struct { if (std.ascii.eqlIgnoreCase(method, "post")) { opts.method = .POST; opts.body = buf.items; + // form_data.write currently only supports this encoding, so we know this has to be the content type + opts.header = "Content-Type: application/x-www-form-urlencoded"; } else { action = try URL.concatQueryString(transfer_arena, action, buf.items); } @@ -986,6 +989,7 @@ pub const NavigateOpts = struct { reason: NavigateReason = .address_bar, method: HttpClient.Method = .GET, body: ?[]const u8 = null, + header: ?[:0]const u8 = null, }; fn timestamp() u32 { diff --git a/src/browser/storage/cookie.zig b/src/browser/storage/cookie.zig index 98104c0e..25c96071 100644 --- a/src/browser/storage/cookie.zig +++ b/src/browser/storage/cookie.zig @@ -12,6 +12,7 @@ pub const LookupOpts = struct { origin_uri: ?*const Uri = null, is_http: bool, is_navigation: bool = true, + prefix: ?[]const u8 = null, }; pub const Jar = struct { @@ -91,10 +92,15 @@ pub const Jar = struct { var first = true; for (self.cookies.items) |*cookie| { - if (!cookie.appliesTo(&target, same_site, opts.is_navigation, opts.is_http)) continue; + if (!cookie.appliesTo(&target, same_site, opts.is_navigation, opts.is_http)) { + continue; + } // we have a match! if (first) { + if (opts.prefix) |prefix| { + try writer.writeAll(prefix); + } first = false; } else { try writer.writeAll("; "); diff --git a/src/browser/xhr/xhr.zig b/src/browser/xhr/xhr.zig index a91967e7..94c54461 100644 --- a/src/browser/xhr/xhr.zig +++ b/src/browser/xhr/xhr.zig @@ -375,7 +375,6 @@ pub const XMLHttpRequest = struct { .url = self.url.?, .method = self.method, .body = self.request_body, - .content_type = "Content-Type: text/plain; charset=UTF-8", // @newhttp TODO .cookie = page.requestCookie(.{}), .start_callback = httpStartCallback, .header_callback = httpHeaderCallback, diff --git a/src/http/Client.zig b/src/http/Client.zig index 0493279d..c9aa6651 100644 --- a/src/http/Client.zig +++ b/src/http/Client.zig @@ -263,24 +263,21 @@ fn makeRequest(self: *Client, handle: *Handle, req: Request) !void { var header_list = conn.commonHeaders(); errdefer c.curl_slist_free_all(header_list); - if (req.content_type) |ct| { - header_list = c.curl_slist_append(header_list, ct); + if (req.header) |hdr| { + header_list = c.curl_slist_append(header_list, hdr); } { - const COOKIE_HEADER = "Cookie: "; const aa = self.arena.allocator(); - defer _ = self.arena.reset(.{ .retain_with_limit = 2048 }); - var arr: std.ArrayListUnmanaged(u8) = .{}; - try arr.appendSlice(aa, COOKIE_HEADER); try req.cookie.forRequest(&uri, arr.writer(aa)); - if (arr.items.len > COOKIE_HEADER.len) { + 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 }); } } @@ -485,6 +482,7 @@ pub const RequestCookie = struct { .is_http = self.is_http, .is_navigation = self.is_navigation, .origin_uri = self.origin, + .prefix = "Cookie: ", }); } }; @@ -493,7 +491,7 @@ pub const Request = struct { method: Method, url: [:0]const u8, body: ?[]const u8 = null, - content_type: ?[:0]const u8 = null, + header: ?[:0]const u8 = null, cookie: RequestCookie, // arbitrary data that can be associated with this request