Skip to content

Commit

Permalink
Refactor directory file lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Jan 14, 2018
1 parent f424ddb commit bd48207
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 39 deletions.
63 changes: 24 additions & 39 deletions lib/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ exports.handler = function (route, options) {
error = err;
}

// Not found
// Handle Not found

if (internals.isNotFound(error)) {
if (settings.defaultExtension) {
Expand All @@ -138,55 +138,40 @@ exports.handler = function (route, options) {
return;
}

// Propagate non-directory errors
// Handle Directory

if (!internals.isDirectory(error)) {
if (internals.isDirectory(error)) {
if (settings.redirectToSlash !== false && // Defaults to true
!request.server.settings.router.stripTrailingSlash &&
!hasTrailingSlash) {

throw error;
}

// Directory

if (!indexNames.length &&
!settings.listing) {

throw error;
}

if (settings.redirectToSlash !== false && // Defaults to true
!request.server.settings.router.stripTrailingSlash &&
!hasTrailingSlash) {

return reply.redirect(resource + '/');
}

for (let i = 0; i < indexNames.length; ++i) {
const indexName = indexNames[i];

const indexFile = Path.join(path, indexName);
try {
// File loaded successfully

return await File.load(indexFile, request, fileOptions);
return reply.redirect(resource + '/');
}
catch (err) {
Bounce.ignore(err, 'boom');

// Directory
for (const indexName of indexNames) {
const indexFile = Path.join(path, indexName);
try {
return await File.load(indexFile, request, fileOptions);
}
catch (err) {
Bounce.ignore(err, 'boom');

if (!internals.isNotFound(err)) {
throw Boom.internal(indexName + ' is a directory', err);
}

if (!internals.isNotFound(err)) {
throw Boom.badImplementation(indexName + ' is a directory');
// Not found - try next
}
}
}

// None of the index files were found
// None of the index files were found

if (!settings.listing) {
throw Boom.forbidden();
if (settings.listing) {
return internals.generateListing(Path.join(baseDir, path), resource, selection, hasTrailingSlash, settings, request);
}
}

return await internals.generateListing(Path.join(baseDir, path), resource, selection, hasTrailingSlash, settings, request);
throw error;
};

for (let i = 0; i < paths.length; ++i) {
Expand Down
23 changes: 23 additions & 0 deletions test/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ describe('directory', () => {
expect(res.payload).to.contain('name": "inert"');
});

it('returns 404 when the a fn directory handler returns an empty array', async () => {

const directoryFn = (request) => {

return [];
};

const server = await provisionServer();
server.route({ method: 'GET', path: '/directoryfn/{path?}', handler: { directory: { path: directoryFn } } });

const res = await server.inject('/directoryfn/index.js');
expect(res.statusCode).to.equal(404);
});

it('returns the correct file when requesting a file from a child directory', async () => {

const server = await provisionServer();
Expand Down Expand Up @@ -525,6 +539,15 @@ describe('directory', () => {
expect(res.statusCode).to.equal(404);
});

it('appends default extension and errors on file', async () => {

const server = await provisionServer();
server.route({ method: 'GET', path: '/directory/{path*}', handler: { directory: { path: __dirname, defaultExtension: 'dir' } } });

const res = await server.inject('/directory/directory/index');
expect(res.statusCode).to.equal(403);
});

it('does not append default extension when directory exists', async () => {

const server = await provisionServer();
Expand Down
Empty file.

0 comments on commit bd48207

Please sign in to comment.