Skip to content
Myr

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

4 files+13 added−16 removed

modified:lib/response.js+9 added-14 removed
16 unmodified lines
17var encodeUrl = require('encodeurl');18var escapeHtml = require('escape-html');19var http = require('http');20var onFinished = require('on-finished');21var mime = require('mime-types')22var path = require('path');23var pathIsAbsolute = require('path-is-absolute');24var statuses = require('statuses')25var merge = require('utils-merge');
3 unmodified lines
29var setCharset = require('./utils').setCharset;30var cookie = require('cookie');31var send = require('send');32var extname = path.extname;32var mime = send.mime;33var resolve = path.resolve;34var vary = require('vary');3536/**
10 unmodified lines
4748module.exports = res4950/**51 * Module variables.52 * @private53 */5455var charsetRegExp = /;\s*charset\s*=/;5657/**51 * Set status `code`.52 *53 * @param {Number} code54 * @return {ServerResponse}
388 unmodified lines
443 return this.sendFile(fullPath, opts, done)444};445446/**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 lines
464465res.contentType =466res.type = function contentType(type) {467 var ct = type.indexOf('/') === -1473 ? mime.lookup(type)468 ? (mime.contentType(type) || 'application/octet-stream')469 : type;470471 return this.set('Content-Type', ct);472};
142 unmodified lines
615 * 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 * @public
10 unmodified lines
636 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 }642643 this.setHeader(field, value);644 } else {
359 unmodified lines
modified:lib/utils.js+2 added-2 removed
12 unmodified lines
13 */1415var Buffer = require('safe-buffer').Buffer16var contentType = require('content-type');17var mime = require('send').mime;17var etag = require('etag');18var mime = require('mime-types')19var proxyaddr = require('proxy-addr');20var qs = require('qs');21var querystring = require('querystring');22
29 unmodified lines
5253exports.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};5859/**60 * Normalize `types`, for example "html" becomes "text/html".
190 unmodified lines
modified:History.md+1 added
3 unmodified lines
4This incorporates all changes after 4.17.1 up to 4.17.2.56 * 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 lines
modified:package.json+1 added