From 41aabf7d5ae7c3783caf6cab1a8d64a9f20e6fe3 Mon Sep 17 00:00:00 2001 From: markt Date: Tue, 28 Sep 2010 12:08:53 +0000 Subject: [PATCH] Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49625 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 --- .../coyote/http11/AbstractHttp11Processor.java | 70 +++++++++++++--------- webapps/docs/changelog.xml | 4 ++ 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/java/org/apache/coyote/http11/AbstractHttp11Processor.java b/java/org/apache/coyote/http11/AbstractHttp11Processor.java index 9dc59dc67..f1204cd1d 100644 --- a/java/org/apache/coyote/http11/AbstractHttp11Processor.java +++ b/java/org/apache/coyote/http11/AbstractHttp11Processor.java @@ -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) { diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index aa063d9e2..48a4284f0 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -188,6 +188,10 @@ (markt) + 49625: Ensure Vary header is set if response may be + compressed rather than only setting it if it is compressed. (markt) + + 49802: Re-factor connector pause, stop and destroy methods so that calling any of those methods has the expected results. (markt) -- 2.11.0