Skip to content

Commit

Permalink
encodeURI instead of encodeURIComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
vvo committed Feb 3, 2015
1 parent 35ab85c commit 4f63710
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/ecstatic/showdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ module.exports = function (opts, stat) {
var writeRow = function (file, i) {
// render a row given a [name, stat] tuple
var isDir = file[1].isDirectory && file[1].isDirectory();
var href =
var href = encodeURI(
parsed.pathname.replace(/\/$/, '') +
'/' + encodeURIComponent(file[0]);
'/' + file[0]);

// append trailing slash and query for dir entry
if (isDir) {
Expand Down
4 changes: 4 additions & 0 deletions test/public/show-dir-href-encoding/aname+aplus.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
It should generate an href of <a href="/show-dir-href-encoding/aname+aplus.txt">..

http://stackoverflow.com/a/1006074/147079
http://tools.ietf.org/html/rfc1738
33 changes: 33 additions & 0 deletions test/showdir-href-encoding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var test = require('tap').test,
ecstatic = require('../lib/ecstatic'),
http = require('http'),
request = require('request'),
path = require('path');

var root = __dirname + '/public',
baseDir = 'base';

test('url encoding in href', function (t) {
var port = Math.floor(Math.random() * ((1<<16) - 1e4) + 1e4);

var uri = 'http://localhost:' + port + path.join('/', baseDir, 'show-dir-href-encoding');

var server = http.createServer(
ecstatic({
root: root,
baseDir: baseDir,
showDir: true,
autoIndex: false
})
);

server.listen(port, function () {
request.get({
uri: uri
}, function(err, res, body) {
t.ok(/href="\/base\/show\-dir\-href\-encoding\/aname\+aplus\.txt"/.test(body), 'We found the right href');
server.close();
t.end();
});
});
});

0 comments on commit 4f63710

Please sign in to comment.