diff --git a/request.js b/request.js index eaa866576..124157e86 100644 --- a/request.js +++ b/request.js @@ -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() diff --git a/tests/test-gzip.js b/tests/test-gzip.js index 85870eb4d..cf5ce48ad 100644 --- a/tests/test-gzip.js +++ b/tests/test-gzip.js @@ -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') { @@ -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() {