Skip to content

Commit

Permalink
Merge pull request actionhero#949 from aurasalexander/fix_compress_ie
Browse files Browse the repository at this point in the history
Respect accept-encoding priority order as set by request header
  • Loading branch information
evantahler committed Sep 27, 2016
2 parents 154e3b0 + b9b5c67 commit dfc84c5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
13 changes: 8 additions & 5 deletions servers/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,20 @@ const initialize = function(api, options, next){
// https://nodejs.org/api/zlib.html#zlib_zlib_createinflate_options
// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
if(api.config.servers.web.compress === true){
if(acceptEncoding.match(/\bdeflate\b/)){
headers.push(['Content-Encoding', 'deflate']);
compressor = zlib.createDeflate();
stringEncoder = zlib.deflate;
}else if(acceptEncoding.match(/\bgzip\b/)){
let gzipMatch = acceptEncoding.match(/\bgzip\b/);
let deflateMatch = acceptEncoding.match(/\bdeflate\b/);
if((gzipMatch && !deflateMatch) || (gzipMatch && deflateMatch && gzipMatch.index < deflateMatch.index)){
headers.push(['Content-Encoding', 'gzip']);
compressor = zlib.createGzip();
stringEncoder = zlib.gzip;
}else if((!gzipMatch && deflateMatch) || (gzipMatch && deflateMatch && deflateMatch.index < gzipMatch.index)){
headers.push(['Content-Encoding', 'deflate']);
compressor = zlib.createDeflate();
stringEncoder = zlib.deflate;
}
}


// the 'finish' event deontes a successful transfer
connection.rawConnection.res.on('finish', () => {
connection.destroy();
Expand Down
59 changes: 59 additions & 0 deletions test/core/staticFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,63 @@ describe('Core: Static File', function(){
});
});

describe('Core: Static File -> Compression Tests', function() {
var serverCompressionState;
before(function(done) {
serverCompressionState = api.config.servers.web.compress
api.config.servers.web.compress = true; //activate compression, default is likely to be false
done();
})

after(function(done) {
api.config.servers.web.compress = serverCompressionState;
done();
});

it('should find the compression configuration in servers web config', function(done){
serverCompressionState.should.be.a.Boolean();
done();
});

it('should respect accept-encoding header priority with gzip as first in a list of encodings', function(done){
request.get({url:url + '/simple.html', headers:{'Accept-Encoding':'gzip, deflate, sdch, br'}}, function(error, response, body){
response.statusCode.should.eql(200);
response.headers['content-encoding'].should.equal('gzip');
done();
});
});

it('should respect accept-encoding header priority with deflate as second in a list of encodings', function(done){
request.get({url:url + '/simple.html', headers:{'Accept-Encoding':'br, deflate, gzip'}}, function(error, response, body){
response.statusCode.should.eql(200);
response.headers['content-encoding'].should.equal('deflate'); //br is not a currently supported encoding
done();
});
});

it('should respect accept-encoding header priority with gzip as only option', function(done){
request.get({url:url + '/simple.html', headers:{'Accept-Encoding':'gzip'}}, function(error, response, body){
response.statusCode.should.eql(200);
response.headers['content-encoding'].should.equal('gzip');
done();
});
});

it('should\'nt encode content without a valid a supported value in accept-encoding header', function(done){
request.get({url:url + '/simple.html', headers:{'Accept-Encoding':'sdch, br'}}, function(error, response, body){
response.statusCode.should.eql(200);
should.not.exist(response.headers['content-encoding']);
done();
});
});

it('should\'nt encode content without accept-encoding header', function(done){
request.get({url:url + '/simple.html'}, function(error, response, body){
response.statusCode.should.eql(200);
should.not.exist(response.headers['content-encoding']);
done();
});
});

});
});

0 comments on commit dfc84c5

Please sign in to comment.