Skip to content

Commit

Permalink
Adding gzip error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
geek committed Nov 20, 2012
1 parent 0ad3e35 commit 7dbd223
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/response/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var NodeUtil = require('util');
var Zlib = require('zlib');
var Base = require('./base');
var Utils = require('../utils');
var Err = require('../error');


// Declare internals
Expand Down Expand Up @@ -37,17 +38,27 @@ internals.Generic.prototype._transmit = function (request, callback) {

var processGzip = function() {

if (!self._payload || isHeadMethod) {
return send();
}

var rawReq = (request && request.raw && request.raw.req) ? request.raw.req : null;
var acceptEncoding = rawReq && rawReq.headers ? rawReq.headers['accept-encoding'] : null;
var isGzip = acceptEncoding && acceptEncoding.indexOf('gzip') !== -1;

if(!isGzip || isHeadMethod && !self._payload) {
if(!isGzip) {
return send();
}

Zlib.gzip(new Buffer(self._payload), function(err, result) {

if (result) {
if (err) {
var errorResponse = Err.internal('Unable to gzip payload', err).toResponse();

self._code = errorResponse.code;
self._payload = errorResponse.payload;
}
else if (result) {
self._payload = result;
self.header('Content-Encoding', 'gzip');
self.header('Vary', 'Accept-Encoding');
Expand Down

0 comments on commit 7dbd223

Please sign in to comment.