Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #1056 and #1057 #1058

Merged
merged 1 commit into from
Sep 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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