3 * Copyright(c) 2010 Sencha Inc.
4 * Copyright(c) 2011 TJ Holowaychuk
5 * Copyright(c) 2014-2015 Douglas Christopher Wilson
12 * Module dependencies.
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;
29 module.exports = favicon;
36 var maxMaxAge = 60 * 60 * 24 * 365 * 1000; // 1 year
39 * Serves the favicon located by the given `path`.
42 * @param {String|Buffer} path
43 * @param {Object} [options]
44 * @return {Function} middleware
47 function favicon(path, options) {
48 var opts = options || {};
51 var icon; // favicon cache
52 var maxAge = calcMaxAge(opts.maxAge);
55 if (!path) throw new TypeError('path to favicon.ico is required');
57 if (Buffer.isBuffer(path)) {
58 buf = new Buffer(path.length);
61 icon = createIcon(buf, maxAge);
62 } else if (typeof path === 'string') {
64 stat = fs.statSync(path);
65 if (stat.isDirectory()) throw createIsDirError(path);
67 throw new TypeError('path to favicon.ico must be string or buffer');
70 return function favicon(req, res, next){
71 if (parseUrl(req).pathname !== '/favicon.ico') {
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');
84 if (icon) return send(req, res, icon);
86 fs.readFile(path, function(err, buf){
87 if (err) return next(err);
88 icon = createIcon(buf, maxAge);
95 * Calculate the max-age from a configured value.
98 * @param {string|number} val
102 function calcMaxAge(val) {
103 var num = typeof val === 'string'
108 ? Math.min(Math.max(0, num), maxMaxAge)
113 * Create icon data from Buffer and max-age.
116 * @param {Buffer} buf
117 * @param {number} maxAge
121 function createIcon(buf, maxAge) {
125 'Cache-Control': 'public, max-age=' + Math.floor(maxAge / 1000),
132 * Create EISDIR error.
135 * @param {string} path
139 function createIsDirError(path) {
140 var error = new Error('EISDIR, illegal operation on directory \'' + path + '\'');
141 error.code = 'EISDIR';
144 error.syscall = 'open';
149 * Send icon data in response to a request.
152 * @param {IncomingMessage} req
153 * @param {OutgoingMessage} res
154 * @param {object} icon
157 function send(req, res, icon) {
158 var headers = icon.headers;
161 var keys = Object.keys(headers);
162 for (var i = 0; i < keys.length; i++) {
164 res.setHeader(key, headers[key]);
167 if (fresh(req.headers, res._headers)) {
168 res.statusCode = 304;
173 res.statusCode = 200;
174 res.setHeader('Content-Length', icon.body.length);
175 res.setHeader('Content-Type', 'image/x-icon');