2 var typer = require('media-typer')
3 var mime = require('mime-types')
5 module.exports = typeofrequest;
6 typeofrequest.is = typeis;
7 typeofrequest.hasBody = hasbody;
8 typeofrequest.normalize = normalize;
9 typeofrequest.match = mimeMatch;
12 * Compare a `value` content-type with `types`.
13 * Each `type` can be an extension like `html`,
14 * a special shortcut like `multipart` or `urlencoded`,
17 * If no types match, `false` is returned.
18 * Otherwise, the first `type` that matches is returned.
20 * @param {String} value
21 * @param {Array} types
25 function typeis(value, types_) {
29 // remove parameters and normalize
30 var val = typenormalize(value)
37 // support flattened arguments
38 if (types && !Array.isArray(types)) {
39 types = new Array(arguments.length - 1)
40 for (i = 0; i < types.length; i++) {
41 types[i] = arguments[i + 1]
45 // no types, return the content type
46 if (!types || !types.length) {
51 for (i = 0; i < types.length; i++) {
52 if (mimeMatch(normalize(type = types[i]), val)) {
53 return type[0] === '+' || ~type.indexOf('*')
64 * Check if a request has a request body.
65 * A request with a body __must__ either have `transfer-encoding`
66 * or `content-length` headers set.
67 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3
69 * @param {Object} request
74 function hasbody(req) {
75 var headers = req.headers;
76 if ('transfer-encoding' in headers) return true;
77 return !isNaN(headers['content-length']);
81 * Check if the incoming request contains the "Content-Type"
82 * header field, and it contains any of the give mime `type`s.
83 * If there is no request body, `null` is returned.
84 * If there is no content type, `false` is returned.
85 * Otherwise, it returns the first `type` that matches.
89 * // With Content-Type: text/html; charset=utf-8
90 * this.is('html'); // => 'html'
91 * this.is('text/html'); // => 'text/html'
92 * this.is('text/*', 'application/json'); // => 'text/html'
94 * // When Content-Type is application/json
95 * this.is('json', 'urlencoded'); // => 'json'
96 * this.is('application/json'); // => 'application/json'
97 * this.is('html', 'application/*'); // => 'application/json'
99 * this.is('html'); // => false
101 * @param {String|Array} types...
102 * @return {String|false|null}
106 function typeofrequest(req, types_) {
114 // support flattened arguments
115 if (arguments.length > 2) {
116 types = new Array(arguments.length - 1)
117 for (var i = 0; i < types.length; i++) {
118 types[i] = arguments[i + 1]
122 // request content type
123 var value = req.headers['content-type']
125 return typeis(value, types);
129 * Normalize a mime type.
130 * If it's a shorthand, expand it to a valid mime type.
132 * In general, you probably want:
134 * var type = is(req, ['urlencoded', 'json', 'multipart']);
136 * Then use the appropriate body parsers.
137 * These three are the most common request body types
138 * and are thus ensured to work.
140 * @param {String} type
144 function normalize(type) {
146 case 'urlencoded': return 'application/x-www-form-urlencoded';
148 type = 'multipart/*';
152 return type[0] === '+' || ~type.indexOf('/')
158 * Check if `exected` mime type
159 * matches `actual` mime type with
160 * wildcard and +suffix support.
162 * @param {String} expected
163 * @param {String} actual
168 function mimeMatch(expected, actual) {
170 if (expected === false) {
175 if (expected === actual) {
179 actual = actual.split('/');
181 if (expected[0] === '+') {
183 return Boolean(actual[1])
184 && expected.length <= actual[1].length
185 && expected === actual[1].substr(0 - expected.length)
188 if (!~expected.indexOf('*')) return false;
190 expected = expected.split('/');
192 if (expected[0] === '*') {
194 return expected[1] === actual[1]
197 if (expected[1] === '*') {
199 return expected[0] === actual[0]
202 if (expected[1][0] === '*' && expected[1][1] === '+') {
204 return expected[0] === actual[0]
205 && expected[1].length <= actual[1].length + 1
206 && expected[1].substr(1) === actual[1].substr(1 - expected[1].length)
213 * Normalize a type and remove parameters.
215 * @param {string} value
220 function typenormalize(value) {
222 var type = typer.parse(value)
223 delete type.parameters
224 return typer.format(type)