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

Adding support for gzip responses #255

Merged
merged 3 commits into from
Nov 20, 2012
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions lib/response/generic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Load modules

var NodeUtil = require('util');
var Zlib = require('zlib');
var Base = require('./base');
var Utils = require('../utils');

Expand Down Expand Up @@ -31,10 +32,41 @@ NodeUtil.inherits(internals.Generic, Base);

internals.Generic.prototype._transmit = function (request, callback) {

request.raw.res.writeHead(this._code, this.headers);
request.raw.res.end(request.method !== 'head' ? this._payload : '');
var self = this;
var isHeadMethod = request.method === 'head';

return callback();
var processGzip = function() {

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

condition is not clear (using both || and && in one line)

return send();
}

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

if (result) {
self._payload = result;
self.header('Content-Encoding', 'gzip');
self.header('Vary', 'Accept-Encoding');
self.header('Content-Length', result.length);
}

return send();
});
};

var send = function() {

request.raw.res.writeHead(self._code, self.headers);
request.raw.res.end(!isHeadMethod ? self._payload : '');

return callback();
};

processGzip();
};


Expand Down Expand Up @@ -71,4 +103,4 @@ internals.Generic.prototype.ttl = function (ttl) {

this.options.ttl = ttl;
return this;
};
};
42 changes: 38 additions & 4 deletions test/integration/gzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ var expect = require('chai').expect;
var libPath = process.env.TEST_COV ? '../../lib-cov/' : '../../lib/';
var Hapi = require(libPath + 'hapi');
var Zlib = require('zlib');
var Request = require('request');


describe('Payload', function () {

var server = new Hapi.Server('0.0.0.0', 8080);
var server = new Hapi.Server('0.0.0.0', 17080);
var message = { 'msg': 'This message is going to be gzipped.' };
var badMessage = '{ this is just wrong }';

Expand All @@ -18,7 +19,7 @@ describe('Payload', function () {
config: {
handler: function (req) {

req.reply(req.payload)
req.reply(req.payload);
}
}
};
Expand Down Expand Up @@ -70,7 +71,7 @@ describe('Payload', function () {
expect(res.result).to.exist;
expect(res.result).to.deep.equal(message);
done();
});;
});
});

it('returns error if given non-JSON gzipped payload when expecting gzip', function (done) {
Expand All @@ -97,5 +98,38 @@ describe('Payload', function () {
});
});
});
});

it('returns a gzip response when accept-encoding: gzip is requested', function(done) {

var rawBody = '{"test":"true"}';

Zlib.gzip(new Buffer(rawBody), function(err, zippedBody) {

server.start(function() {

Request.post({ url: 'http://localhost:17080', headers: { 'accept-encoding': 'gzip' }, body: rawBody }, function(err, res, body) {

expect(body).to.equal(zippedBody.toString());
done();
});
});
});
});

it('returns a gzip response when accept-encoding: deflate,gzip is requested', function(done) {

var rawBody = '{"test":"true"}';

Zlib.gzip(new Buffer(rawBody), function(err, zippedBody) {

server.start(function() {

Request.post({ url: 'http://localhost:17080', headers: { 'accept-encoding': 'deflate, gzip' }, body: rawBody }, function(err, res, body) {

expect(body).to.equal(zippedBody.toString());
done();
});
});
});
});
});