Add URL struct

Combine uri + rawuri into single struct.

Try to improve ownership around URIs and URI-like things.
 - cookie & request can take *const std.Uri
   (TODO: make them aware of the new URL struct?)
 - Location (web api) should own its URL (web api URL)
 - Window should own its Location

Most of these changes result in (a) a cleaner Page and (b) not having to carry
around 2 nullable objects (URI and rawuri).
This commit is contained in:
Karl Seguin
2025-04-09 18:14:53 +08:00
parent 8b296534a4
commit be75b5b237
14 changed files with 279 additions and 241 deletions

View File

@@ -44,25 +44,24 @@ pub const Interfaces = .{
// 2. The other way would bu to copy the `std.Uri` code to ahve a dedicated
// parser including the characters we want for the web API.
pub const URL = struct {
rawuri: []const u8,
uri: std.Uri,
search_params: URLSearchParams,
pub const mem_guarantied = true;
pub fn constructor(alloc: std.mem.Allocator, url: []const u8, base: ?[]const u8) !URL {
const raw = try std.mem.concat(alloc, u8, &[_][]const u8{ url, base orelse "" });
errdefer alloc.free(raw);
pub fn constructor(arena: std.mem.Allocator, url: []const u8, base: ?[]const u8) !URL {
const raw = try std.mem.concat(arena, u8, &[_][]const u8{ url, base orelse "" });
errdefer arena.free(raw);
const uri = std.Uri.parse(raw) catch {
return error.TypeError;
};
const uri = std.Uri.parse(raw) catch return error.TypeError;
return init(arena, uri);
}
pub fn init(arena: std.mem.Allocator, uri: std.Uri) !URL {
return .{
.rawuri = raw,
.uri = uri,
.search_params = try URLSearchParams.constructor(
alloc,
arena,
uriComponentNullStr(uri.query),
),
};
@@ -70,7 +69,6 @@ pub const URL = struct {
pub fn deinit(self: *URL, alloc: std.mem.Allocator) void {
self.search_params.deinit(alloc);
alloc.free(self.rawuri);
}
// the caller must free the returned string.