parseMongoDBUrl

Parses the given string as a mongodb URL. The URL must be in the form documented at http://www.mongodb.org/display/DOCS/Connections which is:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]]/[database][?options]

@safe
bool
parseMongoDBUrl

Return Value

Type: bool

true if the URL was successfully parsed. False if the URL can not be parsed.

If the URL is successfully parsed the MongoClientSettings instance will contain the parsed config. If the URL is not successfully parsed the information in the MongoClientSettings instance may be incomplete and should not be used.

Examples

parseMongoDBUrl parses minimal localhost URL with all defaults

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost"));
assert(cfg.hosts.length == 1);
assert(cfg.database == "");
assert(cfg.hosts[0].name == "localhost");
assert(cfg.hosts[0].port == 27017);
assert(cfg.replicaSet == "");
assert(cfg.safe == false);
assert(cfg.w == Bson.init);
assert(cfg.wTimeoutMS == long.init);
assert(cfg.fsync == false);
assert(cfg.journal == false);
assert(cfg.connectTimeoutMS == 10_000);
assert(cfg.socketTimeoutMS == long.init);
assert(cfg.ssl == bool.init);
assert(cfg.sslverifycertificate == true);

parseMongoDBUrl parses URL with username and password

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://fred:foobar@localhost"));
assert(cfg.username == "fred");
assert(cfg.digest == MongoClientSettings.makeDigest("fred", "foobar"));
assert(cfg.hosts.length == 1);
assert(cfg.database == "");
assert(cfg.hosts[0].name == "localhost");
assert(cfg.hosts[0].port == 27017);

parseMongoDBUrl parses URL with empty password and database

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://fred:@localhost/baz"));
assert(cfg.username == "fred");
assert(cfg.digest == MongoClientSettings.makeDigest("fred", ""));
assert(cfg.database == "baz");
assert(cfg.hosts.length == 1);
assert(cfg.hosts[0].name == "localhost");
assert(cfg.hosts[0].port == 27017);

parseMongoDBUrl parses multi-host URL with safe, w, wtimeoutMS, ssl options

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://host1,host2,host3/?safe=true&w=2&wtimeoutMS=2000&ssl=true&sslverifycertificate=false"));
assert(cfg.username == "");
assert(cfg.digest == "");
assert(cfg.database == "");
assert(cfg.hosts.length == 3);
assert(cfg.hosts[0].name == "host1");
assert(cfg.hosts[0].port == 27017);
assert(cfg.hosts[1].name == "host2");
assert(cfg.hosts[1].port == 27017);
assert(cfg.hosts[2].name == "host3");
assert(cfg.hosts[2].port == 27017);
assert(cfg.safe == true);
assert(cfg.w == Bson(2L));
assert(cfg.wTimeoutMS == 2000);
assert(cfg.ssl == true);
assert(cfg.sslverifycertificate == false);

parseMongoDBUrl parses full URL with credentials, multi-host with ports, database, and all options

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg,
			"mongodb://fred:flinstone@host1.example.com,host2.other.example.com:27108,host3:"
			~ "27019/mydb?journal=true;fsync=true;connectTimeoutms=1500;sockettimeoutMs=1000;w=majority"));
assert(cfg.username == "fred");
assert(cfg.digest == MongoClientSettings.makeDigest("fred", "flinstone"));
assert(cfg.database == "mydb");
assert(cfg.hosts.length == 3);
assert(cfg.hosts[0].name == "host1.example.com");
assert(cfg.hosts[0].port == 27017);
assert(cfg.hosts[1].name == "host2.other.example.com");
assert(cfg.hosts[1].port == 27108);
assert(cfg.hosts[2].name == "host3");
assert(cfg.hosts[2].port == 27019);
assert(cfg.fsync == true);
assert(cfg.journal == true);
assert(cfg.connectTimeoutMS == 1500);
assert(cfg.socketTimeoutMS == 1000);
assert(cfg.w == Bson("majority"));
assert(cfg.safe == true);

parseMongoDBUrl returns false for invalid URLs

MongoClientSettings cfg;

