diff --git a/lib/staticGzip.js b/lib/staticGzip.js index 400f385..baa0c88 100644 --- a/lib/staticGzip.js +++ b/lib/staticGzip.js @@ -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) { @@ -98,7 +101,7 @@ exports = module.exports = function staticGzip(dirPath, options){ 'mtime': mtime, 'content': gzippedData }; - sendGzipped(gzippedData); + sendGzipped(gzippoCache[gzipName]); }); } @@ -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 @@ -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]); } } }); }; -}; \ No newline at end of file +}; diff --git a/test/staticGzipTest.js b/test/staticGzipTest.js index ff3723a..8ad73db 100644 --- a/test/staticGzipTest.js +++ b/test/staticGzipTest.js @@ -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); + } + ); + } + ); } -}; \ No newline at end of file +};