/**
 * Analyzes X-Cache headers from caching servers like Squid or Varnish
 */
exports.version = '0.1';

exports.module = function(phantomas) {
	phantomas.setMetric('cacheHits');
	phantomas.setMetric('cacheMisses');
	phantomas.setMetric('cachePasses');

	var re = /miss|hit|pass/i;
	
	// examples:
	// X-Cache:HIT, HIT
	// X-Cache:arsenic miss (0)
	phantomas.on('recv', function(entry,res) {
		var header = entry.headers['X-Cache'] || '',
			isHit,
			isPass;

		if (re.test(header)) {
		   isHit = header.toLowerCase().indexOf('hit') > -1;
		   if (isHit) {
		        phantomas.incrMetric('cacheHits');
		   }
		   else {
		        isPass = header.toLowerCase().indexOf('pass') > -1;
		        if (isPass) {
		             phantomas.incrMetric('cachePasses');
		             phantomas.log('Cache pass: on <' + entry.url + '> (X-Cache: ' + header + ')');
		        }
		        else {
		             phantomas.incrMetric('cacheMisses');
		             phantomas.log('Cache miss: on <' + entry.url + '> (X-Cache: ' + header + ')');
		        }
		   }
		}
	});
};

@@ -428,16 +428,30 @@
 he'%5D
+.toLowerCase()
  %7C%7C '',%0A
 %09%09%09i
@@ -446,16 +446,16 @@
  %7C%7C '',%0A
-
 %09%09%09isHit
@@ -495,19 +495,17 @@
 r)) %7B%0A%09%09
-   
+%09
 isHit = 
@@ -507,38 +507,24 @@
 it = header.
-toLowerCase().
 indexOf('hit
@@ -534,19 +534,17 @@
 %3E -1;%0A%09%09
-   
+%09
 if (isHi
@@ -550,24 +550,18 @@
 it) %7B%0A%09%09
-        
+%09%09
 phantoma
@@ -589,26 +589,22 @@
 ts');%0A%09%09
-   
+%09
 %7D%0A%09%09
-   
+%09
 else %7B%0A%09
@@ -604,24 +604,18 @@
 lse %7B%0A%09%09
-        
+%09%09
 isPass =
@@ -626,22 +626,8 @@
 der.
-toLowerCase().
 inde
@@ -646,24 +646,18 @@
 %3E -1;%0A%09%09
-        
+%09%09
 if (isPa
@@ -660,37 +660,27 @@
 isPass) %7B%0A%09%09
-             
+%09%09%09
 phantomas.in
@@ -702,37 +702,81 @@
 Passes');%0A%09%09
-             
+%09%09%09phantomas.addOffender('cachePasses', entry.url);%0A%09%09%09%09%09
 phantomas.lo
@@ -848,50 +848,28 @@
 ;%0A%09%09
-        %7D%0A%09%09        else %7B%0A%09%09             
+%09%09%7D%0A%09%09%09%09else %7B%0A%09%09%09%09%09
 phan
@@ -907,21 +907,11 @@
 ;%0A%09%09
-             
+%09%09%09
 phan
@@ -991,23 +991,15 @@
 ;%0A%09%09
-        
+%09%09
 %7D%0A%09%09
-   
+%09
 %7D%0A%09%09

fix lint - add offender for cache passes - remove one toLowerCase call