From edd34ebbf7d2d7dcf222c40398a8631c4cfbecfa Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Mon, 26 Mar 2012 11:12:34 -0600
Subject: [PATCH] use etagify for content-based hashes in ETag headers - issue
 #1331

---
 config/local.json      |  4 ++--
 lib/browserid/views.js | 36 ++++++++++++++++--------------------
 package.json           |  1 +
 3 files changed, 19 insertions(+), 22 deletions(-)

diff --git a/config/local.json b/config/local.json
index 960bdf2e9..c85c8f828 100644
--- a/config/local.json
+++ b/config/local.json
@@ -9,6 +9,6 @@
     "driver": "json"
   },
   "express_log_format": "dev_bid",
-  "email_to_console": true
+  "email_to_console": true,
+  "env": "local"
 }
-
diff --git a/lib/browserid/views.js b/lib/browserid/views.js
index 0ca111100..9380f9b5d 100644
--- a/lib/browserid/views.js
+++ b/lib/browserid/views.js
@@ -11,14 +11,14 @@ connect = require('connect'),
 config = require('../configuration.js'),
 und = require('underscore'),
 util = require('util'),
-httputils = require('../httputils.js');
+httputils = require('../httputils.js'),
+etagify = require('etagify');
 
 // all templated content, redirects, and renames are handled here.
 // anything that is not an api, and not static
 const
 path = require('path');
 
-
 const VIEW_PATH = path.join(__dirname, "..", "..", "resources", "views");
 
 // none of our views include dynamic data.  all of them should be served
@@ -26,27 +26,23 @@ const VIEW_PATH = path.join(__dirname, "..", "..", "resources", "views");
 // cache headers maximally leveraging the same logic that connect uses
 // issue #910
 function renderCachableView(req, res, template, options) {
-  fs.stat(path.join(VIEW_PATH, template), function (err, stat) {
-    res.setHeader('Date', new Date().toUTCString());
-    res.setHeader('Vary', 'Accept-Encoding,Accept-Language');
-    if (config.get('env') !== 'local') {
-      // allow caching, but require revalidation via ETag
-      res.setHeader('Cache-Control', 'public, max-age=0');
-      res.setHeader('ETag', util.format('"%s-%s-%s"', stat.size, Number(stat.mtime), req.lang));
-    } else {
-      res.setHeader('Cache-Control', 'no-store');
-    }
-    res.setHeader('Content-Type', 'text/html; charset=utf8');
-    if (connect.utils.conditionalGET(req)) {
-      if (!connect.utils.modified(req, res)) {
-        return connect.utils.notModified(res);
-      }
-    }
-    res.render(template, options);
-  });
+  if (config.get('env') !== 'local') {
+    // allow caching, but require revalidation via ETag
+    res.etagify();
+    res.setHeader('Cache-Control', 'public, max-age=0');
+  } else {
+    // disable all caching for local dev
+    res.setHeader('Cache-Control', 'no-store');
+  }
+  res.setHeader('Date', new Date().toUTCString());
+  res.setHeader('Vary', 'Accept-Encoding,Accept-Language');
+  res.setHeader('Content-Type', 'text/html; charset=utf8');
+  res.render(template, options);
 }
 
 exports.setup = function(app) {
+  app.use(etagify());
+
   app.set("views", VIEW_PATH);
 
   app.set('view options', {
diff --git a/package.json b/package.json
index 2a86e4b4d..ad3bd2c8b 100644
--- a/package.json
+++ b/package.json
@@ -14,6 +14,7 @@
         "connect-cookie-session": "0.0.2",
         "connect-logger-statsd": "0.0.1",
         "ejs": "0.4.3",
+        "etagify": "0.0.1",
         "express": "2.5.0",
         "iconv": "1.1.3",
         "jwcrypto": "0.1.1",
-- 
GitLab