Skip to content
Browse files

changed the etag calculation to use md5 of the file instead of time+ …

…content length. should provide more reliable etag
  • Loading branch information...
1 parent aa0b6cd commit 36f0553a5b13175bf31a9ae2c45daf553ec2a883 Dov Amihod committed Nov 20, 2012
Showing with 13 additions and 5 deletions.
  1. +13 −5 lib/staticGzip.js
View
18 lib/staticGzip.js
@@ -13,6 +13,7 @@ var fs = require('fs'),
path = require('path'),
mime = require('mime'),
zlib = require('zlib'),
+ crypto = require('crypto'),
staticSend;
try {
staticSend = require('connect').static.send;
@@ -115,10 +116,10 @@ exports = module.exports = function staticGzip(dirPath, options){
res.setHeader('Content-Length', cacheObj.content.length);
res.setHeader('Last-Modified', cacheObj.mtime.toUTCString());
res.setHeader('Date', new Date().toUTCString());
- res.setHeader('Expires', new Date(Date.now() + clientMaxAge).toUTCString());
- res.setHeader('Cache-Control', 'public, max-age=' + (clientMaxAge / 1000));
- res.setHeader('ETag', '"' + cacheObj.content.length + '-' + Number(cacheObj.mtime) + '"');
- }
+ res.setHeader('Expires', new Date(Date.now() + clientMaxAge).toUTCString());
+ res.setHeader('Cache-Control', 'public, max-age=' + (clientMaxAge / 1000));
+ res.setHeader('ETag', '"' + cacheObj.etag + '"');
+ }
function sendGzipped(cacheObj) {
setHeaders(cacheObj);
@@ -127,10 +128,17 @@ exports = module.exports = function staticGzip(dirPath, options){
function gzipAndSend(filename, gzipName, mtime) {
gzippo(filename, charset, function(gzippedData) {
+
+ var etag;
+ var md5 = crypto.createHash('md5');
+ md5.update(gzippedData);
+ etag = md5.digest('base64');
+
gzippoCache[gzipName] = {
'ctime': Date.now(),
'mtime': mtime,
- 'content': gzippedData
+ 'etag' : etag,
+ 'content': gzippedData
};
sendGzipped(gzippoCache[gzipName]);
});

0 comments on commit 36f0553

Please sign in to comment.
Something went wrong with that request. Please try again.