Skip to content

Fix network-path reference stitching #949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
243 changes: 117 additions & 126 deletions src/url.zig
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,6 @@ pub const URL = struct {
return WebApiURL.init(allocator, self.uri);
}

const StitchOpts = struct {
alloc: AllocWhen = .always,
null_terminated: bool = false,

const AllocWhen = enum {
always,
if_needed,
};
};

/// Properly stitches two URL fragments together.
///
/// For URLs with a path, it will replace the last entry with the src.
Expand All @@ -106,25 +96,24 @@ pub const URL = struct {
comptime opts: StitchOpts,
) !StitchReturn(opts) {
if (base.len == 0 or isComleteHTTPUrl(path)) {
if (comptime opts.null_terminated) {
return allocator.dupeZ(u8, path);
}

if (opts.alloc == .always) {
return allocator.dupe(u8, path);
}
return path;
return simpleStitch(allocator, path, opts);
}

if (path.len == 0) {
if (comptime opts.null_terminated) {
return allocator.dupeZ(u8, base);
}
return simpleStitch(allocator, base, opts);
}

if (std.mem.startsWith(u8, path, "//")) {
// network-path reference
const index = std.mem.indexOfScalar(u8, base, ':') orelse {
return simpleStitch(allocator, path, opts);
};

if (opts.alloc == .always) {
return allocator.dupe(u8, base);
const protocol = base[0..index];
if (comptime opts.null_terminated) {
return std.fmt.allocPrintZ(allocator, "{s}:{s}", .{ protocol, path });
}
return base;
return std.fmt.allocPrint(allocator, "{s}:{s}", .{ protocol, path });
}

// Quick hack because domains have to be at least 3 characters.
Expand Down Expand Up @@ -191,10 +180,6 @@ pub const URL = struct {
return out[0..out_i];
}

fn StitchReturn(comptime opts: StitchOpts) type {
return if (opts.null_terminated) [:0]const u8 else []const u8;
}

pub fn concatQueryString(arena: Allocator, url: []const u8, query_string: []const u8) ![]const u8 {
std.debug.assert(url.len != 0);

Expand All @@ -221,11 +206,33 @@ pub const URL = struct {
}
};

fn isComleteHTTPUrl(url: []const u8) bool {
if (std.mem.startsWith(u8, url, "://")) {
return true;
const StitchOpts = struct {
alloc: AllocWhen = .always,
null_terminated: bool = false,

const AllocWhen = enum {
always,
if_needed,
};
};

fn StitchReturn(comptime opts: StitchOpts) type {
return if (opts.null_terminated) [:0]const u8 else []const u8;
}

fn simpleStitch(allocator: Allocator, url: []const u8, comptime opts: StitchOpts) !StitchReturn(opts) {
if (comptime opts.null_terminated) {
return allocator.dupeZ(u8, url);
}

if (comptime opts.alloc == .always) {
return allocator.dupe(u8, url);
}

return url;
}

fn isComleteHTTPUrl(url: []const u8) bool {
if (url.len < 8) {
return false;
}
Expand All @@ -243,8 +250,6 @@ fn isComleteHTTPUrl(url: []const u8) bool {

const testing = @import("testing.zig");
test "URL: isComleteHTTPUrl" {
try testing.expectEqual(true, isComleteHTTPUrl("://lightpanda.io"));
try testing.expectEqual(true, isComleteHTTPUrl("://lightpanda.io/about"));
try testing.expectEqual(true, isComleteHTTPUrl("http://lightpanda.io/about"));
try testing.expectEqual(true, isComleteHTTPUrl("HttP://lightpanda.io/about"));
try testing.expectEqual(true, isComleteHTTPUrl("httpS://lightpanda.io/about"));
Expand All @@ -253,6 +258,8 @@ test "URL: isComleteHTTPUrl" {
try testing.expectEqual(false, isComleteHTTPUrl("/lightpanda.io"));
try testing.expectEqual(false, isComleteHTTPUrl("../../about"));
try testing.expectEqual(false, isComleteHTTPUrl("about"));
try testing.expectEqual(false, isComleteHTTPUrl("//lightpanda.io"));
try testing.expectEqual(false, isComleteHTTPUrl("//lightpanda.io/about"));
}

test "URL: resolve size" {
Expand Down Expand Up @@ -280,99 +287,83 @@ test "URL: stitch" {
expected: []const u8,
};

const cases = [_]Case{
.{
.base = "https://lightpanda.io/xyz/abc/123",
.path = "something.js",
.expected = "https://lightpanda.io/xyz/abc/something.js",
},
.{
.base = "https://lightpanda.io/xyz/abc/123",
.path = "/something.js",
.expected = "https://lightpanda.io/something.js",
},
.{
.base = "https://lightpanda.io/",
.path = "something.js",
.expected = "https://lightpanda.io/something.js",
},
.{
.base = "https://lightpanda.io/",
.path = "/something.js",
.expected = "https://lightpanda.io/something.js",
},
.{
.base = "https://lightpanda.io",
.path = "something.js",
.expected = "https://lightpanda.io/something.js",
},
.{
.base = "https://lightpanda.io",
.path = "abc/something.js",
.expected = "https://lightpanda.io/abc/something.js",
},
.{
.base = "https://lightpanda.io/nested",
.path = "abc/something.js",
.expected = "https://lightpanda.io/abc/something.js",
},
.{
.base = "https://lightpanda.io/nested/",
.path = "abc/something.js",
.expected = "https://lightpanda.io/nested/abc/something.js",
},
.{
.base = "https://lightpanda.io/nested/",
.path = "/abc/something.js",
.expected = "https://lightpanda.io/abc/something.js",
},
.{
.base = "https://lightpanda.io/nested/",
.path = "http://www.github.com/lightpanda-io/",
.expected = "http://www.github.com/lightpanda-io/",
},
.{
.base = "https://lightpanda.io/nested/",
.path = "",
.expected = "https://lightpanda.io/nested/",
},
.{
.base = "https://lightpanda.io/abc/aaa",
.path = "./hello/./world",
.expected = "https://lightpanda.io/abc/hello/world",
},
.{
.base = "https://lightpanda.io/abc/aaa/",
.path = "../hello",
.expected = "https://lightpanda.io/abc/hello",
},
.{
.base = "https://lightpanda.io/abc/aaa",
.path = "../hello",
.expected = "https://lightpanda.io/hello",
},
.{
.base = "https://lightpanda.io/abc/aaa/",
.path = "./.././.././hello",
.expected = "https://lightpanda.io/hello",
},
.{
.base = "some/page",
.path = "hello",
.expected = "some/hello",
},
.{
.base = "some/page/",
.path = "hello",
.expected = "some/page/hello",
},

.{
.base = "some/page/other",
.path = ".././hello",
.expected = "some/hello",
},
};
const cases = [_]Case{ .{
.base = "https://lightpanda.io/xyz/abc/123",
.path = "something.js",
.expected = "https://lightpanda.io/xyz/abc/something.js",
}, .{
.base = "https://lightpanda.io/xyz/abc/123",
.path = "/something.js",
.expected = "https://lightpanda.io/something.js",
}, .{
.base = "https://lightpanda.io/",
.path = "something.js",
.expected = "https://lightpanda.io/something.js",
}, .{
.base = "https://lightpanda.io/",
.path = "/something.js",
.expected = "https://lightpanda.io/something.js",
}, .{
.base = "https://lightpanda.io",
.path = "something.js",
.expected = "https://lightpanda.io/something.js",
}, .{
.base = "https://lightpanda.io",
.path = "abc/something.js",
.expected = "https://lightpanda.io/abc/something.js",
}, .{
.base = "https://lightpanda.io/nested",
.path = "abc/something.js",
.expected = "https://lightpanda.io/abc/something.js",
}, .{
.base = "https://lightpanda.io/nested/",
.path = "abc/something.js",
.expected = "https://lightpanda.io/nested/abc/something.js",
}, .{
.base = "https://lightpanda.io/nested/",
.path = "/abc/something.js",
.expected = "https://lightpanda.io/abc/something.js",
}, .{
.base = "https://lightpanda.io/nested/",
.path = "http://www.github.com/lightpanda-io/",
.expected = "http://www.github.com/lightpanda-io/",
}, .{
.base = "https://lightpanda.io/nested/",
.path = "",
.expected = "https://lightpanda.io/nested/",
}, .{
.base = "https://lightpanda.io/abc/aaa",
.path = "./hello/./world",
.expected = "https://lightpanda.io/abc/hello/world",
}, .{
.base = "https://lightpanda.io/abc/aaa/",
.path = "../hello",
.expected = "https://lightpanda.io/abc/hello",
}, .{
.base = "https://lightpanda.io/abc/aaa",
.path = "../hello",
.expected = "https://lightpanda.io/hello",
}, .{
.base = "https://lightpanda.io/abc/aaa/",
.path = "./.././.././hello",
.expected = "https://lightpanda.io/hello",
}, .{
.base = "some/page",
.path = "hello",
.expected = "some/hello",
}, .{
.base = "some/page/",
.path = "hello",
.expected = "some/page/hello",
}, .{
.base = "some/page/other",
.path = ".././hello",
.expected = "some/hello",
}, .{
.path = "//static.lightpanda.io/hello.js",
.base = "https://lightpanda.io/about/",
.expected = "https://static.lightpanda.io/hello.js",
} };

for (cases) |case| {
const result = try stitch(testing.arena_allocator, case.path, case.base, .{});
Expand Down