Skip to content

Commit 8c573ad

Browse files
committed
cleanup optional request headers
1 parent 4bef1d8 commit 8c573ad

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

src/browser/page.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ pub const Page = struct {
438438
.url = owned_url,
439439
.method = opts.method,
440440
.body = opts.body,
441+
.header = opts.header,
441442
.cookie = self.requestCookie(.{ .is_navigation = true }),
442443
.header_done_callback = pageHeaderDoneCallback,
443444
.data_callback = pageDataCallback,
@@ -1013,6 +1014,8 @@ pub const Page = struct {
10131014
if (std.ascii.eqlIgnoreCase(method, "post")) {
10141015
opts.method = .POST;
10151016
opts.body = buf.items;
1017+
// form_data.write currently only supports this encoding, so we know this has to be the content type
1018+
opts.header = "Content-Type: application/x-www-form-urlencoded";
10161019
} else {
10171020
action = try URL.concatQueryString(transfer_arena, action, buf.items);
10181021
}
@@ -1068,6 +1071,7 @@ pub const NavigateOpts = struct {
10681071
reason: NavigateReason = .address_bar,
10691072
method: HttpClient.Method = .GET,
10701073
body: ?[]const u8 = null,
1074+
header: ?[:0]const u8 = null,
10711075
};
10721076

10731077
fn timestamp() u32 {

src/browser/storage/cookie.zig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub const LookupOpts = struct {
1212
origin_uri: ?*const Uri = null,
1313
is_http: bool,
1414
is_navigation: bool = true,
15+
prefix: ?[]const u8 = null,
1516
};
1617

1718
pub const Jar = struct {
@@ -91,10 +92,15 @@ pub const Jar = struct {
9192

9293
var first = true;
9394
for (self.cookies.items) |*cookie| {
94-
if (!cookie.appliesTo(&target, same_site, opts.is_navigation, opts.is_http)) continue;
95+
if (!cookie.appliesTo(&target, same_site, opts.is_navigation, opts.is_http)) {
96+
continue;
97+
}
9598

9699
// we have a match!
97100
if (first) {
101+
if (opts.prefix) |prefix| {
102+
try writer.writeAll(prefix);
103+
}
98104
first = false;
99105
} else {
100106
try writer.writeAll("; ");

src/browser/xhr/xhr.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,6 @@ pub const XMLHttpRequest = struct {
375375
.url = self.url.?,
376376
.method = self.method,
377377
.body = self.request_body,
378-
.content_type = "Content-Type: text/plain; charset=UTF-8", // @newhttp TODO
379378
.cookie = page.requestCookie(.{}),
380379
.start_callback = httpStartCallback,
381380
.header_callback = httpHeaderCallback,

src/http/Client.zig

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,24 +263,21 @@ fn makeRequest(self: *Client, handle: *Handle, req: Request) !void {
263263
var header_list = conn.commonHeaders();
264264
errdefer c.curl_slist_free_all(header_list);
265265

266-
if (req.content_type) |ct| {
267-
header_list = c.curl_slist_append(header_list, ct);
266+
if (req.header) |hdr| {
267+
header_list = c.curl_slist_append(header_list, hdr);
268268
}
269269

270270
{
271-
const COOKIE_HEADER = "Cookie: ";
272271
const aa = self.arena.allocator();
273-
defer _ = self.arena.reset(.{ .retain_with_limit = 2048 });
274-
275272
var arr: std.ArrayListUnmanaged(u8) = .{};
276-
try arr.appendSlice(aa, COOKIE_HEADER);
277273
try req.cookie.forRequest(&uri, arr.writer(aa));
278274

279-
if (arr.items.len > COOKIE_HEADER.len) {
275+
if (arr.items.len > 0) {
280276
try arr.append(aa, 0); //null terminate
281277

282278
// copies the value
283279
header_list = c.curl_slist_append(header_list, @ptrCast(arr.items.ptr));
280+
defer _ = self.arena.reset(.{ .retain_with_limit = 2048 });
284281
}
285282
}
286283

@@ -485,6 +482,7 @@ pub const RequestCookie = struct {
485482
.is_http = self.is_http,
486483
.is_navigation = self.is_navigation,
487484
.origin_uri = self.origin,
485+
.prefix = "Cookie: ",
488486
});
489487
}
490488
};
@@ -493,7 +491,7 @@ pub const Request = struct {
493491
method: Method,
494492
url: [:0]const u8,
495493
body: ?[]const u8 = null,
496-
content_type: ?[:0]const u8 = null,
494+
header: ?[:0]const u8 = null,
497495
cookie: RequestCookie,
498496

499497
// arbitrary data that can be associated with this request

0 commit comments

Comments
 (0)