Use mime-types for file to content type mapping
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 3d05e85 · Browse files · 4 files·+13 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+13 added−16 removed
- other:+13 added−16 removed
Provenance
- Commit
a0276c6- Tree
ddbc9cd- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
3d05e85
4 files changed+13 added−16 removed
modified:lib/response.js+9 added-14 removed
16 unmodified lines17 var encodeUrl = require('encodeurl');18 var escapeHtml = require('escape-html');19 var http = require('http');20 var onFinished = require('on-finished');21+var mime = require('mime-types')22 var path = require('path');23 var pathIsAbsolute = require('path-is-absolute');24 var statuses = require('statuses')25 var merge = require('utils-merge');3 unmodified lines29 var setCharset = require('./utils').setCharset;30 var cookie = require('cookie');31 var send = require('send');32 var extname = path.extname;32-var mime = send.mime;33 var resolve = path.resolve;34 var vary = require('vary');35 36 /**10 unmodified lines47 48 module.exports = res49 50 /**51- * Module variables.52- * @private53- */54-55-var charsetRegExp = /;\s*charset\s*=/;56-57-/**51 * Set status `code`.52 *53 * @param {Number} code54 * @return {ServerResponse}388 unmodified lines443 return this.sendFile(fullPath, opts, done)444 };445 446 /**454- * Set _Content-Type_ response header with `type` through `mime.lookup()`447+ * Set _Content-Type_ response header with `type` through `mime.contentType()`448 * when it does not contain "/", or set the Content-Type to `type` otherwise.449+ * When no mapping is found though `mime.contentType()`, the type is set to450+ * "application/octet-stream".451 *452 * Examples:453 *454 * res.type('.html');9 unmodified lines464 465 res.contentType =466 res.type = function contentType(type) {467 var ct = type.indexOf('/') === -1473- ? mime.lookup(type)468+ ? (mime.contentType(type) || 'application/octet-stream')469 : type;470 471 return this.set('Content-Type', ct);472 };142 unmodified lines615 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });616 *617 * Aliased as `res.header()`.618 *619+ * When the set header is "Content-Type", the type is expanded to include620+ * the charset if not present using `mime.contentType()`.621+ *622 * @param {String|Object} field623 * @param {String|Array} val624 * @return {ServerResponse} for chaining625 * @public10 unmodified lines636 if (field.toLowerCase() === 'content-type') {637 if (Array.isArray(value)) {638 throw new TypeError('Content-Type cannot be set to an Array');639 }642- if (!charsetRegExp.test(value)) {643- var charset = mime.charsets.lookup(value.split(';')[0]);644- if (charset) value += '; charset=' + charset.toLowerCase();645- }640+ value = mime.contentType(value)641 }642 643 this.setHeader(field, value);644 } else {359 unmodified linesmodified:lib/utils.js+2 added-2 removed
12 unmodified lines13 */14 15 var Buffer = require('safe-buffer').Buffer16 var contentType = require('content-type');17-var mime = require('send').mime;17 var etag = require('etag');18+var mime = require('mime-types')19 var proxyaddr = require('proxy-addr');20 var qs = require('qs');21 var querystring = require('querystring');22 29 unmodified lines52 53 exports.normalizeType = function(type){54 return ~type.indexOf('/')55 ? acceptParams(type)56- : { value: mime.lookup(type), params: {} };56+ : { value: (mime.lookup(type) || 'application/octet-stream'), params: {} }57 };58 59 /**60 * Normalize `types`, for example "html" becomes "text/html".190 unmodified linesmodified:History.md+1 added
3 unmodified lines4 This incorporates all changes after 4.17.1 up to 4.17.2.5 6 * change:7 - query parser setting defaults to `'simple'`8+ - Use `mime-types` for file to content type mapping9 * deps: array-flatten@3.0.010 * deps: body-parser@2.0.0-beta.111 - `req.body` is no longer always initialized to `{}`12 - `urlencoded` parser now defaults `extended` to `false`3645 unmodified linesmodified:package.json+1 added
42 unmodified lines43444546474849505150 unmodified lines42 unmodified lines "finalhandler": "~1.1.2", "fresh": "0.5.2", "merge-descriptors": "1.0.1", "methods": "~1.1.2", "mime-types": "~2.1.34", "on-finished": "~2.3.0", "parseurl": "~1.3.3", "path-is-absolute": "1.0.1", "proxy-addr": "~2.0.7",50 unmodified lines