Skip to content

Commit

Permalink
Fix unterminated RegExp groups.
Browse files Browse the repository at this point in the history
  • Loading branch information
Southern committed Sep 19, 2012
1 parent 76e6b96 commit 559ca26
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
57 changes: 55 additions & 2 deletions lib/director/router.js
Expand Up @@ -108,6 +108,42 @@ function regifyString(str, params) {
return str;
}

//
// ### Fix unterminated RegExp groups in routes.
//
function terminator(routes, delimiter) {
var last = 0,
left = 0,
right = 0,
i;

for (i = 0; i < routes.length; i++) {
var chunk = routes[i];
if ((chunk.indexOf('(', last) > chunk.indexOf(')', last)) ||
(~chunk.indexOf('(', last) && !~chunk.indexOf(')', last)) ||
(!~chunk.indexOf('(', last) && ~chunk.indexOf(')', last))) {

left = chunk.indexOf('(', last);
right = chunk.indexOf(')', last);

if (!~right) {
var tmp = routes.slice(0, (i || 1) + 1).join(delimiter);
routes = [tmp].concat(routes.slice((i || 1) + 1));
}

last = (right > left ? right : left) + 1;
i = 0;
}
else {
last = 0;
}
}

return routes;
}



//
// ### function Router (routes)
// #### @routes {Object} **Optional** Routing table for this instance.
Expand Down Expand Up @@ -217,7 +253,17 @@ Router.prototype.on = Router.prototype.route = function (method, path, route) {
});
}

this.insert(method, this.scope.concat(path.split(new RegExp(this.delimiter))), route);
//
// ### Split the route up by the delimiter.
//
path = path.split(new RegExp(this.delimiter));

//
// ### Fix unterminated groups. Fixes #59
//
path = terminator(path, this.delimiter);

this.insert(method, this.scope.concat(path), route);
};

//
Expand All @@ -234,7 +280,15 @@ Router.prototype.path = function (path, routesFn) {
path = path.source.replace(/\\\//ig, '/');
}

//
// ### Split the route up by the delimiter.
//
path = path.split(new RegExp(this.delimiter));

//
// ### Fix unterminated groups.
//
path = terminator(path, this.delimiter);
this.scope = this.scope.concat(path);

routesFn.call(this, this);
Expand Down Expand Up @@ -467,7 +521,6 @@ Router.prototype.traverse = function (method, path, routes, regexp, filter) {
//
current = exact = regexp + this.delimiter + r;


if (!this.strict) {
exact += '[' + this.delimiter + ']?';
}
Expand Down
10 changes: 9 additions & 1 deletion test/server/http/http-test.js
Expand Up @@ -42,6 +42,8 @@ vows.describe('director/http').addBatch({
router.path(/bar\/bazz\//, function () {
this.get(/(\w+)/, handlers.respondWithId);
});
router.get(/\/foo\/wild\/(.*)/, handlers.respondWithId);
router.get(/(\/v2)?\/somepath/, handlers.respondWithId);

helpers.createServer(router)
.listen(9090, this.callback);
Expand All @@ -50,7 +52,13 @@ vows.describe('director/http').addBatch({
"a request to foo/update/bark": assertBark('foo/update/bark'),
"a request to bar/bazz/bark": assertBark('bar/bazz/bark'),
"a request to foo/bar/bark?test=test": assertBark('foo/bar/bark?test=test'),
"a request to foo/%RT": macros.assert404(9090, 'foo/%RT')
"a request to foo/wild/bark": assertBark('foo/wild/bark'),
"a request to foo/%RT": macros.assert404(9090, 'foo/%RT'),
"a request to /v2/somepath": macros.assertGet(
9090,
'/v2/somepath',
'hello from (/v2)'
)
}
}
}
Expand Down

0 comments on commit 559ca26

Please sign in to comment.