assert(!(parseMongoDBUrl(cfg, "localhost:27018")));
assert(!(parseMongoDBUrl(cfg, "http://blah")));
assert(!(parseMongoDBUrl(cfg, "mongodb://@localhost")));
assert(!(parseMongoDBUrl(cfg, "mongodb://:thepass@localhost")));
assert(!(parseMongoDBUrl(cfg, "mongodb://:badport/")));

parseMongoDBUrl parses URL with special characters in password

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://me:sl$ash/w0+rd@localhost"));
assert(cfg.digest == MongoClientSettings.makeDigest("me", "sl$ash/w0+rd"));
assert(cfg.hosts.length == 1);
assert(cfg.hosts[0].name == "localhost");
assert(cfg.hosts[0].port == 27017);

parseMongoDBUrl parses URL with special characters in password and database

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://me:sl$ash/w0+rd@localhost/mydb"));
assert(cfg.digest == MongoClientSettings.makeDigest("me", "sl$ash/w0+rd"));
assert(cfg.database == "mydb");
assert(cfg.hosts.length == 1);
assert(cfg.hosts[0].name == "localhost");
assert(cfg.hosts[0].port == 27017);

parseMongoDBUrl parses authMechanism=SCRAM-SHA-1

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://user:pass@localhost/?authMechanism=SCRAM-SHA-1"));
assert(cfg.authMechanism == MongoAuthMechanism.scramSHA1);

parseMongoDBUrl parses authMechanism=MONGODB-CR

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://user:pass@localhost/?authMechanism=MONGODB-CR"));
assert(cfg.authMechanism == MongoAuthMechanism.mongoDBCR);

parseMongoDBUrl parses authMechanism=MONGODB-X509

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://user:pass@localhost/?authMechanism=MONGODB-X509"));
assert(cfg.authMechanism == MongoAuthMechanism.mongoDBX509);

parseMongoDBUrl throws on invalid authMechanism

import std.exception : assertThrown;

MongoClientSettings cfg;

assertThrown!Exception(parseMongoDBUrl(cfg, "mongodb://user:pass@localhost/?authMechanism=INVALID"));

parseMongoDBUrl parses authSource overriding database for getAuthDatabase

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://user:pass@localhost/mydb?authSource=admin"));
assert(cfg.authSource == "admin");
assert(cfg.getAuthDatabase() == "admin");

parseMongoDBUrl parses appName option

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost/?appName=myApp"));
assert(cfg.appName == "myApp");

parseMongoDBUrl parses replicaSet option

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost/?replicaSet=rs0"));
assert(cfg.replicaSet == "rs0");

parseMongoDBUrl parses tls=true as ssl alias

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost/?tls=true"));
assert(cfg.ssl == true);

parseMongoDBUrl parses tls=false as ssl alias

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost/?tls=false"));
assert(cfg.ssl == false);

parseMongoDBUrl parses connectTimeoutMS

import core.time : msecs;

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost/?connectTimeoutMS=5000"));
assert(cfg.connectTimeout == 5000.msecs);
assert(cfg.connectTimeoutMS == 5000);

parseMongoDBUrl parses socketTimeoutMS

import core.time : msecs;

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost/?socketTimeoutMS=3000"));
assert(cfg.socketTimeout == 3000.msecs);
assert(cfg.socketTimeoutMS == 3000);

parseMongoDBUrl parses w=1 as integer write concern

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost/?w=1"));
assert(cfg.w == Bson(1L));

parseMongoDBUrl parses w=majority as string write concern

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost/?w=majority"));
assert(cfg.w == Bson("majority"));

parseMongoDBUrl sets safe=true when journal=true

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost/?journal=true"));
assert(cfg.journal == true);
assert(cfg.safe == true);

parseMongoDBUrl sets safe=true when fsync=true

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost/?fsync=true"));
assert(cfg.fsync == true);
assert(cfg.safe == true);

parseMongoDBUrl parses sslverifycertificate=false

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost/?sslverifycertificate=false"));
assert(cfg.sslverifycertificate == false);

parseMongoDBUrl parses multiple combined options

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost/?appName=test&replicaSet=rs1&ssl=true&authSource=admin"));
assert(cfg.appName == "test");
assert(cfg.replicaSet == "rs1");
assert(cfg.ssl == true);
assert(cfg.authSource == "admin");

