Started Request#stream() based of Aarons work on Static#streamfile()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 28df150 · Browse files · 3 files·+77 added−39 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+77 added−39 removed
- other:+77 added−39 removed
Provenance
- Commit
2ccf876- Tree
4d93bc2- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
28df150
3 files changed+77 added−39 removed
modified:lib/express/plugins/content-length.js+4 added-3 removed
8 unmodified lines9101112131314151415161718198 unmodified lines */ response: function(event) { var response = event.request.response if (!response.chunkedEncoding) response.headers['content-length'] = response.headers['content-length'] || response.body.length response.headers['content-length'] = response.headers['content-length'] || response.body.length } }})No newline at end of fileNo newline at end of filemodified:lib/express/plugins/static.js+31 added-36 removed
20 unmodified lines212223242525262728293031323334353632383334353643444546474849505152535455565758596061626364656667686937383940414243444546474849505152535455565758596061626364656634 unmodified lines10110210310411211310510610710810911011111214 unmodified lines20 unmodified lines */ constructor: function(path) { this.path = path if (path.indexOf('..') != -1) if (path.indexOf('..') !== -1) Error.raise('InvalidPathError', "`" + path + "' is not a valid path") }, /** * Transfer static file to the given _request_. * * - Ensures the file exists * - Ensures the file is a regular file (not FIFO, Socket, etc) * - Automatically assigns content type * - Halts with 404 when failing * * @param {Request} request * @settings 'cache static files' * @api public */ sendTo: function(request) { var file = this.path function sendFromDisc() { path.exists(file, function(exists){ if (!exists) return request.notFound() fs.stat(file, function(err, stats){ if (err) throw err if (!stats.isFile()) return request.notFound() fs.readFile(file, 'binary', function(err, content){ if (err) throw err request.contentType(file) if (set('cache static files')) request.cache.set('static:' + file, { type: file, content: content }) request.halt(200, content, 'binary') }) }) }) } if (set('cache static files')) request.cache.get('static:' + file, function(cache){ if (cache) request.contentType(cache.type), request.halt(200, cache.content, 'binary') else sendFromDisc() }) else sendFromDisc() // function sendFromDisc() { // path.exists(file, function(exists){ // if (!exists) return request.notFound() // fs.stat(file, function(err, stats){ // if (err) throw err // if (!stats.isFile()) return request.notFound() // fs.readFile(file, 'binary', function(err, content){ // if (err) throw err // request.contentType(file) // if (set('cache static files')) // request.cache.set('static:' + file, { type: file, content: content }) // request.halt(200, content, 'binary') // }) // }) // }) // } // if (set('cache static files')) // request.cache.get('static:' + file, function(cache){ // if (cache) // request.contentType(cache.type), // request.halt(200, cache.content, 'binary') // else // sendFromDisc() // }) // else // sendFromDisc() }})
// --- Static34 unmodified lines * @api public */
sendfile: function(path) { (new exports.File(path)).sendTo(this) return this if (path.indexOf('..') !== -1) Error.raise('InvalidPathError', "`" + path + "' is not a valid path") this.response.chunkedEncoding = true return this.stream(fs.createReadStream(path)) }, /** * Transfer static _file_ as an attachment.14 unmodified linesmodified:lib/express/request.js+42 added
199 unmodified lines200 }, true)201 },202 203 /**204+ * Transfer the given _stream_.205+ *206+ * @param {Stream} stream207+ * @return {Request}208+ * @api public209+ */210+ 211+ stream: function(stream, callback) {212+ var self = this,213+ first = true214+ stream215+ .addListener('error', function(err){216+ if (first)217+ return 'errno' in err && err.errno === 2218+ ? self.notFound()219+ : self.error(err)220+ // TODO: err wtf ? bad file descriptor apparently...221+ self.response.end()222+ })223+ .addListener('data', function(data){224+ if (first) {225+ self.status(200)226+ first = false227+ self.contentType(stream.path)228+ self.response.writeHeader(self.response.status, self.response.headers)229+ self.response.write(data, 'binary')230+ } else231+ self.response.write(data, 'binary')232+ })233+ .addListener('end', function(){234+ self.response.end()235+ self.trigger('response', function(err){236+ if (err) {237+ stream.destroy()238+ if (callback !== undefined) callback(err)239+ else self.error(err)240+ }241+ }, true)242+ })243+ },244+ 245+ /**246 * Pass control to the next matching route, or247 * the given _path_.248 *249 * NOTE: _path_ may be the request pathname only,142 unmodified lines