diff --git a/request.js b/request.js index 1339435df..f8c33f49f 100644 --- a/request.js +++ b/request.js @@ -1120,56 +1120,58 @@ Request.prototype.onRequestResponse = function (response) { self._ended = true }) - var dataStream + var responseContent if (self.gzip) { var contentEncoding = response.headers['content-encoding'] || 'identity' contentEncoding = contentEncoding.trim().toLowerCase() if (contentEncoding === 'gzip') { - dataStream = zlib.createGunzip() - response.pipe(dataStream) + responseContent = zlib.createGunzip() + response.pipe(responseContent) } else { // Since previous versions didn't check for Content-Encoding header, // ignore any invalid values to preserve backwards-compatibility if (contentEncoding !== 'identity') { debug('ignoring unrecognized Content-Encoding ' + contentEncoding) } - dataStream = response + responseContent = response } } else { - dataStream = response + responseContent = response } if (self.encoding) { if (self.dests.length !== 0) { console.error('Ignoring encoding parameter as this stream is being piped to another stream which makes the encoding option invalid.') - } else if (dataStream.setEncoding) { - dataStream.setEncoding(self.encoding) + } else if (responseContent.setEncoding) { + responseContent.setEncoding(self.encoding) } else { // Should only occur on node pre-v0.9.4 (joyent/node@9b5abe5) with // zlib streams. // If/When support for 0.9.4 is dropped, this should be unnecessary. - dataStream = dataStream.pipe(stringstream(self.encoding)) + responseContent = responseContent.pipe(stringstream(self.encoding)) } } + self.responseContent = responseContent + self.emit('response', response) self.dests.forEach(function (dest) { self.pipeDest(dest) }) - dataStream.on('data', function (chunk) { + responseContent.on('data', function (chunk) { self._destdata = true self.emit('data', chunk) }) - dataStream.on('end', function (chunk) { + responseContent.on('end', function (chunk) { self.emit('end', chunk) }) - dataStream.on('error', function (error) { + responseContent.on('error', function (error) { self.emit('error', error) }) - dataStream.on('close', function () {self.emit('close')}) + responseContent.on('close', function () {self.emit('close')}) if (self.callback) { var buffer = bl() @@ -1536,18 +1538,18 @@ Request.prototype.end = function (chunk) { } Request.prototype.pause = function () { var self = this - if (!self.response) { + if (!self.responseContent) { self._paused = true } else { - self.response.pause.apply(self.response, arguments) + self.responseContent.pause.apply(self.responseContent, arguments) } } Request.prototype.resume = function () { var self = this - if (!self.response) { + if (!self.responseContent) { self._paused = false } else { - self.response.resume.apply(self.response, arguments) + self.responseContent.resume.apply(self.responseContent, arguments) } } Request.prototype.destroy = function () {