diff --git a/src/http.cc b/src/http.cc index 8af3dfa..19f5bdf 100644 --- a/src/http.cc +++ b/src/http.cc @@ -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(std::stoi(portstr)); host = host.substr(0, host.rfind(":" + portstr)); diff --git a/tests/tests.cc b/tests/tests.cc index e06e1b3..57d5bad 100644 --- a/tests/tests.cc +++ b/tests/tests.cc @@ -698,6 +698,36 @@ class Tests { throw; } } + + void TestBaseUrl() { + std::cout << "TestBaseUrl()" << std::endl; + std::vector> 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*/[]) { @@ -755,6 +785,7 @@ int main(int /*argc*/, char* /*argv*/[]) { tests.RemoveObjects(); tests.SelectObjectContent(); tests.ListenBucketNotification(); + tests.TestBaseUrl(); return EXIT_SUCCESS; }