summaryrefslogtreecommitdiff
path: root/src/mongo/client/mongo_uri.cpp
diff options
context:
space:
mode:
authorTyler Kaye <tyler.kaye@mongodb.com>2018-10-29 13:06:49 -0400
committerTyler Kaye <tyler.kaye@mongodb.com>2018-11-14 13:30:44 -0500
commitddcf9f0572755a456632d036744276a09baf5760 (patch)
treed417e74a1fa34b835916422aaf1ddcdf7879d58c /src/mongo/client/mongo_uri.cpp
parent106eba1584b61497c133896b5dab7a3cea49296d (diff)
downloadmongo-ddcf9f0572755a456632d036744276a09baf5760.tar.gz
SERVER-35212: Refactor shell code to enable default authentication database as admin
Diffstat (limited to 'src/mongo/client/mongo_uri.cpp')
-rw-r--r--src/mongo/client/mongo_uri.cpp54
1 files changed, 51 insertions, 3 deletions
diff --git a/src/mongo/client/mongo_uri.cpp b/src/mongo/client/mongo_uri.cpp
index 597933712df..c8b76e776a8 100644
--- a/src/mongo/client/mongo_uri.cpp
+++ b/src/mongo/client/mongo_uri.cpp
@@ -40,6 +40,7 @@
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/find_iterator.hpp>
#include <boost/algorithm/string/predicate.hpp>
+#include <boost/range/algorithm/count.hpp>
#include "mongo/base/status_with.h"
#include "mongo/bson/bsonobjbuilder.h"
@@ -56,8 +57,6 @@ using namespace std::literals::string_literals;
namespace {
constexpr std::array<char, 16> hexits{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
-const mongo::StringData kURIPrefix{"mongodb://"};
-const mongo::StringData kURISRVPrefix{"mongodb+srv://"};
// This vector must remain sorted. It is over pairs to facilitate a call to `std::includes` using
// a `std::map<std::string, std::string>` as the other parameter.
@@ -112,6 +111,10 @@ namespace mongo {
namespace {
+constexpr StringData kURIPrefix = "mongodb://"_sd;
+constexpr StringData kURISRVPrefix = "mongodb+srv://"_sd;
+constexpr StringData kDefaultMongoHost = "127.0.0.1:27017"_sd;
+
/**
* Helper Method for MongoURI::parse() to split a string into exactly 2 pieces by a char
* delimiter.
@@ -519,8 +522,53 @@ const boost::optional<std::string> MongoURI::getAppName() const {
const auto optIter = _options.find("appName");
if (optIter != end(_options)) {
return optIter->second;
+ }
+ return boost::none;
+}
+
+std::string MongoURI::canonicalizeURIAsString() const {
+ StringBuilder uri;
+ uri << kURIPrefix;
+ if (!_user.empty()) {
+ uri << uriEncode(_user);
+ if (!_password.empty()) {
+ uri << ":" << uriEncode(_password);
+ }
+ uri << "@";
+ }
+
+ const auto& servers = _connectString.getServers();
+ if (!servers.empty()) {
+ auto delimeter = "";
+ for (auto& hostAndPort : servers) {
+ if (boost::count(hostAndPort.host(), ':') > 1) {
+ uri << delimeter << "[" << uriEncode(hostAndPort.host()) << "]"
+ << ":" << uriEncode(std::to_string(hostAndPort.port()));
+ } else if (StringData(hostAndPort.host()).endsWith(".sock")) {
+ uri << delimeter << uriEncode(hostAndPort.host());
+ } else {
+ uri << delimeter << uriEncode(hostAndPort.host()) << ":"
+ << uriEncode(std::to_string(hostAndPort.port()));
+ }
+ delimeter = ",";
+ }
} else {
- return boost::none;
+ uri << kDefaultMongoHost;
+ }
+
+ uri << "/";
+ if (!_database.empty()) {
+ uri << uriEncode(_database);
+ }
+
+ if (!_options.empty()) {
+ auto delimeter = "";
+ uri << "?";
+ for (const auto& pair : _options) {
+ uri << delimeter << uriEncode(pair.first) << "=" << uriEncode(pair.second);
+ delimeter = "&";
+ }
}
+ return uri.str();
}
} // namespace mongo