add matchesMime() util
This commit is contained in:
parent
271d921c41
commit
48601ea7d8
1 changed files with 36 additions and 14 deletions
|
@ -514,18 +514,22 @@ module.exports = {
|
||||||
is: function(types){
|
is: function(types){
|
||||||
// no request body
|
// no request body
|
||||||
if (!(this.length || 'transfer-encoding' in this.header)) return null;
|
if (!(this.length || 'transfer-encoding' in this.header)) return null;
|
||||||
|
|
||||||
var ct = this.type;
|
var ct = this.type;
|
||||||
// request body without a content type?
|
// no content-type
|
||||||
if (!ct) return false;
|
if (!ct) return false;
|
||||||
|
|
||||||
|
// paramless
|
||||||
ct = ct.split(';')[0];
|
ct = ct.split(';')[0];
|
||||||
|
|
||||||
if (!types) return ct;
|
if (!types) return ct;
|
||||||
|
|
||||||
|
// ensure array
|
||||||
if (!Array.isArray(types)) types = [].slice.call(arguments);
|
if (!Array.isArray(types)) types = [].slice.call(arguments);
|
||||||
var type, mt, cts;
|
|
||||||
for (var i = 0; i < types.length; i++) {
|
for (var i = 0; i < types.length; i++) {
|
||||||
type = types[i];
|
var type = types[i];
|
||||||
// exact match
|
|
||||||
if (type == ct) return type;
|
|
||||||
// convert to a valid mime type
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'urlencoded':
|
case 'urlencoded':
|
||||||
mt = 'application/x-www-form-urlencoded';
|
mt = 'application/x-www-form-urlencoded';
|
||||||
|
@ -533,15 +537,10 @@ module.exports = {
|
||||||
default:
|
default:
|
||||||
mt = ~type.indexOf('/') ? type : mime.lookup(type);
|
mt = ~type.indexOf('/') ? type : mime.lookup(type);
|
||||||
}
|
}
|
||||||
// mime types match
|
|
||||||
if (mt == ct) return type;
|
if (mimeMatch(mt, ct)) return type;
|
||||||
// type or subtype match
|
|
||||||
if (!~type.indexOf('*')) continue;
|
|
||||||
mt = mt.split('/');
|
|
||||||
cts = cts || ct.split('/');
|
|
||||||
if ('*' == mt[0] && mt[1] == cts[1]) return type;
|
|
||||||
if ('*' == mt[1] && mt[0] == cts[0]) return type;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// no matches
|
// no matches
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
|
@ -632,3 +631,26 @@ function extToMime(type) {
|
||||||
if (~type.indexOf('/')) return type;
|
if (~type.indexOf('/')) return type;
|
||||||
return mime.lookup(type);
|
return mime.lookup(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if `exected` mime type
|
||||||
|
* matches `actual` mime type with
|
||||||
|
* wildcard support.
|
||||||
|
*
|
||||||
|
* @param {String} expected
|
||||||
|
* @param {String} actual
|
||||||
|
* @return {Boolean}
|
||||||
|
* @api private
|
||||||
|
*/
|
||||||
|
|
||||||
|
function mimeMatch(expected, actual) {
|
||||||
|
if (expected == actual) return true;
|
||||||
|
|
||||||
|
if (!~expected.indexOf('*')) return false;
|
||||||
|
|
||||||
|
actual = actual.split('/');
|
||||||
|
expected = expected.split('/');
|
||||||
|
|
||||||
|
if ('*' == expected[0] && expected[1] == actual[1]) return true;
|
||||||
|
if ('*' == expected[1] && expected[0] == actual[0]) return true;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue