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

Do not try to pipe Gzip responses with no body #2176

Merged
merged 2 commits into from Apr 17, 2016
Merged
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion request.js
Expand Up @@ -920,8 +920,20 @@ Request.prototype.onRequestResponse = function (response) {
self._ended = true
})

var noBody = function (code) {
return (
self.method === 'HEAD'
// Informational
|| (code >= 100 && code < 200)
// No Content
|| code === 204
// Not Modified
|| code === 304
)
}

var responseContent
if (self.gzip) {
if (self.gzip && !noBody(response.statusCode)) {
var contentEncoding = response.headers['content-encoding'] || 'identity'
contentEncoding = contentEncoding.trim().toLowerCase()

Expand Down
51 changes: 51 additions & 0 deletions tests/test-gzip.js
Expand Up @@ -16,6 +16,20 @@ var server = http.createServer(function(req, res) {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')

if (req.method === 'HEAD') {
res.setHeader('Content-Encoding', 'gzip')
res.end()
return
}
if (req.headers.code) {
res.writeHead(req.headers.code, {
'Content-Encoding': 'gzip',
code: req.headers.code
})
res.end()
return
}

if (/\bgzip\b/i.test(req.headers['accept-encoding'])) {
res.setHeader('Content-Encoding', 'gzip')
if (req.url === '/error') {
Expand Down Expand Up @@ -226,6 +240,43 @@ tape('transparently supports deflate decoding to callbacks', function(t) {
})
})

tape('do not try to pipe HEAD request responses', function(t) {
var options = { method: 'HEAD', url: 'http://localhost:6767/foo', gzip: true }

request(options, function(err, res, body) {
t.equal(err, null)
t.equal(body, '')
t.end()
})
})

tape('do not try to pipe responses with no body', function(t) {
var options = { url: 'http://localhost:6767/foo', gzip: true }

options.headers = {code: 105}
request.post(options, function(err, res, body) {
t.equal(err, null)
t.equal(res.headers.code, '105')
t.equal(body, '')

options.headers = {code: 204}
request.post(options, function(err, res, body) {
t.equal(err, null)
t.equal(res.headers.code, '204')
t.equal(body, '')

options.headers = {code: 304}
request.post(options, function(err, res, body) {
t.equal(err, null)
t.equal(res.headers.code, '304')
t.equal(body, '')

t.end()
})
})
})
})


tape('cleanup', function(t) {
server.close(function() {
Expand Down