Skip to content

Instantly share code, notes, and snippets.

@traut
Created November 23, 2010 15:50
Show Gist options
  • Save traut/711967 to your computer and use it in GitHub Desktop.
Save traut/711967 to your computer and use it in GitHub Desktop.
var compress = require('./lib/compress');
var sys = require('sys');
exports.gzip = function(request, response, result) {
var encoding = 'gzip';
if ((request.headers['accept-encoding'] || '').indexOf(encoding) >= 0) {
var contentType = result.headers["Content-Type"];
contentType = contentType && contentType.split("/")[0];
if (!result.headers['Content-Encoding'] && // skip if already encoded
result.status != 204 && // of if no body
(contentType == "application" || contentType == "text")) { // or if not an acceptable type
var zipper = new compress.Gzip;
zipper.init();
var zip = zipper.deflate(result.body, 'utf-8') + zipper.end();
result.headers['Content-Encoding'] = encoding;
result.headers['Content-Length'] = zip.length;
response.writeHead(result.status, result.headers);
response.write(zip, 'binary');
response.end();
return;
}
}
sys.log('No gzip, client: ' + request.headers['user-agent'] + '. Sending raw ' + result.headers['Content-Type']);
response.writeHead(result.status, result.headers);
response.end(result.body);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment