r9831 - helma-ng/trunk/modules/helma/middleware

[email protected] Tue, 16 Jun 2009 15:51:51 +0200 (CEST)
Newsgroups gmane.comp.java.helma.cvs
Message-ID <20090616135151.0E5913D0E3@mia>
Author: hannes
Date: 2009-06-16 15:51:50 +0200 (Tue, 16 Jun 2009)
New Revision: 9831

Modified:
   helma-ng/trunk/modules/helma/middleware/gzip.js
Log:
Try to be smarter about which responses to encode

Details at http://dev.helma.org/trac/helma/changeset/9831

Modified: helma-ng/trunk/modules/helma/middleware/gzip.js
===================================================================
--- helma-ng/trunk/modules/helma/middleware/gzip.js	2009-06-16 13:51:49 UTC (rev 9830)
+++ helma-ng/trunk/modules/helma/middleware/gzip.js	2009-06-16 13:51:50 UTC (rev 9831)
@@ -11,10 +11,11 @@
  * @return the HTTP response object
  */
 function handleRequest(req) {
-    var header = req.getHeader("accept-encoding");
     var res = req.process();
     var [status, headers, body] = res;
-    if (status == 200 && header &&  header.indexOf('gzip') > -1) {
+    if (canCompress(status,
+            req.getHeader("accept-encoding"),
+            HashP.get(headers, 'content-type'))) {
         var bytes = new ByteArrayOutputStream();
         var gzip = new GZIPOutputStream(bytes);
         body.forEach(function(block) {
@@ -26,4 +27,10 @@
         HashP.set(headers, 'Content-Encoding', 'gzip');
     }
     return res;
+}
+
+function canCompress(status, acceptEncoding, contentType) {
+    return status == 200 && acceptEncoding &&
+           acceptEncoding.indexOf('gzip') > -1 && contentType &&
+           contentType.match(/^text|xml|json|javascript/);
 }
\ No newline at end of file