simplify cloning of Req/Resp

This commit is contained in:
Muki Kiboigo
2025-09-08 07:54:25 -07:00
parent 03130a95d8
commit ebb590250f
4 changed files with 14 additions and 36 deletions

View File

@@ -107,12 +107,6 @@ pub fn constructor(_init: ?HeadersInit, page: *Page) !Headers {
};
}
pub fn clone(self: *const Headers, allocator: std.mem.Allocator) !Headers {
return Headers{
.headers = try self.headers.clone(allocator),
};
}
pub fn append(self: *Headers, name: []const u8, value: []const u8, allocator: std.mem.Allocator) !void {
const gop = try self.headers.getOrPut(allocator, name);

View File

@@ -173,23 +173,23 @@ pub fn get_url(self: *const Request) []const u8 {
return self.url;
}
pub fn _clone(self: *Request, page: *Page) !Request {
pub fn _clone(self: *Request) !Request {
// Not allowed to clone if the body was used.
if (self.body_used) {
return error.TypeError;
}
const arena = page.arena;
// OK to just return the same fields BECAUSE
// all of these fields are read-only and can't be modified.
return Request{
.body = if (self.body) |body| try arena.dupe(u8, body) else null,
.body = self.body,
.body_used = self.body_used,
.cache = self.cache,
.credentials = self.credentials,
.headers = try self.headers.clone(arena),
.headers = self.headers,
.method = self.method,
.integrity = try arena.dupe(u8, self.integrity),
.url = try arena.dupeZ(u8, self.url),
.integrity = self.integrity,
.url = self.url,
};
}

View File

@@ -109,21 +109,21 @@ pub fn get_url(self: *const Response) []const u8 {
return self.url;
}
pub fn _clone(self: *const Response, page: *Page) !Response {
pub fn _clone(self: *const Response) !Response {
if (self.body_used) {
return error.TypeError;
}
const arena = page.arena;
// OK to just return the same fields BECAUSE
// all of these fields are read-only and can't be modified.
return Response{
.body = try arena.dupe(u8, self.body),
.body = self.body,
.body_used = self.body_used,
.mime = if (self.mime) |mime| try mime.clone(arena) else null,
.headers = try self.headers.clone(arena),
.mime = self.mime,
.headers = self.headers,
.redirected = self.redirected,
.status = self.status,
.url = try arena.dupe(u8, self.url),
.url = self.url,
};
}

View File

@@ -290,22 +290,6 @@ pub const Mime = struct {
fn trimRight(s: []const u8) []const u8 {
return std.mem.trimRight(u8, s, &std.ascii.whitespace);
}
pub fn clone(self: *const Mime, allocator: Allocator) !Mime {
return Mime{
.content_type = blk: {
switch (self.content_type) {
.other => |data| break :blk ContentType{ .other = .{
.type = try allocator.dupe(u8, data.type),
.sub_type = try allocator.dupe(u8, data.sub_type),
} },
else => break :blk self.content_type,
}
},
.params = try allocator.dupe(u8, self.params),
.charset = if (self.charset) |charset| try allocator.dupeZ(u8, charset) else null,
};
}
};
const testing = @import("../testing.zig");