This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 8.5.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/8.5.x by this push:
     new 425e0da  Complete the fix for 63829. Align CompressionConfig usage 
with 9.0.x
425e0da is described below

commit 425e0da03fadcca7e985126f001abf12b988a4ae
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu Oct 24 09:34:36 2019 +0200

    Complete the fix for 63829. Align CompressionConfig usage with 9.0.x
    
    This removes 4 protected fields from Http11Processor. While this might
    cause problems in the unlikely case of this class being extended, the
    setters exist all the way back to 8.5.0 so there is a fix available that
    will work with all 8.5.x versions.
---
 .../coyote/http11/AbstractHttp11Protocol.java      |  11 +-
 java/org/apache/coyote/http11/Http11Processor.java | 215 +++------------------
 2 files changed, 39 insertions(+), 187 deletions(-)

diff --git a/java/org/apache/coyote/http11/AbstractHttp11Protocol.java 
b/java/org/apache/coyote/http11/AbstractHttp11Protocol.java
index 0310a0e..9611532 100644
--- a/java/org/apache/coyote/http11/AbstractHttp11Protocol.java
+++ b/java/org/apache/coyote/http11/AbstractHttp11Protocol.java
@@ -31,6 +31,8 @@ import javax.servlet.http.HttpUpgradeHandler;
 import org.apache.coyote.AbstractProtocol;
 import org.apache.coyote.CompressionConfig;
 import org.apache.coyote.Processor;
+import org.apache.coyote.Request;
+import org.apache.coyote.Response;
 import org.apache.coyote.UpgradeProtocol;
 import org.apache.coyote.UpgradeToken;
 import org.apache.coyote.http11.upgrade.InternalHttpUpgradeHandler;
@@ -254,6 +256,11 @@ public abstract class AbstractHttp11Protocol<S> extends 
AbstractProtocol<S> {
     }
 
 
+    public boolean useCompression(Request request, Response response) {
+        return compressionConfig.useCompression(request, response);
+    }
+
+
     /**
      * Regular expression that defines the User agents which should be
      * restricted to HTTP/1.0 support.
@@ -880,10 +887,6 @@ public abstract class AbstractHttp11Protocol<S> extends 
AbstractProtocol<S> {
         processor.setMaxKeepAliveRequests(getMaxKeepAliveRequests());
         processor.setConnectionUploadTimeout(getConnectionUploadTimeout());
         processor.setDisableUploadTimeout(getDisableUploadTimeout());
-        processor.setCompressionMinSize(getCompressionMinSize());
-        processor.setCompression(getCompression());
-        processor.setNoCompressionUserAgents(getNoCompressionUserAgents());
-        processor.setCompressibleMimeTypes(getCompressibleMimeTypes());
         processor.setRestrictedUserAgents(getRestrictedUserAgents());
         processor.setMaxSavePostSize(getMaxSavePostSize());
         processor.setServer(getServer());
diff --git a/java/org/apache/coyote/http11/Http11Processor.java 
b/java/org/apache/coyote/http11/Http11Processor.java
index b922d61..d2b8e48 100644
--- a/java/org/apache/coyote/http11/Http11Processor.java
+++ b/java/org/apache/coyote/http11/Http11Processor.java
@@ -48,6 +48,7 @@ import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.ExceptionUtils;
 import org.apache.tomcat.util.buf.ByteChunk;
 import org.apache.tomcat.util.buf.MessageBytes;
+import org.apache.tomcat.util.buf.StringUtils;
 import org.apache.tomcat.util.http.FastHttpDateFormat;
 import org.apache.tomcat.util.http.MimeHeaders;
 import org.apache.tomcat.util.http.parser.HttpParser;
@@ -159,38 +160,12 @@ public class Http11Processor extends AbstractProcessor {
 
 
     /**
-     * Allowed compression level.
-     */
-    protected int compressionLevel = 0;
-
-
-    /**
-     * Minimum content size to make compression.
-     */
-    protected int compressionMinSize = 2048;
-
-
-    /**
      * Max saved post size.
      */
     protected int maxSavePostSize = 4 * 1024;
 
 
     /**
-     * Regular expression that defines the user agents to not use gzip with
-     */
-    protected Pattern noCompressionUserAgents = null;
-
-
-    /**
-     * List of MIMES for which compression may be enabled.
-     * Note: This is not spelled correctly but can't be changed without 
breaking
-     *       compatibility
-     */
-    protected String[] compressableMimeTypes;
-
-
-    /**
      * Allow a customized the server header for the tin-foil hat folks.
      */
     private String server = null;
