support non-ascii filenames in content-disposition headers
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andy VanWagoner · committed by Andy VanWagoner · parent 79cc5a4 · Browse files · 5 files·+60 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+60 added−5 removed
- tests:+31 added−0 removed
- other:+29 added−5 removed
Provenance
- Commit
56b672e- Tree
ac9e544- Author
Andy VanWagoner <thetalecrafter@gmail.com>- Committer
Andy VanWagoner <thetalecrafter@gmail.com>- Parent
79cc5a4
5 files changed+60 added−5 removed
added:examples/downloads/files/utf-8 한中日.txt+1 added
1+한中日
modified:examples/downloads/app.js+2 added-1 removed
6 unmodified lines78910111213141524 unmodified lines4041424343446 unmodified lines
app.get('/', function(req, res){ res.send('<ul>' + '<li>Download <a href="/files/amazing.txt">amazing.txt</a>.</li>' + '<li>Download <a href="/files/utf-8 한中日.txt">utf-8 한中日.txt</a>.</li>' + '<li>Download <a href="/files/missing.txt">missing.txt</a>.</li>' + '</ul>');});
24 unmodified lines
if (!module.parent) { app.listen(3000); console.log('Express started on port 3000');}No newline at end of file}modified:lib/response.js+3 added-4 removed
7 unmodified lines8 var escapeHtml = require('escape-html');9 var sign = require('cookie-signature').sign;10 var normalizeType = require('./utils').normalizeType;11 var normalizeTypes = require('./utils').normalizeTypes;12+var contentDisposition = require('./utils').contentDisposition;13 var etag = require('./utils').etag;14 var statusCodes = http.STATUS_CODES;15 var cookie = require('cookie');16 var send = require('send');348 unmodified lines365 filename = null;366 }367 368 filename = filename || path;368- this.set('Content-Disposition', 'attachment; filename="' + basename(filename) + '"');369+ this.set('Content-Disposition', contentDisposition(filename));370 return this.sendfile(path, fn);371 };372 373 /**113 unmodified lines487 */488 489 res.attachment = function(filename){490 if (filename) this.type(extname(filename));490- this.set('Content-Disposition', filename491- ? 'attachment; filename="' + basename(filename) + '"'492- : 'attachment');491+ this.set('Content-Disposition', contentDisposition(filename));492 return this;493 };494 495 /**282 unmodified linesmodified:lib/utils.js+23 added
2 unmodified lines3456789101173 unmodified lines85868788899091929394959697989910010110210310410510610710810911011111211311420 unmodified lines2 unmodified lines */
var mime = require('send').mime;var crc32 = require('buffer-crc32');var basename = require('path').basename;
/** * Return ETag for `body`. *73 unmodified lines return ret;};
/** * Generate Content-Disposition header appropriate for the filename. * non-ascii filenames are urlencoded and a filename* parameter is added * * @param {String} filename * @return {String} * @api private */
exports.contentDisposition = function(filename){ var ret = 'attachment'; if (filename) { filename = basename(filename); // if filename contains non-ascii characters, add a utf-8 version ala RFC 5987 ret = /[^\040-\176]/.test(filename) ? 'attachment; filename=' + encodeURI(filename) + '; filename*=UTF-8\'\'' + encodeURI(filename) : 'attachment; filename="' + filename + '"'; }
return ret;};
/** * Parse accept params `str` returning an * object with `.value`, `.quality` and `.params`. * also includes `.originalIndex` for stable sorting *20 unmodified linesmodified:test/res.attachment.js+31 added
42 unmodified lines43 .get('/')44 .expect('Content-Type', 'image/png', done);45 })46 })47+48+ describe('.attachment(utf8filename)', function(){49+ it('should add the filename and filename* params', function(done){50+ var app = express();51+52+ app.use(function(req, res){53+ res.attachment('/locales/日本語.txt');54+ res.send('japanese');55+ });56+57+ request(app)58+ .get('/')59+ .expect('Content-Disposition', 'attachment;' +60+ ' filename=%E6%97%A5%E6%9C%AC%E8%AA%9E.txt;' +61+ ' filename*=UTF-8\'\'%E6%97%A5%E6%9C%AC%E8%AA%9E.txt',62+ done);63+ })64+65+ it('should set the Content-Type', function(done){66+ var app = express();67+68+ app.use(function(req, res){69+ res.attachment('/locales/日本語.txt');70+ res.send('japanese');71+ });72+73+ request(app)74+ .get('/')75+ .expect('Content-Type', 'text/plain; charset=utf-8', done);76+ })77+ })78 })