Skip to content

Commit

Permalink
[fix] Trap errors from decodeURI. Fixes #127
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Jul 26, 2012
1 parent c583a86 commit 2069a8b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
14 changes: 10 additions & 4 deletions lib/director/http/index.js
Expand Up @@ -108,24 +108,30 @@ Router.prototype.dispatch = function (req, res, callback) {
// Dispatch `HEAD` requests to `GET`
//
var method = req.method === 'HEAD' ? 'get' : req.method.toLowerCase(),
url = decodeURI(req.url.split('?', 1)[0]),
thisArg = { req: req, res: res },
self = this,
contentType,
runlist,
stream,
error,
fns;
fns,
url;

if (this._hasAccepts) {
//
// Trap bad URLs from `decodeUri`
//
try { url = decodeURI(req.url.split('?', 1)[0]) }
catch (ex) { url = null }

if (url && this._hasAccepts) {
contentType = req.headers['content-type'];
fns = this.traverse(method, url, this.routes, '', function (route) {
return !route.accept || route.accept.some(function (a) {
return a.test(contentType);
});
});
}
else {
else if (url) {
fns = this.traverse(method, url, this.routes, '');
}

Expand Down
12 changes: 12 additions & 0 deletions test/server/helpers/macros.js
Expand Up @@ -25,6 +25,18 @@ exports.assertGet = function(port, uri, expected) {
return context;
};

exports.assert404 = function (port, uri) {
return {
topic: function () {
request({ uri: 'http://localhost:' + port + '/' + uri }, this.callback);
},
"should respond with 404": function (err, res, body) {
assert.isNull(err);
assert.equal(res.statusCode, 404);
}
};
};

exports.assertPost = function(port, uri, expected) {
return {
topic: function () {
Expand Down
3 changes: 2 additions & 1 deletion test/server/http/http-test.js
Expand Up @@ -49,7 +49,8 @@ vows.describe('director/http').addBatch({
"a request to foo/bar/bark": assertBark('foo/bar/bark'),
"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/bar/bark?test=test": assertBark('foo/bar/bark?test=test'),
"a request to foo/%RT": macros.assert404(9090, 'foo/%RT')
}
}
}
Expand Down

0 comments on commit 2069a8b

Please sign in to comment.