Compare commits

...

4 Commits

Author SHA1 Message Date
Karl Seguin
ca76575c2a Add handling for resolving special URLs
Takes inspiration from https://github.com/lightpanda-io/browser/pull/2030 and
fixes https://github.com/lightpanda-io/browser/issues/1994

A url like http:/test gets special treatment. If the scheme, `http:` matches
the base scheme, then it's treated as relative to the base. If it doesn't match
the base scheme, then it's normalized to http://test, e.g. the path becomes
the host.
2026-03-30 17:07:10 +08:00
dinisimys2018
49cef740b2 fix(browser-url): added errdefer for joined result then get resolve error 2026-03-30 10:05:21 +03:00
dinisimys2018
4d812f1e74 fix(browser-url): added errdefer for path 2026-03-30 10:00:08 +03:00
dinisimys2018
3ee1da0ac1 fix resolve path with scheme 2026-03-30 00:34:34 +03:00

View File

@@ -77,6 +77,41 @@ pub fn resolve(allocator: Allocator, base: [:0]const u8, path: anytype, comptime
return processResolved(allocator, result, opts); return processResolved(allocator, result, opts);
} }
if (path.len >= 4) { // Minimum: "ws:x"
if (std.mem.indexOfScalar(u8, path[0..@min(path.len, 6)], ':')) |pos| {
// we know this isn't a complete URL, else the very first check in
// this function would have handled it.
const possible_special_protocol = path[0..pos];
const special_schemes = [_][]const u8{ "https", "http", "ws", "wss", "file", "ftp" };
for (special_schemes) |special_scheme| {
if (std.ascii.eqlIgnoreCase(possible_special_protocol, special_scheme)) {
const rest = path[pos + 1 ..];
// Check if base has the same scheme
const base_scheme_end = std.mem.indexOf(u8, base, "://") orelse 0;
if (base_scheme_end > 0 and std.ascii.eqlIgnoreCase(base[0..base_scheme_end], special_scheme)) {
// Same scheme - strip it and resolve rest as relative
return resolve(allocator, base, rest, opts);
}
// Different scheme - construct absolute URL
// Skip any leading slashes in rest
var rest_start: usize = 0;
while (rest_start < rest.len and (rest[rest_start] == '/' or rest[rest_start] == '\\')) {
rest_start += 1;
}
const rest_trimmed = rest[rest_start..];
// file: scheme needs empty host (triple slash)
const separator = if (std.mem.eql(u8, special_scheme, "file")) ":///" else "://";
const normalized = try std.mem.joinZ(allocator, "", &.{ special_scheme, separator, rest_trimmed });
return resolve(allocator, "", normalized, opts);
}
}
// Don't know what this is, just try to resolve it through our normal logic
}
}
const scheme_end = std.mem.indexOf(u8, base, "://"); const scheme_end = std.mem.indexOf(u8, base, "://");
const authority_start = if (scheme_end) |end| end + 3 else 0; const authority_start = if (scheme_end) |end| end + 3 else 0;
const path_start = std.mem.indexOfScalarPos(u8, base, authority_start, '/') orelse base.len; const path_start = std.mem.indexOfScalarPos(u8, base, authority_start, '/') orelse base.len;
@@ -1570,3 +1605,84 @@ test "URL: getOrigin" {
} }
} }
} }
test "URL: resolve path scheme" {
const Case = struct {
base: [:0]const u8,
path: [:0]const u8,
expected: [:0]const u8,
};
const cases = [_]Case{
.{
.base = "https://www.example.com/example",
.path = "https:/about",
.expected = "https://www.example.com/about",
},
.{
.base = "https://www.example.com/example",
.path = "https:about",
.expected = "https://www.example.com/about",
},
.{
.base = "https://www.example.com/example",
.path = "https://about",
.expected = "https://about",
},
.{
.base = "https://www.example.com/example",
.path = "http:about",
.expected = "http://about",
},
.{
.base = "https://www.example.com/example",
.path = "http:/about",
.expected = "http://about",
},
.{
.base = "https://www.example.com/example",
.path = "http://about",
.expected = "http://about",
},
.{
.base = "https://site/",
.path = "https://path",
.expected = "https://path",
},
.{
.base = "http://localhost/",
.path = "data:test",
.expected = "data:test",
},
.{
.base = "https://www.example.com/example",
.path = "ws://about",
.expected = "ws://about",
},
.{
.base = "https://www.example.com/example",
.path = "wss://about",
.expected = "wss://about",
},
.{
.base = "https://www.example.com/example",
.path = "ftp://about",
.expected = "ftp://about",
},
.{
.base = "https://www.example.com/example",
.path = "file://path/to/file",
.expected = "file://path/to/file",
},
.{
.base = "https://www.example.com/example",
.path = "file:/path/to/file",
.expected = "file:///path/to/file",
},
};
for (cases) |case| {
const result = try resolve(testing.arena_allocator, case.base, case.path, .{});
try testing.expectString(case.expected, result);
}
}