3 * Copyright(c) 2012 TJ Holowaychuk
4 * Copyright(c) 2014-2015 Douglas Christopher Wilson
12 var debug = require('debug')('send')
13 var deprecate = require('depd')('send')
14 var destroy = require('destroy')
15 var escapeHtml = require('escape-html')
16 , parseRange = require('range-parser')
17 , Stream = require('stream')
18 , mime = require('mime')
19 , fresh = require('fresh')
20 , path = require('path')
21 , http = require('http')
23 , normalize = path.normalize
25 var etag = require('etag')
26 var EventEmitter = require('events').EventEmitter;
27 var ms = require('ms');
28 var onFinished = require('on-finished')
33 var extname = path.extname
34 var maxMaxAge = 60 * 60 * 24 * 365 * 1000; // 1 year
35 var resolve = path.resolve
37 var toString = Object.prototype.toString
38 var upPathRegexp = /(?:^|[\\\/])\.\.(?:[\\\/]|$)/
44 exports = module.exports = send;
53 * Shim EventEmitter.listenerCount for node.js < 0.10
56 /* istanbul ignore next */
57 var listenerCount = EventEmitter.listenerCount
58 || function(emitter, type){ return emitter.listeners(type).length; };
61 * Return a `SendStream` for `req` and `path`.
63 * @param {Request} req
64 * @param {String} path
65 * @param {object} [options]
66 * @return {SendStream}
70 function send(req, path, options) {
71 return new SendStream(req, path, options);
75 * Initialize a `SendStream` with the given `path`.
77 * @param {Request} req
78 * @param {String} path
79 * @param {object} [options]
83 function SendStream(req, path, options) {
84 var opts = options || {}
90 this._etag = opts.etag !== undefined
94 this._dotfiles = opts.dotfiles !== undefined
98 if (['allow', 'deny', 'ignore'].indexOf(this._dotfiles) === -1) {
99 throw new TypeError('dotfiles option must be "allow", "deny", or "ignore"')
102 this._hidden = Boolean(opts.hidden)
104 if (opts.hidden !== undefined) {
105 deprecate('hidden: use dotfiles: \'' + (this._hidden ? 'allow' : 'ignore') + '\' instead')
109 if (opts.dotfiles === undefined) {
110 this._dotfiles = undefined
113 this._extensions = opts.extensions !== undefined
114 ? normalizeList(opts.extensions)
117 this._index = opts.index !== undefined
118 ? normalizeList(opts.index)
121 this._lastModified = opts.lastModified !== undefined
122 ? Boolean(opts.lastModified)
125 this._maxage = opts.maxAge || opts.maxage
126 this._maxage = typeof this._maxage === 'string'
128 : Number(this._maxage)
129 this._maxage = !isNaN(this._maxage)
130 ? Math.min(Math.max(0, this._maxage), maxMaxAge)
133 this._root = opts.root
137 if (!this._root && opts.from) {
143 * Inherits from `Stream.prototype`.
146 SendStream.prototype.__proto__ = Stream.prototype;
149 * Enable or disable etag generation.
151 * @param {Boolean} val
152 * @return {SendStream}
156 SendStream.prototype.etag = deprecate.function(function etag(val) {
158 debug('etag %s', val);
161 }, 'send.etag: pass etag as option');
164 * Enable or disable "hidden" (dot) files.
166 * @param {Boolean} path
167 * @return {SendStream}
171 SendStream.prototype.hidden = deprecate.function(function hidden(val) {
173 debug('hidden %s', val);
175 this._dotfiles = undefined
177 }, 'send.hidden: use dotfiles option');
180 * Set index `paths`, set to a falsy
181 * value to disable index support.
183 * @param {String|Boolean|Array} paths
184 * @return {SendStream}
188 SendStream.prototype.index = deprecate.function(function index(paths) {
189 var index = !paths ? [] : normalizeList(paths);
190 debug('index %o', paths);
193 }, 'send.index: pass index as option');
198 * @param {String} path
199 * @return {SendStream}
203 SendStream.prototype.root = function(path){
205 this._root = resolve(path)
209 SendStream.prototype.from = deprecate.function(SendStream.prototype.root,
210 'send.from: pass root as option');
212 SendStream.prototype.root = deprecate.function(SendStream.prototype.root,
213 'send.root: pass root as option');
216 * Set max-age to `maxAge`.
218 * @param {Number} maxAge
219 * @return {SendStream}
223 SendStream.prototype.maxage = deprecate.function(function maxage(maxAge) {
224 maxAge = typeof maxAge === 'string'
227 if (isNaN(maxAge)) maxAge = 0;
228 if (Infinity == maxAge) maxAge = 60 * 60 * 24 * 365 * 1000;
229 debug('max-age %d', maxAge);
230 this._maxage = maxAge;
232 }, 'send.maxage: pass maxAge as option');
235 * Emit error with `status`.
237 * @param {Number} status
241 SendStream.prototype.error = function(status, err){
243 var msg = http.STATUS_CODES[status];
245 err = err || new Error(msg);
248 // emit if listeners instead of responding
249 if (listenerCount(this, 'error') !== 0) {
250 return this.emit('error', err);
253 // wipe all existing headers
254 res._headers = undefined;
256 res.statusCode = err.status;
261 * Check if the pathname ends with "/".
267 SendStream.prototype.hasTrailingSlash = function(){
268 return '/' == this.path[this.path.length - 1];
272 * Check if this is a conditional GET request.
278 SendStream.prototype.isConditionalGET = function(){
279 return this.req.headers['if-none-match']
280 || this.req.headers['if-modified-since'];
284 * Strip content-* header fields.
289 SendStream.prototype.removeContentHeaderFields = function(){
291 Object.keys(res._headers).forEach(function(field){
292 if (0 == field.indexOf('content')) {
293 res.removeHeader(field);
299 * Respond with 304 not modified.
304 SendStream.prototype.notModified = function(){
306 debug('not modified');
307 this.removeContentHeaderFields();
308 res.statusCode = 304;
313 * Raise error that headers already sent.
318 SendStream.prototype.headersAlreadySent = function headersAlreadySent(){
319 var err = new Error('Can\'t set headers after they are sent.');
320 debug('headers already sent');
321 this.error(500, err);
325 * Check if the request is cacheable, aka
326 * responded with 2xx or 304 (see RFC 2616 section 14.2{5,6}).
332 SendStream.prototype.isCachable = function(){
334 return (res.statusCode >= 200 && res.statusCode < 300) || 304 == res.statusCode;
338 * Handle stat() error.
344 SendStream.prototype.onStatError = function(err){
345 var notfound = ['ENOENT', 'ENAMETOOLONG', 'ENOTDIR'];
346 if (~notfound.indexOf(err.code)) return this.error(404, err);
347 this.error(500, err);
351 * Check if the cache is fresh.
357 SendStream.prototype.isFresh = function(){
358 return fresh(this.req.headers, this.res._headers);
362 * Check if the range is fresh.
368 SendStream.prototype.isRangeFresh = function isRangeFresh(){
369 var ifRange = this.req.headers['if-range'];
371 if (!ifRange) return true;
373 return ~ifRange.indexOf('"')
374 ? ~ifRange.indexOf(this.res._headers['etag'])
375 : Date.parse(this.res._headers['last-modified']) <= Date.parse(ifRange);
379 * Redirect to `path`.
381 * @param {String} path
385 SendStream.prototype.redirect = function(path){
386 if (listenerCount(this, 'directory') !== 0) {
387 return this.emit('directory');
390 if (this.hasTrailingSlash()) return this.error(403);
393 res.statusCode = 301;
394 res.setHeader('Content-Type', 'text/html; charset=utf-8');
395 res.setHeader('Location', path);
396 res.end('Redirecting to <a href="' + escapeHtml(path) + '">' + escapeHtml(path) + '</a>\n');
402 * @param {Stream} res
403 * @return {Stream} res
407 SendStream.prototype.pipe = function(res){
416 var path = decode(this.path)
417 if (path === -1) return this.error(400)
420 if (~path.indexOf('\0')) return this.error(400);
425 if (upPathRegexp.test(normalize('.' + sep + path))) {
426 debug('malicious path "%s"', path)
427 return this.error(403)
430 // join / normalize from optional root dir
431 path = normalize(join(root, path))
432 root = normalize(root + sep)
434 // explode path parts
435 parts = path.substr(root.length).split(sep)
437 // ".." is malicious without "root"
438 if (upPathRegexp.test(path)) {
439 debug('malicious path "%s"', path)
440 return this.error(403)
443 // explode path parts
444 parts = normalize(path).split(sep)
451 if (containsDotFile(parts)) {
452 var access = this._dotfiles
455 if (access === undefined) {
456 access = parts[parts.length - 1][0] === '.'
457 ? (this._hidden ? 'allow' : 'ignore')
461 debug('%s dotfile "%s"', access, path)
466 return this.error(403)
469 return this.error(404)
473 // index file support
474 if (this._index.length && this.path[this.path.length - 1] === '/') {
475 this.sendIndex(path);
486 * @param {String} path
490 SendStream.prototype.send = function(path, stat){
492 var options = this.options
496 var ranges = req.headers.range;
497 var offset = options.start || 0;
500 // impossible to send now
501 return this.headersAlreadySent();
504 debug('pipe "%s"', path)
507 this.setHeader(path, stat);
512 // conditional GET support
513 if (this.isConditionalGET()
516 return this.notModified();
519 // adjust len to start/end options
520 len = Math.max(0, len - offset);
521 if (options.end !== undefined) {
522 var bytes = options.end - offset + 1;
523 if (len > bytes) len = bytes;
528 ranges = parseRange(len, ranges);
531 if (!this.isRangeFresh()) {
532 debug('range stale');
538 debug('range unsatisfiable');
539 res.setHeader('Content-Range', 'bytes */' + stat.size);
540 return this.error(416);
543 // valid (syntactically invalid/multiple ranges are treated as a regular response)
544 if (-2 != ranges && ranges.length === 1) {
545 debug('range %j', ranges);
548 res.statusCode = 206;
549 res.setHeader('Content-Range', 'bytes '
556 offset += ranges[0].start;
557 len = ranges[0].end - ranges[0].start + 1;
562 for (var prop in options) {
563 opts[prop] = options[prop]
568 opts.end = Math.max(offset, offset + len - 1)
571 res.setHeader('Content-Length', len);
574 if ('HEAD' == req.method) return res.end();
576 this.stream(path, opts)
580 * Transfer file for `path`.
582 * @param {String} path
585 SendStream.prototype.sendFile = function sendFile(path) {
589 debug('stat "%s"', path);
590 fs.stat(path, function onstat(err, stat) {
591 if (err && err.code === 'ENOENT'
593 && path[path.length - 1] !== sep) {
594 // not found, check extensions
597 if (err) return self.onStatError(err)
598 if (stat.isDirectory()) return self.redirect(self.path)
599 self.emit('file', path, stat)
600 self.send(path, stat)
604 if (self._extensions.length <= i) {
606 ? self.onStatError(err)
610 var p = path + '.' + self._extensions[i++]
612 debug('stat "%s"', p)
613 fs.stat(p, function (err, stat) {
614 if (err) return next(err)
615 if (stat.isDirectory()) return next()
616 self.emit('file', p, stat)
623 * Transfer index for `path`.
625 * @param {String} path
628 SendStream.prototype.sendIndex = function sendIndex(path){
633 if (++i >= self._index.length) {
634 if (err) return self.onStatError(err);
635 return self.error(404);
638 var p = join(path, self._index[i]);
640 debug('stat "%s"', p);
641 fs.stat(p, function(err, stat){
642 if (err) return next(err);
643 if (stat.isDirectory()) return next();
644 self.emit('file', p, stat);
653 * Stream `path` to the response.
655 * @param {String} path
656 * @param {Object} options
660 SendStream.prototype.stream = function(path, options){
661 // TODO: this is all lame, refactor meeee
662 var finished = false;
668 var stream = fs.createReadStream(path, options);
669 this.emit('stream', stream);
672 // response finished, done with the fd
673 onFinished(res, function onfinished(){
678 // error handling code-smell
679 stream.on('error', function onerror(err){
680 // request already finished
681 if (finished) return;
688 self.onStatError(err);
692 stream.on('end', function onend(){
698 * Set content-type based on `path`
699 * if it hasn't been explicitly set.
701 * @param {String} path
705 SendStream.prototype.type = function(path){
707 if (res.getHeader('Content-Type')) return;
708 var type = mime.lookup(path);
709 var charset = mime.charsets.lookup(type);
710 debug('content-type %s', type);
711 res.setHeader('Content-Type', type + (charset ? '; charset=' + charset : ''));
715 * Set response header fields, most
716 * fields may be pre-defined.
718 * @param {String} path
719 * @param {Object} stat
723 SendStream.prototype.setHeader = function setHeader(path, stat){
726 this.emit('headers', res, path, stat);
728 if (!res.getHeader('Accept-Ranges')) res.setHeader('Accept-Ranges', 'bytes');
729 if (!res.getHeader('Date')) res.setHeader('Date', new Date().toUTCString());
730 if (!res.getHeader('Cache-Control')) res.setHeader('Cache-Control', 'public, max-age=' + Math.floor(this._maxage / 1000));
732 if (this._lastModified && !res.getHeader('Last-Modified')) {
733 var modified = stat.mtime.toUTCString()
734 debug('modified %s', modified)
735 res.setHeader('Last-Modified', modified)
738 if (this._etag && !res.getHeader('ETag')) {
740 debug('etag %s', val)
741 res.setHeader('ETag', val)
746 * Determine if path parts contain a dotfile.
751 function containsDotFile(parts) {
752 for (var i = 0; i < parts.length; i++) {
753 if (parts[i][0] === '.') {
762 * decodeURIComponent.
764 * Allows V8 to only deoptimize this fn instead of all
767 * @param {String} path
771 function decode(path) {
773 return decodeURIComponent(path)
780 * Normalize the index option into an array.
782 * @param {boolean|string|array} val
786 function normalizeList(val){
787 return [].concat(val || [])