Skip to content

Commit

Permalink
fix: address code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sjinks committed Nov 19, 2024
1 parent c8749e3 commit e59a4b1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
26 changes: 16 additions & 10 deletions ext/include/opentelemetry/ext/http/common/url_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ class UrlParser
path_ = std::string("/"); // use default path
if (is_port)
{
auto port_str = url_.substr(cpos, url_.length());
auto port_str = url_.substr(cpos);
port_ = GetPort(port_str);
}
else
{
host_ = url_.substr(cpos, url_.length());
host_ = url_.substr(cpos);
}
return;
}
Expand All @@ -119,21 +119,20 @@ class UrlParser
pos = url_.find('?', cpos);
if (pos == std::string::npos)
{
path_ = url_.substr(cpos, url_.length());
query_ = "";
path_ = url_.substr(cpos);
}
else
{
path_ = url_.substr(cpos, pos - cpos);
cpos = pos + 1;
query_ = url_.substr(cpos, url_.length());
query_ = url_.substr(cpos);
}
return;
}
path_ = std::string("/");
if (url_[cpos] == '?')
{
query_ = url_.substr(cpos, url_.length());
query_ = url_.substr(cpos);
}
}

Expand Down Expand Up @@ -207,16 +206,23 @@ class UrlDecoder
for (size_t pos = 0; pos < encoded.size(); pos++)
{
auto c = encoded[pos];
if (c == '%' && pos + 2 < encoded.size())
if (c == '%')
{
if (pos + 2 >= encoded.size())
{
return encoded;
}

int hi = hex_to_int(encoded[pos + 1]);
int lo = hex_to_int(encoded[pos + 2]);

if (hi != -1 && lo != -1)
if (hi == -1 || lo == -1)
{
c = static_cast<char>((hi << 4) | lo);
pos += 2;
return encoded;
}

c = static_cast<char>((hi << 4) | lo);
pos += 2;
}

result.push_back(c);
Expand Down
3 changes: 2 additions & 1 deletion ext/test/http/url_parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ TEST(UrlDecoderTests, BasicTests)
{"%2x", "%2x"},
{"%20", " "},
{"text%2", "text%2"},
};
{"%20test%zztest", "%20test%zztest"},
{"%20test%2", "%20test%2"}};

for (auto &testsample : testdata)
{
Expand Down

0 comments on commit e59a4b1

Please sign in to comment.