From dca9208f2a896d28eded959b94ec1adcc8f9fceb Mon Sep 17 00:00:00 2001 From: Eran Hammer Date: Thu, 25 Apr 2013 22:53:40 -0700 Subject: [PATCH] Closes #789 --- lib/response/file.js | 11 ++++++--- lib/response/index.js | 4 +-- lib/response/stream.js | 55 ++++++++++++++++++++++++------------------ 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/lib/response/file.js b/lib/response/file.js index f8d5c3722..883dad93b 100755 --- a/lib/response/file.js +++ b/lib/response/file.js @@ -54,9 +54,6 @@ internals.File.prototype._prepare = function (request, callback) { } var fileName = Path.basename(self._filePath); - var stream = Fs.createReadStream(self._filePath); - - StreamResponse.call(self, stream); self._headers['Content-Type'] = Mime.lookup(self._filePath) || 'application/octet-stream'; self._headers['Content-Length'] = stat.size; @@ -92,3 +89,11 @@ internals.File.prototype._prepare = function (request, callback) { return StreamResponse.prototype._prepare.call(self, request, callback); }); }; + + +internals.File.prototype._transmit = function (request, callback) { + + this._setStream(Fs.createReadStream(this._filePath)); + return StreamResponse.prototype._transmit.call(this, request, callback); +}; + diff --git a/lib/response/index.js b/lib/response/index.js index 660e8e5c3..15dc72c38 100755 --- a/lib/response/index.js +++ b/lib/response/index.js @@ -178,7 +178,7 @@ exports._respond = function (item, request, callback) { request.raw.req.headers['if-none-match'] === etag) { var unchanged = new internals.Empty(); - unchanged._code = 304; + unchanged.code(304); return prepare(unchanged); } @@ -198,7 +198,7 @@ exports._respond = function (item, request, callback) { ifModifiedSince >= lastModified) { var unchanged = new internals.Empty(); - unchanged._code = 304; + unchanged.code(304); return prepare(unchanged); } } diff --git a/lib/response/stream.js b/lib/response/stream.js index d3b5a4760..3e9cadfad 100755 --- a/lib/response/stream.js +++ b/lib/response/stream.js @@ -24,46 +24,53 @@ exports = module.exports = internals.Stream = function (stream) { delete this._payload; this._passThrough = {}; + this._setStream(stream); - if (stream) { + return this; +}; - // Check if stream is a node HTTP response (stream.*) or a (mikeal's) Request object (stream.response.*) +Utils.inherits(internals.Stream, Generic); - if (stream.statusCode || - (stream.response && stream.response.statusCode)) { - this._passThrough.code = stream.statusCode || stream.response.statusCode; - } +internals.Stream.prototype.bytes = function (bytes) { - if (stream.headers || - (stream.response && stream.response.headers)) { + this._headers['Content-Length'] = bytes; + return this; +}; - this._passThrough.headers = stream.headers || stream.response.headers; - } - // Support pre node v0.10 streams API +internals.Stream.prototype._setStream = function (stream) { - if (stream.pipe === Stream.prototype.pipe) { + if (!stream) { + this._stream = null; + return; + } - var oldStream = stream; - oldStream.pause(); - stream = new Stream.Readable().wrap(stream); - oldStream.resume(); - } + // Check if stream is a node HTTP response (stream.*) or a (mikeal's) Request object (stream.response.*) - this._stream = stream; + if (stream.statusCode || + (stream.response && stream.response.statusCode)) { + + this._passThrough.code = stream.statusCode || stream.response.statusCode; } - return this; -}; + if (stream.headers || + (stream.response && stream.response.headers)) { -Utils.inherits(internals.Stream, Generic); + this._passThrough.headers = stream.headers || stream.response.headers; + } + // Support pre node v0.10 streams API -internals.Stream.prototype.bytes = function (bytes) { + if (stream.pipe === Stream.prototype.pipe) { - this._headers['Content-Length'] = bytes; - return this; + var oldStream = stream; + oldStream.pause(); + stream = new Stream.Readable().wrap(stream); + oldStream.resume(); + } + + this._stream = stream; };