Skip to content

Commit d2bc5b7

Browse files
authored
Fix various URI resolution bugs (#2278)
Signed-off-by: Juan Cruz Viotti <[email protected]>
1 parent 120af1f commit d2bc5b7

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/core/uri/resolution.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,19 @@ auto URI::relative_to(const URI &base) -> URI & {
202202
return *this;
203203
}
204204

205-
// Hosts must match (but both can be null for URNs)
205+
// The full authority must match (but components can be null for URNs)
206+
if (this->userinfo_ != base.userinfo_) {
207+
return *this;
208+
}
209+
206210
if (this->host_ != base.host_) {
207211
return *this;
208212
}
209213

214+
if (this->port_ != base.port_) {
215+
return *this;
216+
}
217+
210218
// Special case: both URIs are exactly the same
211219
if (this->path_ == base.path_ && this->query_ == base.query_ &&
212220
this->fragment_ == base.fragment_) {

test/uri/uri_relative_to_test.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,27 @@ TEST(URI_relative_to, absolute_absolute_base_false_4) {
5858
EXPECT_EQ(uri.recompose(), "https://bar.com");
5959
}
6060

61+
TEST(URI_relative_to, absolute_absolute_base_false_different_ports) {
62+
const sourcemeta::core::URI base{"http://localhost:8000"};
63+
sourcemeta::core::URI uri{"http://localhost:9000/schemas/test.json"};
64+
uri.relative_to(base);
65+
EXPECT_EQ(uri.recompose(), "http://localhost:9000/schemas/test.json");
66+
}
67+
68+
TEST(URI_relative_to, absolute_absolute_base_false_different_userinfo) {
69+
const sourcemeta::core::URI base{"https://[email protected]/foo"};
70+
sourcemeta::core::URI uri{"https://[email protected]/foo/bar"};
71+
uri.relative_to(base);
72+
EXPECT_EQ(uri.recompose(), "https://[email protected]/foo/bar");
73+
}
74+
75+
TEST(URI_relative_to, absolute_absolute_base_false_userinfo_vs_none) {
76+
const sourcemeta::core::URI base{"https://example.com/foo"};
77+
sourcemeta::core::URI uri{"https://[email protected]/foo/bar"};
78+
uri.relative_to(base);
79+
EXPECT_EQ(uri.recompose(), "https://[email protected]/foo/bar");
80+
}
81+
6182
TEST(URI_relative_to, absolute_relative_1) {
6283
const sourcemeta::core::URI base{"https://www.example.com"};
6384
sourcemeta::core::URI uri{"foo"};

0 commit comments

Comments
 (0)