Skip to content

Commit

Permalink
attempt parsing of port only if colon was found
Browse files Browse the repository at this point in the history
without this change, urls like 5.my.s3.cluster were misinterpreted and
  the '5' parsed as the port of the url.

Bug:
* the getline() function puts the entire input string into the
  destination string, if the delimiter was not found.
  This leads to the entire host string being fed to the stoi()
  function to parse the port.

Fix:
* If the portstr is equal to the host string, no ':port' was encountered.
  => Skip the parsing of the port
  • Loading branch information
Florian Kaiser committed Jul 12, 2024
1 parent cd4ef14 commit 038f54f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/http.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Url Url::Parse(std::string value) {
while (std::getline(ss, portstr, ':')) {
}

if (!portstr.empty()) {
if (!portstr.empty() && portstr != host) {
try {
port = static_cast<unsigned>(std::stoi(portstr));
host = host.substr(0, host.rfind(":" + portstr));
Expand Down
31 changes: 31 additions & 0 deletions tests/tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,36 @@ class Tests {
throw;
}
}

void TestBaseUrl() {
std::cout << "TestBaseUrl()" << std::endl;
std::vector<std::tuple<std::string, std::string, bool, int>> tests = {
{"http://localhost:9000", "localhost", false, 9000},
{"http://localhost", "localhost", false, 0},
{"http://localhost:80", "localhost", false, 0},
{"https://localhost:9443", "localhost:9443", true, 9443},
{"https://localhost", "localhost", true, 0},
{"https://5.localhost.foo", "5.localhost.foo", true, 0},
{"https://5.localhost.foo:9000", "5.localhost.foo", true, 9000},
};
for (auto& [url, host, https, port] : tests) {
minio::s3::BaseUrl base_url(url);
if (base_url.host != host) {
throw std::runtime_error("BaseUrl.Host(): expected: " + host +
", got: " + base_url.host);
}
if (base_url.https != https) {
throw std::runtime_error("BaseUrl.Secure(): expected: " +
std::to_string(https) +
", got: " + std::to_string(base_url.https));
}
if (base_url.port != port) {
throw std::runtime_error("BaseUrl.Port(): expected: " +
std::to_string(port) +
", got: " + std::to_string(base_url.port));
}
}
}
}; // class Tests

int main(int /*argc*/, char* /*argv*/[]) {
Expand Down Expand Up @@ -755,6 +785,7 @@ int main(int /*argc*/, char* /*argv*/[]) {
tests.RemoveObjects();
tests.SelectObjectContent();
tests.ListenBucketNotification();
tests.TestBaseUrl();

return EXIT_SUCCESS;
}

0 comments on commit 038f54f

Please sign in to comment.