Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stall when piping gzipped response #1568

Merged
merged 1 commit into from May 8, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 18 additions & 16 deletions request.js
Expand Up @@ -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()
Expand Down Expand Up @@ -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 () {
Expand Down