Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/AGROSICA/gzippo
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgco committed Jul 20, 2011
2 parents 4708f1e + f0a8c1e commit fb1f4d9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
34 changes: 25 additions & 9 deletions lib/staticGzip.js
Expand Up @@ -82,13 +82,16 @@ exports = module.exports = function staticGzip(dirPath, options){
staticSend(req, res, next, o);
}

function sendGzipped(data) {
function sendGzipped(cacheObj) {
contentType = contentType + (charset ? '; charset=' + charset : '');
res.setHeader('Content-Type', contentType);
res.setHeader('Content-Encoding', 'gzip');
res.setHeader('Vary', 'Accept-Encoding');
res.setHeader('Content-Length', data.length);
res.end(data, 'binary');
res.setHeader('Content-Length', cacheObj.content.length);
res.setHeader('Last-Modified', cacheObj.mtime);
res.setHeader('Date', (new Date()).toUTCString());
res.setHeader('Expires', (new Date((new Date()).getTime()+maxAge)).toUTCString());
res.end(cacheObj.content, 'binary');
}

function gzipAndSend(filename, gzipName, mtime) {
Expand All @@ -98,7 +101,7 @@ exports = module.exports = function staticGzip(dirPath, options){
'mtime': mtime,
'content': gzippedData
};
sendGzipped(gzippedData);
sendGzipped(gzippoCache[gzipName]);
});
}

Expand All @@ -121,7 +124,7 @@ exports = module.exports = function staticGzip(dirPath, options){
if (!~acceptEncoding.indexOf('gzip')) {
return pass(filename);
}

//This is storing in memory for the moment, need to think what the best way to do this.
//Check file is not a directory

Expand All @@ -133,19 +136,32 @@ exports = module.exports = function staticGzip(dirPath, options){
var base = path.basename(filename),
dir = path.dirname(filename),
gzipName = path.join(dir, base + '.gz');

if (req.headers['if-modified-since'] &&
gzippoCache[gzipName] &&
(new Date(gzippoCache[gzipName].mtime)).getTime() <= (new Date(req.headers['if-modified-since'])).getTime()) {
contentType = contentType + (charset ? '; charset=' + charset : '');
res.setHeader('Content-Type', contentType);
res.setHeader('Content-Encoding', 'gzip');
res.setHeader('Vary', 'Accept-Encoding');
res.setHeader('Last-Modified', gzippoCache[gzipName].mtime);
res.setHeader('Date', (new Date()).toUTCString());
res.setHeader('Expires', (new Date((new Date()).getTime()+maxAge)).toUTCString());
return res.send(304);
}

//check for pre-compressed file
//TODO: Look into placing into a loop and using dot notation for speed improvements.
if (typeof gzippoCache[gzipName] === 'undefined') {
gzipAndSend(filename, gzipName, stat.mtime);
gzipAndSend(filename, gzipName, (new Date(stat.mtime)).toUTCString());
} else {
if ((gzippoCache[gzipName].mtime < stat.mtime) ||
((gzippoCache[gzipName].ctime + maxAge) < Date.now())) {
gzipAndSend(filename, gzipName, stat.mtime);
gzipAndSend(filename, gzipName, (new Date(stat.mtime)).toUTCString());
} else {
sendGzipped(gzippoCache[gzipName].content);
sendGzipped(gzippoCache[gzipName]);
}
}
});
};
};
};
26 changes: 25 additions & 1 deletion test/staticGzipTest.js
Expand Up @@ -109,5 +109,29 @@ module.exports = {
res.headers.should.have.property('content-encoding', 'gzip');
}
);
},
'requesting gzipped utf-8 file second time caches': function() {
assert.response(app,
{
url: '/utf8.txt',
headers: {
'Accept-Encoding':"gzip",
}
},
function(res) {
assert.response(app,
{
url: '/utf8.txt',
headers: {
'Accept-Encoding':"gzip",
'If-Modified-Since': 'Mon, 28 Dec 2020 01:00:00 GMT'
}
},
function(res) {
res.statusCode.should.equal(304);
}
);
}
);
}
};
};

0 comments on commit fb1f4d9

Please sign in to comment.