cleanup optional request headers

This commit is contained in:
Karl Seguin
2025-08-06 13:14:58 +08:00
parent c96fb3c2f2
commit 3554634c1c
4 changed files with 17 additions and 10 deletions

View File

@@ -472,6 +472,7 @@ pub const Page = struct {
.url = owned_url, .url = owned_url,
.method = opts.method, .method = opts.method,
.body = opts.body, .body = opts.body,
.header = opts.header,
.cookie = self.requestCookie(.{ .is_navigation = true }), .cookie = self.requestCookie(.{ .is_navigation = true }),
.header_done_callback = pageHeaderDoneCallback, .header_done_callback = pageHeaderDoneCallback,
.data_callback = pageDataCallback, .data_callback = pageDataCallback,
@@ -931,6 +932,8 @@ pub const Page = struct {
if (std.ascii.eqlIgnoreCase(method, "post")) { if (std.ascii.eqlIgnoreCase(method, "post")) {
opts.method = .POST; opts.method = .POST;
opts.body = buf.items; 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 { } else {
action = try URL.concatQueryString(transfer_arena, action, buf.items); action = try URL.concatQueryString(transfer_arena, action, buf.items);
} }
@@ -986,6 +989,7 @@ pub const NavigateOpts = struct {
reason: NavigateReason = .address_bar, reason: NavigateReason = .address_bar,
method: HttpClient.Method = .GET, method: HttpClient.Method = .GET,
body: ?[]const u8 = null, body: ?[]const u8 = null,
header: ?[:0]const u8 = null,
}; };
fn timestamp() u32 { fn timestamp() u32 {

View File

@@ -12,6 +12,7 @@ pub const LookupOpts = struct {
origin_uri: ?*const Uri = null, origin_uri: ?*const Uri = null,
is_http: bool, is_http: bool,
is_navigation: bool = true, is_navigation: bool = true,
prefix: ?[]const u8 = null,
}; };
pub const Jar = struct { pub const Jar = struct {
@@ -91,10 +92,15 @@ pub const Jar = struct {
var first = true; var first = true;
for (self.cookies.items) |*cookie| { 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! // we have a match!
if (first) { if (first) {
if (opts.prefix) |prefix| {
try writer.writeAll(prefix);
}
first = false; first = false;
} else { } else {
try writer.writeAll("; "); try writer.writeAll("; ");

View File

@@ -375,7 +375,6 @@ pub const XMLHttpRequest = struct {
.url = self.url.?, .url = self.url.?,
.method = self.method, .method = self.method,
.body = self.request_body, .body = self.request_body,
.content_type = "Content-Type: text/plain; charset=UTF-8", // @newhttp TODO
.cookie = page.requestCookie(.{}), .cookie = page.requestCookie(.{}),
.start_callback = httpStartCallback, .start_callback = httpStartCallback,
.header_callback = httpHeaderCallback, .header_callback = httpHeaderCallback,

View File

@@ -263,24 +263,21 @@ fn makeRequest(self: *Client, handle: *Handle, req: Request) !void {
var header_list = conn.commonHeaders(); var header_list = conn.commonHeaders();
errdefer c.curl_slist_free_all(header_list); errdefer c.curl_slist_free_all(header_list);
if (req.content_type) |ct| { if (req.header) |hdr| {
header_list = c.curl_slist_append(header_list, ct); header_list = c.curl_slist_append(header_list, hdr);
} }
{ {
const COOKIE_HEADER = "Cookie: ";
const aa = self.arena.allocator(); const aa = self.arena.allocator();
defer _ = self.arena.reset(.{ .retain_with_limit = 2048 });
var arr: std.ArrayListUnmanaged(u8) = .{}; var arr: std.ArrayListUnmanaged(u8) = .{};
try arr.appendSlice(aa, COOKIE_HEADER);
try req.cookie.forRequest(&uri, arr.writer(aa)); 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 try arr.append(aa, 0); //null terminate
// copies the value // copies the value
header_list = c.curl_slist_append(header_list, @ptrCast(arr.items.ptr)); 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_http = self.is_http,
.is_navigation = self.is_navigation, .is_navigation = self.is_navigation,
.origin_uri = self.origin, .origin_uri = self.origin,
.prefix = "Cookie: ",
}); });
} }
}; };
@@ -493,7 +491,7 @@ pub const Request = struct {
method: Method, method: Method,
url: [:0]const u8, url: [:0]const u8,
body: ?[]const u8 = null, body: ?[]const u8 = null,
content_type: ?[:0]const u8 = null, header: ?[:0]const u8 = null,
cookie: RequestCookie, cookie: RequestCookie,
// arbitrary data that can be associated with this request // arbitrary data that can be associated with this request