Skip to content

Commit

Permalink
fixed; allow # in user:pass
Browse files Browse the repository at this point in the history
closes #3
  • Loading branch information
aheckmann committed Feb 17, 2013
1 parent e80f1a3 commit 2c25e90
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 22 deletions.
86 changes: 67 additions & 19 deletions lib/index.js
Expand Up @@ -9,7 +9,6 @@
* Module dependencies
*/

var url = require('url');
var qs = require('querystring');

/**
Expand Down Expand Up @@ -46,33 +45,25 @@ module.exports = exports = function muri (str) {
var db;

uris.split(',').forEach(function (uri) {
if (!/^mongodb:\/\//.test(uri)) {
uri = 'mongodb://' + uri;
}

var o = url.parse(uri);
var o = parse(uri);

if (o.hostname) {
if (o.host) {
ret.hosts.push({
host: o.hostname
, port: parseInt(o.port || DEFAULT_PORT, 10)
host: o.host
, port: parseInt(o.port, 10)
})

if (!db && o.pathname) {
db = o.pathname.replace(/^\//, '');
}
} else {
var domain = /(.+\.sock)(\/?.*)$/.exec(o.pathname);
if (domain && domain[1]) {
ret.hosts.push({ ipc: domain[1] });
if (!db && o.db) {
db = o.db;
}
} else if (o.ipc) {
ret.hosts.push({ ipc: o.ipc });
}

if (o.auth) {
var auth = o.auth.split(':');
ret.auth = {
user: auth[0]
, pass: auth[1]
user: o.auth.user
, pass: o.auth.pass
}
}
})
Expand Down Expand Up @@ -178,6 +169,63 @@ function readPref (val) {
return hasKeys && ret;
}

var ipcRgx = /\.sock/;

function parse (uriString) {
// do not use require('url').parse b/c it can't handle # in username or pwd
// mongo uris are strange

var uri = uriString;
var ret = {};
var parts;
var auth;
var ipcs;

// skip protocol
uri = uri.replace(/^mongodb:\/\//, '');

// auth
if (/@/.test(uri)) {
parts = uri.split(/@/);
auth = parts[0];
uri = parts[1];

parts = auth.split(':');
ret.auth = {};
ret.auth.user = parts[0];
ret.auth.pass = parts[1];
}

// unix domain sockets
if (ipcRgx.test(uri)) {
ipcs = uri.split(ipcRgx);
ret.ipc = ipcs[0] + '.sock';

// included a database?
if (ipcs[1]) {
// strip leading / from database name
ipcs[1] = ipcs[1].replace(/^\//, '');

if (ipcs[1]) {
ret.db = ipcs[1];
}
}

return ret;
}

// database name
parts = uri.split('/');
if (parts[1]) ret.db = parts[1];

// host:port
parts = parts[0].split(':');
ret.host = parts[0];
ret.port = parts[1] || DEFAULT_PORT;

return ret;
}

/**
* Version
*/
Expand Down
24 changes: 21 additions & 3 deletions test/index.js
Expand Up @@ -29,6 +29,24 @@ describe('muri', function(){
assert.equal('password', val.auth.pass);
done();
})

it('handles # in the username', function(done){
var uri = 'mongodb://us#er:password@local:27017';
var val = muri(uri);
assert.ok(val.auth);
assert.equal('us#er', val.auth.user);
assert.equal('password', val.auth.pass);
done();
})

it('handles # in the password', function(done){
var uri = 'mongodb://user:pa#ssword@local:27017';
var val = muri(uri);
assert.ok(val.auth);
assert.equal('user', val.auth.user);
assert.equal('pa#ssword', val.auth.pass);
done();
})
})

describe('host', function(){
Expand Down Expand Up @@ -267,12 +285,12 @@ describe('muri', function(){
})

it('all together now', function(done){
var uri = 'mongodb://user:pass@local,remote:27018,japan:27019/neatdb'
var uri = 'mongodb://u#ser:pas#s@local,remote:27018,japan:27019/neatdb'
uri += '?replicaSet=myreplset&journal=true&w=2&wtimeoutMS=50'
var val = muri(uri);

assert.equal('user', val.auth.user);
assert.equal('pass', val.auth.pass);
assert.equal('u#ser', val.auth.user);
assert.equal('pas#s', val.auth.pass);
assert.equal('neatdb', val.db);
assert.equal(3, val.hosts.length);
assert.equal('local', val.hosts[0].host);
Expand Down

0 comments on commit 2c25e90

Please sign in to comment.