Added ETag support to Request#sendfile()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent fec57df · Browse files · 2 files·+27 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+27 added−7 removed
- other:+27 added−7 removed
Provenance
- Commit
e994345- Tree
7a91409- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
fec57df
2 files changed+27 added−7 removed
modified:lib/express/plugins/static.js+25 added-4 removed
38 unmodified lines394041424344434445464748495051525253545556575859606162636465666768697071727374567576777879808117 unmodified lines38 unmodified lines Request.include({ /** * Transfer static file at the given _path_. All _options_ * are passed to fs.createReadStream(). * Transfer static file at the given _path_ with optional _callback_. * _options_ are passed to fs.createReadStream(). * * @param {string} path * @param {object} options * @param {function} callback * @return {Request} * @api public */
sendfile: function(path, options) { sendfile: function(path, options, callback) { var self = this if (options instanceof Function) callback = options, options = {} if (path.indexOf('..') !== -1) Error.raise('InvalidPathError', "`" + path + "' is not a valid path") this.response.chunkedEncoding = true fs.stat(path, function(err, stat){ if (err) if (callback !== undefined) return callback(err) else return 'errno' in err && err.errno === 2 ? self.notFound() : self.error(err) var etag = stat.ino + '-' + stat.size + '-' + Number(stat.mtime) if (self.header('if-none-match') && self.header('if-none-match') == etag) return self.halt(304) self.header('Content-Length', stat.size) self.header('ETag', etag) return this.stream(fs.createReadStream(path, options)) self.stream(fs.createReadStream(path, options)) }) return this }, /** * Transfer static _file_ as an attachment.17 unmodified linesmodified:lib/express/request.js+2 added-3 removed
202 unmodified lines203 /**204 * Transfer the given _stream_ with optional _callback_.205 *206 * @param {Stream} stream207+ * @param {function} callback208 * @return {Request}209 * @api public210 */211 2 unmodified lines214 first = true215 stream216 .addListener('error', function(err){217 if (first)217- return 'errno' in err && err.errno === 2218- ? self.notFound()219- : self.error(err)218+ return self.error(err)219 self.response.end()220 if (callback !== undefined) callback(err)221 })222 .addListener('data', function(data){167 unmodified lines