Skip to content

Commit 152d7d4

Browse files
committed
expand Headers interface
1 parent 54b3c8a commit 152d7d4

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

src/browser/fetch/Headers.zig

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const std = @import("std");
2020
const URL = @import("../../url.zig").URL;
2121
const Page = @import("../page.zig").Page;
2222

23+
const v8 = @import("v8");
24+
const Env = @import("../env.zig").Env;
25+
2326
// https://developer.mozilla.org/en-US/docs/Web/API/Headers
2427
const Headers = @This();
2528

@@ -93,29 +96,32 @@ pub fn constructor(_init: ?HeadersInit, page: *Page) !Headers {
9396
};
9497
}
9598

96-
pub fn clone(self: *Headers, allocator: std.mem.Allocator) !Headers {
99+
pub fn clone(self: *const Headers, allocator: std.mem.Allocator) !Headers {
97100
return Headers{
98101
.headers = try self.headers.clone(allocator),
99102
};
100103
}
101104

102-
pub fn _append(self: *Headers, name: []const u8, value: []const u8, page: *Page) !void {
103-
const arena = page.arena;
104-
105+
pub fn append(self: *Headers, name: []const u8, value: []const u8, allocator: std.mem.Allocator) !void {
105106
if (self.headers.getEntry(name)) |entry| {
106107
// If we found it, append the value.
107-
const new_value = try std.fmt.allocPrint(arena, "{s}, {s}", .{ entry.value_ptr.*, value });
108+
const new_value = try std.fmt.allocPrint(allocator, "{s}, {s}", .{ entry.value_ptr.*, value });
108109
entry.value_ptr.* = new_value;
109110
} else {
110111
// Otherwise, we should just put it in.
111112
try self.headers.putNoClobber(
112-
arena,
113-
try arena.dupe(u8, name),
114-
try arena.dupe(u8, value),
113+
allocator,
114+
try allocator.dupe(u8, name),
115+
try allocator.dupe(u8, value),
115116
);
116117
}
117118
}
118119

120+
pub fn _append(self: *Headers, name: []const u8, value: []const u8, page: *Page) !void {
121+
const arena = page.arena;
122+
try self.append(name, value, arena);
123+
}
124+
119125
pub fn _delete(self: *Headers, name: []const u8) void {
120126
_ = self.headers.remove(name);
121127
}
@@ -125,7 +131,26 @@ pub fn _delete(self: *Headers, name: []const u8) void {
125131
// 1. Sorted in lexicographical order.
126132
// 2. Duplicate header names should be combined.
127133

128-
// TODO: header for each
134+
pub fn _forEach(self: *Headers, callback_fn: Env.Function, this_arg: ?Env.JsObject) !void {
135+
var iter = self.headers.iterator();
136+
137+
if (this_arg) |this| {
138+
while (iter.next()) |entry| {
139+
try callback_fn.callWithThis(
140+
void,
141+
this,
142+
.{ entry.key_ptr.*, entry.value_ptr.*, self },
143+
);
144+
}
145+
} else {
146+
while (iter.next()) |entry| {
147+
try callback_fn.call(
148+
void,
149+
.{ entry.key_ptr.*, entry.value_ptr.*, self },
150+
);
151+
}
152+
}
153+
}
129154

130155
pub fn _get(self: *const Headers, name: []const u8, page: *Page) !?[]const u8 {
131156
const arena = page.arena;

0 commit comments

Comments
 (0)