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

Ensure that links to directories end in a slash. #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 30 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
};

/**
Expand All @@ -230,7 +246,7 @@ function htmlPath(dir) {
var curr = [];
return dir.split('/').map(function(part){
curr.push(encodeURIComponent(part));
return part ? '<a href="' + curr.join('/') + '">' + part + '</a>' : '';
return part ? '<a href="' + curr.join('/') + '/">' + part + '</a>' : '';
}).join(' / ');
}

Expand Down Expand Up @@ -383,6 +399,7 @@ function html(files, dir, useIcons, view) {

return '<li><a href="'
+ normalizeSlashes(normalize(path.join('/')))
+ (isDir ? '/' : '')
+ '" class="'
+ classes.join(' ') + '"'
+ ' title="' + file.name + '">'
Expand Down
20 changes: 10 additions & 10 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ describe('serveIndex(root)', function () {
.set('Accept', 'text/html')
.expect(200)
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(/<a href="\/g%23%20%253%20o%20%252525%20%2537%20dir"/)
.expect(/<a href="\/users"/)
.expect(/<a href="\/g%23%20%253%20o%20%252525%20%2537%20dir\/"/)
.expect(/<a href="\/users\/"/)
.expect(/<a href="\/file%20%231.txt"/)
.expect(/<a href="\/todo.txt"/)
.expect(/<a href="\/%E3%81%95%E3%81%8F%E3%82%89\.txt"/)
Expand All @@ -149,10 +149,10 @@ describe('serveIndex(root)', function () {
var body = res.text.split('</h1>')[1];
var urls = body.split(/<a href="([^"]*)"/).filter(function(s, i){ return i%2; });
assert.deepEqual(urls, [
'/%23directory',
'/collect',
'/g%23%20%253%20o%20%252525%20%2537%20dir',
'/users',
'/%23directory/',
'/collect/',
'/g%23%20%253%20o%20%252525%20%2537%20dir/',
'/users/',
'/file%20%231.txt',
'/foo%20bar',
'/nums',
Expand Down Expand Up @@ -445,7 +445,7 @@ describe('serveIndex(root)', function () {
.set('Accept', 'text/html')
.expect(200)
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(/<a href="\/%23directory"/)
.expect(/<a href="\/%23directory\/"/)
.expect(/<a href="\/%23directory\/index.html"/)
.end(done);
});
Expand All @@ -458,7 +458,7 @@ describe('serveIndex(root)', function () {
.set('Accept', 'text/html')
.expect(200)
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(/<a href="\/g%23%20%253%20o%20%252525%20%2537%20dir"/)
.expect(/<a href="\/g%23%20%253%20o%20%252525%20%2537%20dir\/"/)
.expect(/<a href="\/g%23%20%253%20o%20%252525%20%2537%20dir\/empty.txt"/)
.end(done);
});
Expand All @@ -483,8 +483,8 @@ describe('serveIndex(root)', function () {
request(server)
.get('/')
.set('Accept', 'text/html')
.expect(/<a href="\/g%23%20%253%20o%20%252525%20%2537%20dir"/)
.expect(/<a href="\/users"/)
.expect(/<a href="\/g%23%20%253%20o%20%252525%20%2537%20dir\/"/)
.expect(/<a href="\/users\/"/)
.expect(/<a href="\/file%20%231.txt"/)
.expect(/<a href="\/todo.txt"/)
.expect(200, done)
Expand Down