Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / serve-favicon / index.js
1 /*!
2  * serve-favicon
3  * Copyright(c) 2010 Sencha Inc.
4  * Copyright(c) 2011 TJ Holowaychuk
5  * Copyright(c) 2014-2015 Douglas Christopher Wilson
6  * MIT Licensed
7  */
8
9 'use strict';
10
11 /**
12  * Module dependencies.
13  * @private
14  */
15
16 var etag = require('etag');
17 var fresh = require('fresh');
18 var fs = require('fs');
19 var ms = require('ms');
20 var parseUrl = require('parseurl');
21 var path = require('path');
22 var resolve = path.resolve;
23
24 /**
25  * Module exports.
26  * @public
27  */
28
29 module.exports = favicon;
30
31 /**
32  * Module variables.
33  * @private
34  */
35
36 var maxMaxAge = 60 * 60 * 24 * 365 * 1000; // 1 year
37
38 /**
39  * Serves the favicon located by the given `path`.
40  *
41  * @public
42  * @param {String|Buffer} path
43  * @param {Object} [options]
44  * @return {Function} middleware
45  */
46
47 function favicon(path, options) {
48   var opts = options || {};
49
50   var buf;
51   var icon; // favicon cache
52   var maxAge = calcMaxAge(opts.maxAge);
53   var stat;
54
55   if (!path) throw new TypeError('path to favicon.ico is required');
56
57   if (Buffer.isBuffer(path)) {
58     buf = new Buffer(path.length);
59     path.copy(buf);
60
61     icon = createIcon(buf, maxAge);
62   } else if (typeof path === 'string') {
63     path = resolve(path);
64     stat = fs.statSync(path);
65     if (stat.isDirectory()) throw createIsDirError(path);
66   } else {
67     throw new TypeError('path to favicon.ico must be string or buffer');
68   }
69
70   return function favicon(req, res, next){
71     if (parseUrl(req).pathname !== '/favicon.ico') {
72       next();
73       return;
74     }
75
76     if (req.method !== 'GET' && req.method !== 'HEAD') {
77       res.statusCode = req.method === 'OPTIONS' ? 200 : 405;
78       res.setHeader('Allow', 'GET, HEAD, OPTIONS');
79       res.setHeader('Content-Length', '0');
80       res.end();
81       return;
82     }
83
84     if (icon) return send(req, res, icon);
85
86     fs.readFile(path, function(err, buf){
87       if (err) return next(err);
88       icon = createIcon(buf, maxAge);
89       send(req, res, icon);
90     });
91   };
92 };
93
94 /**
95  * Calculate the max-age from a configured value.
96  *
97  * @private
98  * @param {string|number} val
99  * @return {number}
100  */
101
102 function calcMaxAge(val) {
103   var num = typeof val === 'string'
104     ? ms(val)
105     : val;
106
107   return num != null
108     ? Math.min(Math.max(0, num), maxMaxAge)
109     : maxMaxAge
110 }
111
112 /**
113  * Create icon data from Buffer and max-age.
114  *
115  * @private
116  * @param {Buffer} buf
117  * @param {number} maxAge
118  * @return {object}
119  */
120
121 function createIcon(buf, maxAge) {
122   return {
123     body: buf,
124     headers: {
125       'Cache-Control': 'public, max-age=' + Math.floor(maxAge / 1000),
126       'ETag': etag(buf)
127     }
128   };
129 }
130
131 /**
132  * Create EISDIR error.
133  *
134  * @private
135  * @param {string} path
136  * @return {Error}
137  */
138
139 function createIsDirError(path) {
140   var error = new Error('EISDIR, illegal operation on directory \'' + path + '\'');
141   error.code = 'EISDIR';
142   error.errno = 28;
143   error.path = path;
144   error.syscall = 'open';
145   return error;
146 }
147
148 /**
149  * Send icon data in response to a request.
150  *
151  * @private
152  * @param {IncomingMessage} req
153  * @param {OutgoingMessage} res
154  * @param {object} icon
155  */
156
157 function send(req, res, icon) {
158   var headers = icon.headers;
159
160   // Set headers
161   var keys = Object.keys(headers);
162   for (var i = 0; i < keys.length; i++) {
163     var key = keys[i];
164     res.setHeader(key, headers[key]);
165   }
166
167   if (fresh(req.headers, res._headers)) {
168     res.statusCode = 304;
169     res.end();
170     return;
171   }
172
173   res.statusCode = 200;
174   res.setHeader('Content-Length', icon.body.length);
175   res.setHeader('Content-Type', 'image/x-icon');
176   res.end(icon.body);
177 }