Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions dali/util/uri.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ URI URI::Parse(std::string uri, URI::ParseOpts opts) {
if (*p == '/' && *(p + 1) == '/') {
p += 2;
parsed.authority_start_ = p - p_start;
while (*p != '\0' && *p != '/') {
while (*p != '\0' && *p != '/' && *p != '?' && *p != '#') {
if (!allowed_char(*p)) {
parsed.valid_ = false;
parsed.err_msg_ = "Invalid character found (" + display_char(*p) + ") in authority";
Expand All @@ -123,7 +123,7 @@ URI URI::Parse(std::string uri, URI::ParseOpts opts) {

// Path
parsed.path_start_ = p - p_start;
while (*p != '\0' && *p != '?') {
while (*p != '\0' && *p != '?' && *p != '#') {
if (!allowed_char(*p) && !allow_non_escaped) {
parsed.valid_ = false;
parsed.err_msg_ = "Invalid character found (" + display_char(*p) + ") in path";
Expand All @@ -135,20 +135,22 @@ URI URI::Parse(std::string uri, URI::ParseOpts opts) {
if (*p == '\0')
return parsed;

// Query
p++;
parsed.query_start_ = p - p_start;
while (*p != '\0' && *p != '#') {
if (!allowed_char(*p) && !allow_non_escaped) {
parsed.valid_ = false;
parsed.err_msg_ = "Invalid character found (" + display_char(*p) + ") in query";
return parsed;
}
if (*p == '?') {
// Query
p++;
parsed.query_start_ = p - p_start;
while (*p != '\0' && *p != '#') {
if (!allowed_char(*p) && !allow_non_escaped) {
parsed.valid_ = false;
parsed.err_msg_ = "Invalid character found (" + display_char(*p) + ") in query";
return parsed;
}
p++;
}
parsed.query_end_ = p - p_start;
if (*p == '\0')
return parsed;
}
parsed.query_end_ = p - p_start;
if (*p == '\0')
return parsed;

// Fragment
p++;
Expand Down
40 changes: 40 additions & 0 deletions dali/util/uri_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,46 @@ TEST(URI, Parse_7) {
EXPECT_EQ("", uri.fragment());
}

TEST(URI, Parse_FragmentWithoutQuery) {
auto uri = URI::Parse("s3://bucket/path#fragment");
EXPECT_EQ("s3", uri.scheme());
EXPECT_EQ("bucket", uri.authority());
EXPECT_EQ("/path", uri.path());
EXPECT_EQ("", uri.query());
EXPECT_EQ("/path", uri.path_and_query());
EXPECT_EQ("fragment", uri.fragment());
}

TEST(URI, Parse_EmptyQueryWithFragment) {
auto uri = URI::Parse("s3://bucket/path?#fragment");
EXPECT_EQ("s3", uri.scheme());
EXPECT_EQ("bucket", uri.authority());
EXPECT_EQ("/path", uri.path());
EXPECT_EQ("", uri.query());
EXPECT_EQ("/path?", uri.path_and_query());
EXPECT_EQ("fragment", uri.fragment());
}

TEST(URI, Parse_EmptyPathWithFragment) {
auto uri = URI::Parse("s3://bucket#fragment");
EXPECT_EQ("s3", uri.scheme());
EXPECT_EQ("bucket", uri.authority());
EXPECT_EQ("", uri.path());
EXPECT_EQ("", uri.query());
EXPECT_EQ("", uri.path_and_query());
EXPECT_EQ("fragment", uri.fragment());
}

TEST(URI, Parse_EmptyPathWithQueryAndFragment) {
auto uri = URI::Parse("s3://bucket?query#fragment");
EXPECT_EQ("s3", uri.scheme());
EXPECT_EQ("bucket", uri.authority());
EXPECT_EQ("", uri.path());
EXPECT_EQ("query", uri.query());
EXPECT_EQ("?query", uri.path_and_query());
EXPECT_EQ("fragment", uri.fragment());
}
Comment on lines +97 to +135

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 [Nit] All four new test cases exercise s3:// URIs (with authority). The path-loop # fix also fires on authority-less URIs such as urn:path#frag or mailto:user@example.com#section, but there is no test for that shape. Adding one test like URI::Parse("urn:path#frag") would give confidence the path-only code path is covered.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


TEST(URI, Parse_Error1) {
auto uri = URI::Parse(
"telnet://192. 0.2.16:80/");
Expand Down