parseMongoDBUrl parses URL with database and no options

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost/mydb"));
assert(cfg.database == "mydb");
assert(cfg.hosts[0].name == "localhost");
assert(cfg.hosts[0].port == 27017);

parseMongoDBUrl parses URL with database and trailing empty query string

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost/mydb?"));
assert(cfg.database == "mydb");

parseMongoDBUrl parses URL with no database but with options

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost/?safe=true"));
assert(cfg.database == "");
assert(cfg.safe == true);

parseMongoDBUrl parses explicit non-default port

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost:27018"));
assert(cfg.hosts[0].port == 27018);

parseMongoDBUrl parses minimum valid port 1

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost:1"));
assert(cfg.hosts[0].port == 1);

parseMongoDBUrl parses maximum valid port 65535

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost:65535"));
assert(cfg.hosts[0].port == 65535);

parseMongoDBUrl parses port 0

MongoClientSettings cfg;

assert(parseMongoDBUrl(cfg, "mongodb://localhost:0"));
assert(cfg.hosts[0].port == 0);

parseMongoDBUrl returns false for port exceeding ushort range

MongoClientSettings cfg;

assert(!parseMongoDBUrl(cfg, "mongodb://localhost:65536"));

parseMongoDBUrl returns false for non-numeric port

MongoClientSettings cfg;

assert(!parseMongoDBUrl(cfg, "mongodb://localhost:abc"));

getAuthDatabase returns authSource when set

auto cfg = new MongoClientSettings();
cfg.authSource = "external";
cfg.database = "mydb";
assert(cfg.getAuthDatabase() == "external");

getAuthDatabase returns database when authSource is empty

auto cfg = new MongoClientSettings();
cfg.database = "mydb";
assert(cfg.getAuthDatabase() == "mydb");

getAuthDatabase returns "admin" when both authSource and database are empty

auto cfg = new MongoClientSettings();
assert(cfg.getAuthDatabase() == "admin");

makeDigest produces deterministic output for same inputs

assert(MongoClientSettings.makeDigest("user", "pass") ==
       MongoClientSettings.makeDigest("user", "pass"));

makeDigest produces different output for different passwords

assert(MongoClientSettings.makeDigest("user", "pass1") !=
       MongoClientSettings.makeDigest("user", "pass2"));

makeDigest produces different output for different usernames

assert(MongoClientSettings.makeDigest("user1", "pass") !=
       MongoClientSettings.makeDigest("user2", "pass"));

connectTimeoutMS defaults to 10000 and round-trips through Duration

import core.time : msecs, seconds;

auto cfg = new MongoClientSettings();

assert(cfg.connectTimeoutMS == 10_000);
assert(cfg.connectTimeout == 10.seconds);

cfg.connectTimeoutMS = 2500;
assert(cfg.connectTimeout == 2500.msecs);
assert(cfg.connectTimeoutMS == 2500);

cfg.connectTimeout = 7.seconds;
assert(cfg.connectTimeoutMS == 7000);

socketTimeoutMS defaults to 0 and round-trips through Duration

import core.time : msecs;

auto cfg = new MongoClientSettings();

assert(cfg.socketTimeoutMS == 0);

cfg.socketTimeoutMS = 5000;
assert(cfg.socketTimeout == 5000.msecs);
assert(cfg.socketTimeoutMS == 5000);

authenticatePassword sets username and digest

auto cfg = new MongoClientSettings();

cfg.authenticatePassword("fred", "secret");
assert(cfg.username == "fred");
assert(cfg.digest == MongoClientSettings.makeDigest("fred", "secret"));

authenticateSSL sets ssl, username, PEM key file, and CA file

auto cfg = new MongoClientSettings();

cfg.authenticateSSL("CN=client", "/path/to/cert.pem", "/path/to/ca.pem");
assert(cfg.ssl == true);
assert(cfg.username == "CN=client");
assert(cfg.digest is null);
assert(cfg.sslPEMKeyFile == "/path/to/cert.pem");
assert(cfg.sslCAFile == "/path/to/ca.pem");

authenticateSSL without CA file sets sslCAFile to null

auto cfg = new MongoClientSettings();

cfg.authenticateSSL("CN=client2", "/path/to/cert2.pem");
assert(cfg.sslCAFile is null);

Meta