Skip to content
Browse files

Play with cacheability, etag instead of last-modified.

  • Loading branch information...
1 parent 65213e8 commit f0a7ec17915956bba261dbcc39fcab5a96e307af @majek majek committed Aug 8, 2011
Showing with 31 additions and 5 deletions.
  1. +31 −5 bin/simple_http_server.js
View
36 bin/simple_http_server.js
@@ -3,6 +3,7 @@ var url = require('url');
var fs = require('fs');
var path = require('path');
var compress = require('compress');
+var crypto = require('crypto');
var port = 8000;
@@ -20,8 +21,15 @@ var mimetypes = {
gif: 'image/gif'
};
+var md5_hex = function (data) {
+ return crypto.createHash('md5').update(data).digest('hex');
+};
+
+
var compressable_ct = ['text', 'application'];
+var cacheable = true;
+
var req_fin = function(statusCode, req, res, content, encoding) {
var gz = '';
if (!encoding) encoding = 'utf-8';
@@ -36,10 +44,21 @@ var req_fin = function(statusCode, req, res, content, encoding) {
gz = '(gzip)';
encoding = 'binary';
}
- // According to: http://code.google.com/speed/page-speed/docs/caching.html
- // 'vary' effectively disables caching for IE. IE users are
- // screwed anyway.
- res.setHeader('Vary', 'Accept-Encoding');
+ if (content) {
+ // According to: http://code.google.com/speed/page-speed/docs/caching.html
+ // 'vary' effectively disables caching for IE. IE users are
+ // screwed anyway.
+ res.setHeader('Vary', 'Accept-Encoding');
+ }
+
+ if (cacheable) {
+ var cache_for = 365 * 24 * 60 * 60; // one year.
+ // See: http://code.google.com/speed/page-speed/docs/caching.html
+ res.setHeader('Cache-Control', 'public, max-age=' + cache_for);
+ var exp = new Date();
+ exp.setTime(exp.getTime() + cache_for * 1000);
+ res.setHeader('Expires', exp.toGMTString());
+ };
var cl = '';
if (content) {
@@ -52,6 +71,7 @@ var req_fin = function(statusCode, req, res, content, encoding) {
res.writeHead(statusCode);
res.end(content, encoding);
} catch (x) {}
+
console.log(req.method, req.url, statusCode, cl, gz);
};
@@ -80,13 +100,19 @@ var handler = function(req, res) {
req_fin(304, req, res);
return;
}
- res.setHeader('Last-Modified', last_modified);
+ // res.setHeader('Last-Modified', last_modified);
fs.readFile(filename, fs_decorator(req, res, cb2));
};
cb2 = function (content) {
+ var md5 = '"' + md5_hex(content) + '"';
+ if (req.headers['if-none-match'] === md5) {
+ req_fin(304, req, res);
+ return;
+ }
var mimetype = mimetypes[path.extname(filename).slice(1)] || 'text/plain';
mimetype += '; charset=UTF-8';
res.setHeader('Content-Type', mimetype);
+ res.setHeader('ETag', md5);
req_fin(200, req, res, content);
};
fs.stat(filename, fs_decorator(req, res, cb1));

0 comments on commit f0a7ec1

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