From 4f631d72733c4092eee80f2613759cc2cce78bea Mon Sep 17 00:00:00 2001 From: Martin Nowak Date: Thu, 25 Sep 2014 01:31:34 +0200 Subject: [PATCH] fix parsing of mongodb url and passwords with '/' --- source/vibe/db/mongo/connection.d | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/source/vibe/db/mongo/connection.d b/source/vibe/db/mongo/connection.d index 5a9b53a9d1..6401a95de5 100644 --- a/source/vibe/db/mongo/connection.d +++ b/source/vibe/db/mongo/connection.d @@ -480,9 +480,7 @@ bool parseMongoDBUrl(out MongoClientSettings cfg, string url) // Reslice to get rid of 'mongodb://' tmpUrl = tmpUrl[10..$]; - auto slashIndex = tmpUrl.indexOf("/"); - if( slashIndex == -1 ) slashIndex = tmpUrl.length; - auto authIndex = tmpUrl[0 .. slashIndex].indexOf('@'); + auto authIndex = tmpUrl.indexOf('@'); sizediff_t hostIndex = 0; // Start of the host portion of the URL. // Parse out the username and optional password. @@ -510,6 +508,10 @@ bool parseMongoDBUrl(out MongoClientSettings cfg, string url) cfg.digest = MongoClientSettings.makeDigest(cfg.username, password); } + auto slashIndex = tmpUrl[hostIndex..$].indexOf("/"); + if( slashIndex == -1 ) slashIndex = tmpUrl.length; + else slashIndex += hostIndex; + // Parse the hosts section. try { @@ -713,6 +715,18 @@ unittest assert(! (parseMongoDBUrl(cfg, "mongodb://@localhost"))); assert(! (parseMongoDBUrl(cfg, "mongodb://:thepass@localhost"))); assert(! (parseMongoDBUrl(cfg, "mongodb://:badport/"))); + + 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); + 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); } private enum OpCode : int {