Strip trailing slash on URIs

`/blogs/` is now the same as `/blogs`.

Root path `/` is a special case.
This commit is contained in:
Bob Farrell 2024-11-13 20:57:25 +00:00
parent c8ae44b508
commit c5acaedb73
2 changed files with 22 additions and 4 deletions

View File

@ -7,8 +7,8 @@
.hash = "1220d0e8734628fd910a73146e804d10a3269e3e7d065de6bb0e3e88d5ba234eb163",
},
.zmpl = .{
.url = "https://github.com/jetzig-framework/zmpl/archive/517b453f333c328ff5ebafa49020b1040e54e880.tar.gz",
.hash = "1220798a4647e3b0766aad653830a2601e11c567ba6bfe83e526eb91d04a6c45f7d8",
.url = "https://github.com/jetzig-framework/zmpl/archive/369193322cc572197b57a1a2cebd85b317bf92c4.tar.gz",
.hash = "12205982592aa38f37bba8d7410f62263827a49b350c1029a2eb79e8cc12b7247e9c",
},
.jetkv = .{
.url = "https://github.com/jetzig-framework/jetkv/archive/2b1130a48979ea2871c8cf6ca89c38b1e7062839.tar.gz",

View File

@ -92,9 +92,9 @@ fn getBasePath(path: []const u8) []const u8 {
return path[0..query_index];
}
} else if (std.mem.lastIndexOfScalar(u8, path, '.')) |extension_index| {
return path[0..extension_index];
return if (std.mem.eql(u8, path, "/")) path else std.mem.trimRight(u8, path[0..extension_index], "/");
} else {
return path;
return if (std.mem.eql(u8, path, "/")) path else std.mem.trimRight(u8, path, "/");
}
}
@ -181,6 +181,18 @@ test ".base_path (without extension, without query)" {
try std.testing.expectEqualStrings("/foo/bar/baz", path.base_path);
}
test ".base_path (with trailing slash)" {
const path = Self.init("/foo/bar/");
try std.testing.expectEqualStrings("/foo/bar", path.base_path);
}
test ".base_path (root path)" {
const path = Self.init("/");
try std.testing.expectEqualStrings("/", path.base_path);
}
test ".directory (with extension, with query)" {
const path = Self.init("/foo/bar/baz.html?qux=quux&corge=grault");
@ -229,6 +241,12 @@ test ".resource_id (without extension, without query, without base path)" {
try std.testing.expectEqualStrings("baz", path.resource_id);
}
test ".resource_id (with trailing slash)" {
const path = Self.init("/foo/bar/");
try std.testing.expectEqualStrings("bar", path.resource_id);
}
test ".extension (with query)" {
const path = Self.init("/foo/bar/baz.html?qux=quux&corge=grault");