Skip to content

Commit

Permalink
Merge pull request hapijs#1058 from spumko/issue/1056
Browse files Browse the repository at this point in the history
  • Loading branch information
geek committed Sep 9, 2013
2 parents 7abce5f + 929cb22 commit 7e75bfa
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
9 changes: 5 additions & 4 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,11 @@ The following options are available when adding a route:
- `'attachment'`
- `'inline'`
<p></p>
- <a name="route.config.directory"></a>`directory` - generates a directory endpoint for serving static content from a directory. Routes using the directory handler must include a
single path parameter at the end of the path string (e.g. '/path/to/somewhere/{param}' where the parameter name does not matter). The path
parameter can use any of the parameter options (e.g. '{param}' for one level files only, '{param?}' for one level files or the directory root,
'{param*}' for any level, or '{param*3}' for a specific level). The directory handler is an object with the following options:
- <a name="route.config.directory"></a>`directory` - generates a directory endpoint for serving static content from a directory. Routes using the
directory handler must include a path parameter at the end of the path string (e.g. '/path/to/somewhere/{param}' where the parameter name does
not matter). The path parameter can use any of the parameter options (e.g. '{param}' for one level files only, '{param?}' for one level files or
the directory root, '{param*}' for any level, or '{param*3}' for a specific level). If additional path parameters are present, they are ignored for
the purpose of selecting the file system resource. The directory handler is an object with the following options:
- `path` - (required) the directory root path (relative paths are resolved based on the server [`files`](#server.config.files) configuration).
Value can be:
- a single path string used as the prefix for any resources requested by appending the request path parameter to the provided string.
Expand Down
9 changes: 5 additions & 4 deletions lib/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ exports.directoryHandler = function (route, options) {
Utils.assert(typeof options === 'object' && options.path, 'Options must be an object with a path');
Utils.assert(typeof options.path === 'function' || typeof options.path === 'string' || Array.isArray(options.path), 'options.path must be a function, a string, or an array of strings');
Utils.assert(route.path[route.path.length - 1] === '}', 'The route path must end with a parameter');
Utils.assert(route.params.length === 1, 'The route path must include one and only one parameter');
Utils.assert(route.params.length >= 1, 'The route path must include at least one parameter');

var settings = Utils.clone(options); // options can be reused
var absolutePath = internals.absolutePath(route);
Expand Down Expand Up @@ -102,12 +102,13 @@ exports.directoryHandler = function (route, options) {
// Append parameter

var selection = null;
if (request._paramsArray[0]) {
if (request._paramsArray[0].indexOf('..') !== -1) {
var lastParam = request._paramsArray[request._paramsArray.length - 1];
if (lastParam) {
if (lastParam.indexOf('..') !== -1) {
return request.reply(Boom.forbidden());
}

selection = request._paramsArray[0];
selection = lastParam;
}

// Generate response
Expand Down
4 changes: 2 additions & 2 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ exports.sort = function (a, b) {
var bSegment = bFingers[i];

if ((aSegment.isWildcard && bSegment.isWildcard) ||
(aSegment.name && bSegment.name) ||
(aSegment.literal == bSegment.literal)) {
(aSegment.name && bSegment.name && !aSegment.isWildcard && !bSegment.isWildcard) ||
(aSegment.literal !== undefined && aSegment.literal === bSegment.literal)) {

continue;
}
Expand Down
11 changes: 11 additions & 0 deletions test/integration/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ describe('Response', function () {
server.route({ method: 'GET', path: '/showindex/{path*}', handler: { directory: { path: './', index: true, listing: true } } });
server.route({ method: 'GET', path: '/multiple/{path*}', handler: { directory: { path: ['./', '../'], listing: true } } });
server.route({ method: 'GET', path: '/redirect/{path*}', handler: { directory: { path: './', index: true, listing: true, redirectToSlash: true } } });
server.route({ method: 'GET', path: '/{ignore}/4/{path*}', handler: { directory: { path: '.' } } });

it('returns a 403 when no index exists and listing is disabled', function (done) {

Expand Down Expand Up @@ -955,6 +956,16 @@ describe('Response', function () {
done();
});
});

it('ignores unused path params', function (done) {

server.inject('/crap/4/response.js', function (res) {

expect(res.statusCode).to.equal(200);
expect(res.payload).to.contain('hapi');
done();
});
});
});

describe('Stream', function () {
Expand Down
6 changes: 4 additions & 2 deletions test/integration/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('Route', function () {
'/a/{p}/b/{x}',
'/{p*5}',
'/a/b/{p*}',
'/{a}/b/{p*}',
'/{p*}'
];

Expand Down Expand Up @@ -111,7 +112,7 @@ describe('Route', function () {
});
};

for (var i = 0; i < 50; i++) {
for (var i = 0; i < 50; ++i) {
randomLoad();
}

Expand Down Expand Up @@ -156,7 +157,8 @@ describe('Route', function () {
['/a/c/b/d', '/a/{p}/b/{x}'],
['/a/b/c/d/e', '/{p*5}'],
['/a/b/c/d/e/f', '/a/b/{p*}'],
['/x/b/c/d/e/f/g', '/{p*}']
['/x/b/c/d/e/f/g', '/{a}/b/{p*}'],
['/x/y/c/d/e/f/g', '/{p*}']
];

Async.forEachSeries(requests, function (request, next) {
Expand Down
6 changes: 6 additions & 0 deletions test/unit/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ describe('Route', function () {
'/path/': {
p: 'path/'
}
},
'/{a}/b/{p*}': {
'/a/b/path/': {
a: 'a',
p: 'path/'
}
}
};

Expand Down

0 comments on commit 7e75bfa

Please sign in to comment.