@@ -263,24 +238,12 @@ public class Http11Processor extends AbstractProcessor {
      * @param compression One of <code>on</code>, <code>force</code>,
      *                    <code>off</code> or the minimum compression size in
      *                    bytes which implies <code>on</code>
+     *
+     * @deprecated Use {@link Http11Protocol#setCompression(String)}
      */
+    @Deprecated
     public void setCompression(String compression) {
-        if (compression.equals("on")) {
-            this.compressionLevel = 1;
-        } else if (compression.equals("force")) {
-            this.compressionLevel = 2;
-        } else if (compression.equals("off")) {
-            this.compressionLevel = 0;
-        } else {
-            try {
-                // Try to parse compression as an int, which would give the
-                // minimum compression size
-                compressionMinSize = Integer.parseInt(compression);
-                this.compressionLevel = 1;
-            } catch (Exception e) {
-                this.compressionLevel = 0;
-            }
-        }
+        protocol.setCompression(compression);
     }
 
     /**
@@ -288,9 +251,12 @@ public class Http11Processor extends AbstractProcessor {
      *
      * @param compressionMinSize The minimum content length required for
      *                           compression in bytes
+     *
+     * @deprecated Use {@link Http11Protocol#setCompressionMinSize(int)}
      */
+    @Deprecated
     public void setCompressionMinSize(int compressionMinSize) {
-        this.compressionMinSize = compressionMinSize;
+        protocol.setCompressionMinSize(compressionMinSize);
     }
 
 
@@ -301,22 +267,20 @@ public class Http11Processor extends AbstractProcessor {
      * @param noCompressionUserAgents The regular expression for user agent
      *                                strings for which compression should not
      *                                be applied
+     *
+     * @deprecated Use {@link 
Http11Protocol#setNoCompressionUserAgents(String)}
      */
+    @Deprecated
     public void setNoCompressionUserAgents(String noCompressionUserAgents) {
-        if (noCompressionUserAgents == null || 
noCompressionUserAgents.length() == 0) {
-            this.noCompressionUserAgents = null;
-        } else {
-            this.noCompressionUserAgents =
-                Pattern.compile(noCompressionUserAgents);
-        }
+        protocol.setNoCompressionUserAgents(noCompressionUserAgents);
     }
 
 
     /**
      * @param compressibleMimeTypes See
      *        {@link Http11Processor#setCompressibleMimeTypes(String[])}
-     * @deprecated Use
-     *             {@link Http11Processor#setCompressibleMimeTypes(String[])}
+     *
+     * @deprecated Use {@link Http11Protocol#setCompressibleMimeType(String)}
      */
     @Deprecated
     public void setCompressableMimeTypes(String[] compressibleMimeTypes) {
@@ -325,15 +289,20 @@ public class Http11Processor extends AbstractProcessor {
 
 
     /**
-     * Set compressible mime-type list (this method is best when used with
-     * a large number of connectors, where it would be better to have all of
-     * them referenced a single array).
+     * Set compressible mime-type list.
      *
      * @param compressibleMimeTypes MIME types for which compression should be
      *                              enabled
+     *
+     * @deprecated Use {@link Http11Protocol#setCompressibleMimeType(String)}
      */
+    @Deprecated
     public void setCompressibleMimeTypes(String[] compressibleMimeTypes) {
-        this.compressableMimeTypes = compressibleMimeTypes;
+        // Conversion from array to comma separated string and back to array in
+        // CompressionConfig isn't ideal but it is better than adding a direct
+        // setter only to immediately deprecate it as it doesn't exist in 9.0.x
+        // given that Tomcat doesn't call this method in normal usage.
+        
protocol.setCompressibleMimeType(StringUtils.join(compressibleMimeTypes));
     }
 
 
@@ -341,36 +310,12 @@ public class Http11Processor extends AbstractProcessor {
      * Return compression level.
      *
      * @return The current compression level in string form (off/on/force)
-     */
-    public String getCompression() {
-        switch (compressionLevel) {
-        case 0:
-            return "off";
-        case 1:
-            return "on";
-        case 2:
-            return "force";
-        }
-        return "off";
-    }
-
-
-    /**
-     * Checks if any entry in the string array starts with the specified value
      *
-     * @param sArray the StringArray
-     * @param value string
+     * @deprecated Use {@link Http11Protocol#getCompression()}
      */
-    private static boolean startsWithStringArray(String sArray[], String 
value) {
-        if (value == null) {
-            return false;
-        }
-        for (int i = 0; i < sArray.length; i++) {
-            if (value.startsWith(sArray[i])) {
-                return true;
-            }
-        }
-        return false;
+    @Deprecated
+    public String getCompression() {
+        return protocol.getCompression();
     }
 
 
@@ -497,76 +442,6 @@ public class Http11Processor extends AbstractProcessor {
 
 
     /**
-     * Check if the resource could be compressed, if the client supports it.
-     */
-    private boolean isCompressible() {
-
-        // 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());
-            }
-        }
-
-        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 =
-            request.getMimeHeaders().getValue("accept-encoding");
-
-        if ((acceptEncodingMB == null)
-            || (acceptEncodingMB.indexOf("gzip") == -1)) {
-            return false;
-        }
-
-        // If force mode, always compress (test purposes only)
-        if (compressionLevel == 2) {
-            return true;
-        }
-
-        // Check for incompatible Browser
-        if (noCompressionUserAgents != null) {
-            MessageBytes userAgentValueMB =
-                request.getMimeHeaders().getValue("user-agent");
-            if(userAgentValueMB != null) {
-                String userAgentValue = userAgentValueMB.toString();
-
-                if (noCompressionUserAgents.matcher(userAgentValue).matches()) 
{
-                    return false;
-                }
-            }
-        }
-
-        return true;
-    }
-
-
-    /**
      * Determine if we must drop the connection because of the HTTP status
      * code.  Use the same list of codes as Apache/httpd.
      */
@@ -1220,17 +1095,9 @@ public class Http11Processor extends AbstractProcessor {
         }
 
         // Check for compression
-        boolean isCompressible = false;
         boolean useCompression = false;
-        if (entityBody && (compressionLevel > 0) && sendfileData == null) {
-            isCompressible = isCompressible();
-            if (isCompressible) {
-                useCompression = useCompression();
-            }
-            // Change content-length to -1 to force chunking
-            if (useCompression) {
-                response.setContentLength(-1);
-            }
+        if (entityBody && sendfileData == null) {
+            useCompression = protocol.useCompression(request, response);
         }
 
         MimeHeaders headers = response.getMimeHeaders();
@@ -1251,8 +1118,7 @@ public class Http11Processor extends AbstractProcessor {
         boolean connectionClosePresent = isConnectionToken(headers, 
Constants.CLOSE);
         if (contentLength != -1) {
             headers.setValue("Content-Length").setLong(contentLength);
-            outputBuffer.addActiveFilter
-                (outputFilters[Constants.IDENTITY_FILTER]);
+            
outputBuffer.addActiveFilter(outputFilters[Constants.IDENTITY_FILTER]);
             contentDelimitation = true;
         } else {
             // If the response code supports an entity body and we're on
@@ -1262,29 +1128,12 @@ public class Http11Processor extends AbstractProcessor {
                 contentDelimitation = true;
                 
headers.addValue(Constants.TRANSFERENCODING).setString(Constants.CHUNKED);
             } else {
-                outputBuffer.addActiveFilter
-                    (outputFilters[Constants.IDENTITY_FILTER]);
+                
outputBuffer.addActiveFilter(outputFilters[Constants.IDENTITY_FILTER]);
             }
         }
 
         if (useCompression) {
             outputBuffer.addActiveFilter(outputFilters[Constants.GZIP_FILTER]);
-            headers.setValue("Content-Encoding").setString("gzip");
-        }
-        // If it might be compressed, set the Vary header
-        if (isCompressible) {
-            // Make Proxies happy via Vary (from mod_deflate)
-            MessageBytes vary = headers.getValue("Vary");
-            if (vary == null) {
-                // Add a new Vary header
-                headers.setValue("Vary").setString("Accept-Encoding");
-            } else if (vary.equals("*")) {
-                // No action required
-            } else {
-                // Merge into current header
-                headers.setValue("Vary").setString(
-                        vary.getString() + ",Accept-Encoding");
-            }
         }
 
         // Add date header unless application has already set one (e.g. in a


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to