Changed to allow extname or type/subtype only. Closes #1072
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 67ca22b · Browse files · 2 files·+42 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+42 added−11 removed
- tests:+31 added−0 removed
- other:+11 added−11 removed
Provenance
- Commit
24a4e95- Tree
a113e1f- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
67ca22b
2 files changed+42 added−11 removed
modified:lib/response.js+11 added-11 removed
18 unmodified lines19 , statusCodes = http.STATUS_CODES20 , send = connect.static.send21 , mime = require('mime')22 , basename = path.basename23+ , extname = path.extname24 , join = path.join;25 26 /**27 * Response prototype.236 unmodified lines264 return this.attachment(filename || path).sendfile(path, fn);265 };266 267 /**267- * Set _Content-Type_ response header passed through `mime.lookup()`.268+ * Set _Content-Type_ response header with `type` through `mime.lookup()`269+ * when it does not contain "/", or set the Content-Type to `type` otherwise.270 *271 * Examples:272 *271- * var filename = 'path/to/image.png';272- * res.contentType(filename);273- * // res.headers['Content-Type'] is now "image/png"274- *275- * res.contentType('.html');276- * res.contentType('html');277- * res.contentType('json');278- * res.contentType('png');273+ * res.type('.html');274+ * res.type('html');275+ * res.type('json');276+ * res.type('application/json');277 * res.type('png');278 *279 * @param {String} type280 * @return {ServerResponse} for chaining1 unmodified line282 */283 284 res.contentType =285 res.type = function(type){288- return this.set('Content-Type', mime.lookup(type));286+ return this.set('Content-Type', ~type.indexOf('/')287+ ? type288+ : mime.lookup(type));289 };290 291 /**292 * Respond to the Acceptable formats using an `obj`73 unmodified lines366 * @api public367 */368 369 res.attachment = function(filename){370- if (filename) this.type(filename);370+ if (filename) this.type(extname(filename));371 this.set('Content-Disposition', filename372 ? 'attachment; filename="' + basename(filename) + '"'373 : 'attachment');374 return this;262 unmodified linesmodified:test/res.type.js+31 added
16 unmodified lines1718192021222324252627282930313233343536373839404142434445464748495051525316 unmodified lines res.headers.should.have.property('content-type', 'application/javascript'); done(); }) })
it('should default to application/octet-stream', function(done){ var app = express();
app.use(function(req, res){ res.type('rawr').end('var name = "tj";'); });
request(app) .get('/') .end(function(res){ res.headers.should.have.property('content-type', 'application/octet-stream'); done(); }) })
it('should set the Content-Type with type/subtype', function(done){ var app = express();
app.use(function(req, res){ res.type('application/vnd.amazon.ebook') .end('var name = "tj";'); });
request(app) .get('/') .end(function(res){ res.headers.should.have.property('content-type', 'application/vnd.amazon.ebook'); done(); }) }) })})