Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49625
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 28 Sep 2010 12:08:53 +0000 (12:08 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 28 Sep 2010 12:08:53 +0000 (12:08 +0000)
Ensure Vary header is set if response may be compressed rather than only setting it if it is compressed.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1002133 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/coyote/http11/AbstractHttp11Processor.java
webapps/docs/changelog.xml

index 9dc59dc..f1204cd 100644 (file)
@@ -640,16 +640,42 @@ public abstract class AbstractHttp11Processor {
 
 
     /**
-     * Check for compression
+     * Check if the resource could be compressed, if the client supports it.
      */
-    protected boolean isCompressable() {
+    private boolean isCompressable() {
 
-        // Nope Compression could works in HTTP 1.0 also
-        // cf: mod_deflate
+        // Check if content is not already gzipped
+        MessageBytes contentEncodingMB =
+            response.getMimeHeaders().getValue("Content-Encoding");
+
+        if ((contentEncodingMB != null)
+            && (contentEncodingMB.indexOf("gzip") != -1))
+            return false;
+
+        // If force mode, always compress (test purposes only)
+        if (compressionLevel == 2)
+           return true;
+
+        // Check if sufficient length to trigger the compression
+        long contentLength = response.getContentLengthLong();
+        if ((contentLength == -1)
+            || (contentLength > compressionMinSize)) {
+            // Check for compatible MIME-TYPE
+            if (compressableMimeTypes != null) {
+                return (startsWithStringArray(compressableMimeTypes,
+                                              response.getContentType()));
+            }
+        }
 
-        // Compression only since HTTP 1.1
-        // if (! http11)
-        //    return false;
+        return false;
+    }
+
+    
+    /**
+     * Check if compression should be used for this resource. Already checked
+     * that the resource could be compressed if the client supports it.
+     */
+    private boolean useCompression() {
 
         // Check if browser support gzip encoding
         MessageBytes acceptEncodingMB =
@@ -659,14 +685,6 @@ public abstract class AbstractHttp11Processor {
             || (acceptEncodingMB.indexOf("gzip") == -1))
             return false;
 
-        // Check if content is not already gzipped
-        MessageBytes contentEncodingMB =
-            response.getMimeHeaders().getValue("Content-Encoding");
-
-        if ((contentEncodingMB != null)
-            && (contentEncodingMB.indexOf("gzip") != -1))
-            return false;
-
         // If force mode, always compress (test purposes only)
         if (compressionLevel == 2)
            return true;
@@ -685,18 +703,7 @@ public abstract class AbstractHttp11Processor {
             }
         }
 
-        // Check if sufficient length to trigger the compression
-        long contentLength = response.getContentLengthLong();
-        if ((contentLength == -1)
-            || (contentLength > compressionMinSize)) {
-            // Check for compatible MIME-TYPE
-            if (compressableMimeTypes != null) {
-                return (startsWithStringArray(compressableMimeTypes,
-                                              response.getContentType()));
-            }
-        }
-
-        return false;
+        return true;
     }
 
     
@@ -973,9 +980,13 @@ public abstract class AbstractHttp11Processor {
         }
         
         // Check for compression
+        boolean isCompressable = false;
         boolean useCompression = false;
         if (entityBody && (compressionLevel > 0) && !sendingWithSendfile) {
-            useCompression = isCompressable();
+            isCompressable = isCompressable();
+            if (isCompressable) {
+                useCompression = useCompression();
+            }
             // Change content-length to -1 to force chunking
             if (useCompression) {
                 response.setContentLength(-1);
@@ -1018,6 +1029,9 @@ public abstract class AbstractHttp11Processor {
         if (useCompression) {
             getOutputBuffer().addActiveFilter(outputFilters[Constants.GZIP_FILTER]);
             headers.setValue("Content-Encoding").setString("gzip");
+        }
+        // If it might be compressed, set the Vary header
+        if (isCompressable) {
             // Make Proxies happy via Vary (from mod_deflate)
             MessageBytes vary = headers.getValue("Vary");
             if (vary == null) {
index aa063d9..48a4284 100644 (file)
         (markt)
       </fix>
       <fix>
+        <bug>49625</bug>: Ensure Vary header is set if response may be
+        compressed rather than only setting it if it is compressed. (markt)
+      </fix>
+      <fix>
         <bug>49802</bug>: Re-factor connector pause, stop and destroy methods so
         that calling any of those methods has the expected results. (markt)
       </fix>