Skip to content

Commit

Permalink
Correctly canonicalize relative URIs that do not begin with /
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Nov 4, 2024
1 parent 3b644bf commit 07b0bcf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/uri/uri.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ static auto canonicalize_path(const std::string &path, const bool is_relative)

// Reconstruct the canonical path
std::string canonical_path;
std::string separator = is_relative ? "/" : "";
std::string separator = (is_relative && !has_leading_with_word) ? "/" : "";

for (const auto &seg : segments) {
canonical_path += separator + seg;
separator = "/";
Expand Down
8 changes: 7 additions & 1 deletion test/uri/uri_canonicalize_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ TEST(URI_canonicalize, example_relative_1) {
TEST(URI_canonicalize, example_relative_2) {
sourcemeta::jsontoolkit::URI uri{"./foo"};
uri.canonicalize();
EXPECT_EQ(uri.recompose(), "/foo");
EXPECT_EQ(uri.recompose(), "foo");
}

TEST(URI_canonicalize, example_relative_3) {
sourcemeta::jsontoolkit::URI uri{"foo"};
uri.canonicalize();
EXPECT_EQ(uri.recompose(), "foo");
}

TEST(URI_canonicalize, example_12) {
Expand Down

0 comments on commit 07b0bcf

Please sign in to comment.