Skip to content

Commit

Permalink
Parsing URI better
Browse files Browse the repository at this point in the history
  • Loading branch information
willhuang85 committed Feb 28, 2015
1 parent 5d9f15d commit 05cf652
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,52 @@ module.exports = function GridFSStore (globalOpts) {
database: globalOpts.dbname
});
} else {
var uriandbucket = globalOpts.uri.trim();
globalOpts.uri = uriandbucket.substring(0, uriandbucket.lastIndexOf('.'));
globalOpts.bucket = uriandbucket.substring(uriandbucket.lastIndexOf('.')+1);
//Thanks to Java's Mongodb driver ConnectionString.class
var serverPart;
var nsPart;
var userName = '';
var password = '';
var database;
var bucket;
var prefix = 'mongodb://';

var unprefixeduri = globalOpts.uri.trim().substring(prefix.length);
var idx = unprefixeduri.lastIndexOf('/');
if (idx < 0) {
serverPart = unprefixeduri;
} else {
serverPart = unprefixeduri.substring(0, idx);
nsPart = unprefixeduri.substring(idx + 1);
}

idx = serverPart.indexOf('@');

if (idx > 0) {
var authPart = serverPart.substring(0, idx);
serverPart = serverPart.substring(idx + 1);
idx = authPart.indexOf(':');
if (idx == -1) {
userName = authPart;
} else {
userName = authPart.substring(0, idx);
password = authPart.substring(idx + 1);
}
}

if (nsPart && nsPart.trim() !== '') {
idx = nsPart.indexOf('.');
if (idx < 0) {
database = nsPart;
} else {
database = nsPart.substring(0, idx);
bucket = nsPart.substring(idx + 1);
}
}

bucket = bucket ? bucket : globalOpts.bucket;
database = database ? database : globalOpts.dbname;
globalOpts.uri = prefix+userName+(password ? ':' : '')+password+(password&&userName||userName ? '@' : '')+serverPart+'/'+database;

This comment has been minimized.

Copy link
@kvermeille

kvermeille Jun 25, 2015

Hey! This uri parse function doesn't work for simpler mongo uris such as mongodb://siteUserAdmin:password@127.0.0.1:27017/dbname

globalOpts.bucket = bucket;
}
}

Expand Down

0 comments on commit 05cf652

Please sign in to comment.