3 * Copyright(c) 2009-2013 TJ Holowaychuk
4 * Copyright(c) 2014-2015 Douglas Christopher Wilson
11 * Module dependencies.
15 var Buffer = require('safe-buffer').Buffer
16 var contentDisposition = require('content-disposition');
17 var contentType = require('content-type');
18 var deprecate = require('depd')('express');
19 var flatten = require('array-flatten');
20 var mime = require('send').mime;
21 var etag = require('etag');
22 var proxyaddr = require('proxy-addr');
23 var qs = require('qs');
24 var querystring = require('querystring');
27 * Return strong ETag for `body`.
29 * @param {String|Buffer} body
30 * @param {String} [encoding]
35 exports.etag = createETagGenerator({ weak: false })
38 * Return weak ETag for `body`.
40 * @param {String|Buffer} body
41 * @param {String} [encoding]
46 exports.wetag = createETagGenerator({ weak: true })
49 * Check if `path` looks absolute.
51 * @param {String} path
56 exports.isAbsolute = function(path){
57 if ('/' === path[0]) return true;
58 if (':' === path[1] && ('\\' === path[2] || '/' === path[2])) return true; // Windows device path
59 if ('\\\\' === path.substring(0, 2)) return true; // Microsoft Azure absolute path
63 * Flatten the given `arr`.
70 exports.flatten = deprecate.function(flatten,
71 'utils.flatten: use array-flatten npm module instead');
74 * Normalize the given `type`, for example "html" becomes "text/html".
76 * @param {String} type
81 exports.normalizeType = function(type){
82 return ~type.indexOf('/')
84 : { value: mime.lookup(type), params: {} };
88 * Normalize `types`, for example "html" becomes "text/html".
90 * @param {Array} types
95 exports.normalizeTypes = function(types){
98 for (var i = 0; i < types.length; ++i) {
99 ret.push(exports.normalizeType(types[i]));
106 * Generate Content-Disposition header appropriate for the filename.
107 * non-ascii filenames are urlencoded and a filename* parameter is added
109 * @param {String} filename
114 exports.contentDisposition = deprecate.function(contentDisposition,
115 'utils.contentDisposition: use content-disposition npm module instead');
118 * Parse accept params `str` returning an
119 * object with `.value`, `.quality` and `.params`.
121 * @param {String} str
126 function acceptParams (str) {
127 var parts = str.split(/ *; */);
128 var ret = { value: parts[0], quality: 1, params: {} }
130 for (var i = 1; i < parts.length; ++i) {
131 var pms = parts[i].split(/ *= */);
132 if ('q' === pms[0]) {
133 ret.quality = parseFloat(pms[1]);
135 ret.params[pms[0]] = pms[1];
143 * Compile "etag" value to function.
145 * @param {Boolean|String|Function} val
150 exports.compileETag = function(val) {
153 if (typeof val === 'function') {
168 throw new TypeError('unknown value for etag function: ' + val);
175 * Compile "query parser" value to function.
177 * @param {String|Function} val
182 exports.compileQueryParser = function compileQueryParser(val) {
185 if (typeof val === 'function') {
192 fn = querystring.parse;
198 fn = parseExtendedQueryString;
201 throw new TypeError('unknown value for query parser function: ' + val);
208 * Compile "proxy trust" value to function.
210 * @param {Boolean|String|Number|Array|Function} val
215 exports.compileTrust = function(val) {
216 if (typeof val === 'function') return val;
219 // Support plain true/false
220 return function(){ return true };
223 if (typeof val === 'number') {
224 // Support trusting hop count
225 return function(a, i){ return i < val };
228 if (typeof val === 'string') {
229 // Support comma-separated values
231 .map(function (v) { return v.trim() })
234 return proxyaddr.compile(val || []);
238 * Set the charset in a given Content-Type string.
240 * @param {String} type
241 * @param {String} charset
246 exports.setCharset = function setCharset(type, charset) {
247 if (!type || !charset) {
252 var parsed = contentType.parse(type);
255 parsed.parameters.charset = charset;
258 return contentType.format(parsed);
262 * Create an ETag generator function, generating ETags with
265 * @param {object} options
270 function createETagGenerator (options) {
271 return function generateETag (body, encoding) {
272 var buf = !Buffer.isBuffer(body)
273 ? Buffer.from(body, encoding)
276 return etag(buf, options)
281 * Parse an extended query string with qs.
283 * @param {String} str
288 function parseExtendedQueryString(str) {
289 return qs.parse(str, {
290 allowPrototypes: true
295 * Return new empty object.
301 function newObject() {