Skip to content

Commit b576aec

Browse files
authored
Allow setting a relative path in a relative URI (#1793)
Fixes: #1794 Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent fb91cc0 commit b576aec

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/core/uri/uri.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,8 @@ auto URI::path(const std::string &path) -> URI & {
321321
}
322322

323323
const auto is_relative_path = path.starts_with(".");
324-
if (is_relative_path) {
325-
throw URIError{"You cannot set a relative path"};
324+
if (is_relative_path && this->is_absolute()) {
325+
throw URIError{"You cannot set a relative path to an absolute URI"};
326326
}
327327

328328
if (path == "/") {
@@ -341,8 +341,8 @@ auto URI::path(std::string &&path) -> URI & {
341341
}
342342

343343
const auto is_relative_path = path.starts_with(".");
344-
if (is_relative_path) {
345-
throw URIError{"You cannot set a relative path"};
344+
if (is_relative_path && this->is_absolute()) {
345+
throw URIError{"You cannot set a relative path to an absolute URI"};
346346
}
347347

348348
if (path == "/") {

test/uri/uri_path_test.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,16 @@ TEST(URI_path_setter_no_scheme, set_path_with_port) {
285285
EXPECT_EQ(path, "");
286286
}
287287

288+
TEST(URI_path_setter, lowercase_relative_path) {
289+
sourcemeta::core::URI uri{"../SERVER.JSON"};
290+
EXPECT_EQ(uri.recompose(), "../SERVER.JSON");
291+
uri.path("../server.json");
292+
EXPECT_EQ(uri.recompose(), "../server.json");
293+
const auto const_path{"../foo.json"};
294+
uri.path(const_path);
295+
EXPECT_EQ(uri.recompose(), "../foo.json");
296+
}
297+
288298
TEST(URI_path, append_path_without_path_from_root) {
289299
sourcemeta::core::URI uri{"http://example.com:8080"};
290300
uri.append_path("/test");

0 commit comments

Comments
 (0)