From 6f0318255f59e8869ee07d15eee3bfcff6b62b9b Mon Sep 17 00:00:00 2001 From: Scott Dial Date: Mon, 26 Jan 2015 12:25:07 -0500 Subject: [PATCH] Ensure that links to directories end in a slash. --- index.js | 43 ++++++++++++++++++++++++++++++------------- test/test.js | 20 ++++++++++---------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index 1fec6fd9..65b10c08 100644 --- a/index.js +++ b/index.js @@ -191,26 +191,42 @@ exports.html = function(req, res, files, next, dir, showUp, icons, path, view, t * Respond with application/json. */ -exports.json = function(req, res, files){ - var body = JSON.stringify(files); - var buf = new Buffer(body, 'utf8'); +exports.json = function(req, res, files, next, dir, showUp, icons, path){ + stat(path, files, function(err, stats) { + if (err) return next(err); + // append slashes to directory filenames + files = files.map(function(file, i) { + return file + (stats[i].isDirectory() ? '/' : ''); + }); - res.setHeader('Content-Type', 'application/json; charset=utf-8'); - res.setHeader('Content-Length', buf.length); - res.end(buf); + var body = JSON.stringify(files); + var buf = new Buffer(body, 'utf8'); + + res.setHeader('Content-Type', 'application/json; charset=utf-8'); + res.setHeader('Content-Length', buf.length); + res.end(buf); + }); }; /** * Respond with text/plain. */ -exports.plain = function(req, res, files){ - var body = files.join('\n') + '\n'; - var buf = new Buffer(body, 'utf8'); +exports.plain = function(req, res, files, next, dir, showUp, icons, path){ + stat(path, files, function(err, stats) { + if (err) return next(err); + // append slashes to directory filenames + files = files.map(function(file, i) { + return file + (stats[i].isDirectory() ? '/' : ''); + }); - res.setHeader('Content-Type', 'text/plain; charset=utf-8'); - res.setHeader('Content-Length', buf.length); - res.end(buf); + var body = files.join('\n') + '\n'; + var buf = new Buffer(body, 'utf8'); + + res.setHeader('Content-Type', 'text/plain; charset=utf-8'); + res.setHeader('Content-Length', buf.length); + res.end(buf); + }); }; /** @@ -230,7 +246,7 @@ function htmlPath(dir) { var curr = []; return dir.split('/').map(function(part){ curr.push(encodeURIComponent(part)); - return part ? '' + part + '' : ''; + return part ? '' + part + '' : ''; }).join(' / '); } @@ -383,6 +399,7 @@ function html(files, dir, useIcons, view) { return '
  • ' diff --git a/test/test.js b/test/test.js index a4046db4..e20a51e1 100644 --- a/test/test.js +++ b/test/test.js @@ -128,8 +128,8 @@ describe('serveIndex(root)', function () { .set('Accept', 'text/html') .expect(200) .expect('Content-Type', 'text/html; charset=utf-8') - .expect(/')[1]; var urls = body.split(/