Skip to content

Commit

Permalink
Closes #789
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Apr 26, 2013
1 parent efa2194 commit dca9208
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
11 changes: 8 additions & 3 deletions lib/response/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};

4 changes: 2 additions & 2 deletions lib/response/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}
}
Expand Down
55 changes: 31 additions & 24 deletions lib/response/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};


Expand Down

0 comments on commit dca9208

Please sign in to comment.