Skip to content

Commit a0276c6

Browse files
committed
Use mime-types for file to content type mapping
1 parent 3d05e85 commit a0276c6

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This incorporates all changes after 4.17.1 up to 4.17.2.
55

66
* change:
77
- query parser setting defaults to `'simple'`
8+
- Use `mime-types` for file to content type mapping
89
* deps: array-flatten@3.0.0
910
* deps: body-parser@2.0.0-beta.1
1011
- `req.body` is no longer always initialized to `{}`

lib/response.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var encodeUrl = require('encodeurl');
1818
var escapeHtml = require('escape-html');
1919
var http = require('http');
2020
var onFinished = require('on-finished');
21+
var mime = require('mime-types')
2122
var path = require('path');
2223
var pathIsAbsolute = require('path-is-absolute');
2324
var statuses = require('statuses')
@@ -29,7 +30,6 @@ var setCharset = require('./utils').setCharset;
2930
var cookie = require('cookie');
3031
var send = require('send');
3132
var extname = path.extname;
32-
var mime = send.mime;
3333
var resolve = path.resolve;
3434
var vary = require('vary');
3535

@@ -47,13 +47,6 @@ var res = Object.create(http.ServerResponse.prototype)
4747

4848
module.exports = res
4949

50-
/**
51-
* Module variables.
52-
* @private
53-
*/
54-
55-
var charsetRegExp = /;\s*charset\s*=/;
56-
5750
/**
5851
* Set status `code`.
5952
*
@@ -451,8 +444,10 @@ res.download = function download (path, filename, options, callback) {
451444
};
452445

453446
/**
454-
* Set _Content-Type_ response header with `type` through `mime.lookup()`
447+
* Set _Content-Type_ response header with `type` through `mime.contentType()`
455448
* 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 to
450+
* "application/octet-stream".
456451
*
457452
* Examples:
458453
*
@@ -470,7 +465,7 @@ res.download = function download (path, filename, options, callback) {
470465
res.contentType =
471466
res.type = function contentType(type) {
472467
var ct = type.indexOf('/') === -1
473-
? mime.lookup(type)
468+
? (mime.contentType(type) || 'application/octet-stream')
474469
: type;
475470

476471
return this.set('Content-Type', ct);
@@ -621,6 +616,9 @@ res.append = function append(field, val) {
621616
*
622617
* Aliased as `res.header()`.
623618
*
619+
* When the set header is "Content-Type", the type is expanded to include
620+
* the charset if not present using `mime.contentType()`.
621+
*
624622
* @param {String|Object} field
625623
* @param {String|Array} val
626624
* @return {ServerResponse} for chaining
@@ -639,10 +637,7 @@ res.header = function header(field, val) {
639637
if (Array.isArray(value)) {
640638
throw new TypeError('Content-Type cannot be set to an Array');
641639
}
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)
646641
}
647642

648643
this.setHeader(field, value);

lib/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
var Buffer = require('safe-buffer').Buffer
1616
var contentType = require('content-type');
17-
var mime = require('send').mime;
1817
var etag = require('etag');
18+
var mime = require('mime-types')
1919
var proxyaddr = require('proxy-addr');
2020
var qs = require('qs');
2121
var querystring = require('querystring');
@@ -53,7 +53,7 @@ exports.wetag = createETagGenerator({ weak: true })
5353
exports.normalizeType = function(type){
5454
return ~type.indexOf('/')
5555
? acceptParams(type)
56-
: { value: mime.lookup(type), params: {} };
56+
: { value: (mime.lookup(type) || 'application/octet-stream'), params: {} }
5757
};
5858

5959
/**

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"fresh": "0.5.2",
4545
"merge-descriptors": "1.0.1",
4646
"methods": "~1.1.2",
47+
"mime-types": "~2.1.34",
4748
"on-finished": "~2.3.0",
4849
"parseurl": "~1.3.3",
4950
"path-is-absolute": "1.0.1",

0 commit comments

Comments
 (0)