3 * Copyright(c) 2014 Jonathan Ong
4 * Copyright(c) 2014-2015 Douglas Christopher Wilson
11 * Module dependencies.
15 var typer = require('media-typer')
16 var mime = require('mime-types')
23 module.exports = typeofrequest
24 module.exports.is = typeis
25 module.exports.hasBody = hasbody
26 module.exports.normalize = normalize
27 module.exports.match = mimeMatch
30 * Compare a `value` content-type with `types`.
31 * Each `type` can be an extension like `html`,
32 * a special shortcut like `multipart` or `urlencoded`,
35 * If no types match, `false` is returned.
36 * Otherwise, the first `type` that matches is returned.
38 * @param {String} value
39 * @param {Array} types
43 function typeis (value, types_) {
47 // remove parameters and normalize
48 var val = tryNormalizeType(value)
55 // support flattened arguments
56 if (types && !Array.isArray(types)) {
57 types = new Array(arguments.length - 1)
58 for (i = 0; i < types.length; i++) {
59 types[i] = arguments[i + 1]
63 // no types, return the content type
64 if (!types || !types.length) {
69 for (i = 0; i < types.length; i++) {
70 if (mimeMatch(normalize(type = types[i]), val)) {
71 return type[0] === '+' || type.indexOf('*') !== -1
82 * Check if a request has a request body.
83 * A request with a body __must__ either have `transfer-encoding`
84 * or `content-length` headers set.
85 * http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3
87 * @param {Object} request
92 function hasbody (req) {
93 return req.headers['transfer-encoding'] !== undefined ||
94 !isNaN(req.headers['content-length'])
98 * Check if the incoming request contains the "Content-Type"
99 * header field, and it contains any of the give mime `type`s.
100 * If there is no request body, `null` is returned.
101 * If there is no content type, `false` is returned.
102 * Otherwise, it returns the first `type` that matches.
106 * // With Content-Type: text/html; charset=utf-8
107 * this.is('html'); // => 'html'
108 * this.is('text/html'); // => 'text/html'
109 * this.is('text/*', 'application/json'); // => 'text/html'
111 * // When Content-Type is application/json
112 * this.is('json', 'urlencoded'); // => 'json'
113 * this.is('application/json'); // => 'application/json'
114 * this.is('html', 'application/*'); // => 'application/json'
116 * this.is('html'); // => false
118 * @param {String|Array} types...
119 * @return {String|false|null}
123 function typeofrequest (req, types_) {
131 // support flattened arguments
132 if (arguments.length > 2) {
133 types = new Array(arguments.length - 1)
134 for (var i = 0; i < types.length; i++) {
135 types[i] = arguments[i + 1]
139 // request content type
140 var value = req.headers['content-type']
142 return typeis(value, types)
146 * Normalize a mime type.
147 * If it's a shorthand, expand it to a valid mime type.
149 * In general, you probably want:
151 * var type = is(req, ['urlencoded', 'json', 'multipart']);
153 * Then use the appropriate body parsers.
154 * These three are the most common request body types
155 * and are thus ensured to work.
157 * @param {String} type
161 function normalize (type) {
162 if (typeof type !== 'string') {
169 return 'application/x-www-form-urlencoded'
174 if (type[0] === '+') {
175 // "+json" -> "*/*+json" expando
179 return type.indexOf('/') === -1
185 * Check if `expected` mime type
186 * matches `actual` mime type with
187 * wildcard and +suffix support.
189 * @param {String} expected
190 * @param {String} actual
195 function mimeMatch (expected, actual) {
197 if (expected === false) {
202 var actualParts = actual.split('/')
203 var expectedParts = expected.split('/')
206 if (actualParts.length !== 2 || expectedParts.length !== 2) {
211 if (expectedParts[0] !== '*' && expectedParts[0] !== actualParts[0]) {
215 // validate suffix wildcard
216 if (expectedParts[1].substr(0, 2) === '*+') {
217 return expectedParts[1].length <= actualParts[1].length + 1 &&
218 expectedParts[1].substr(1) === actualParts[1].substr(1 - expectedParts[1].length)
222 if (expectedParts[1] !== '*' && expectedParts[1] !== actualParts[1]) {
230 * Normalize a type and remove parameters.
232 * @param {string} value
237 function normalizeType (value) {
239 var type = typer.parse(value)
241 // remove the parameters
242 type.parameters = undefined
245 return typer.format(type)
249 * Try to normalize a type and remove parameters.
251 * @param {string} value
256 function tryNormalizeType (value) {
258 return normalizeType(value)