From 70866690e427d50fa7a2232c8c971fffd8ad0416 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Wed, 18 Jan 2017 14:11:25 +1100 Subject: [PATCH] Issue #1270 Avoid GzipHandler rework by DispatcherType exclusion --- .../server/handler/gzip/GzipHandler.java | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java index 85669126ade..130de428256 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java @@ -20,9 +20,12 @@ package org.eclipse.jetty.server.handler.gzip; import java.io.File; import java.io.IOException; +import java.util.Arrays; +import java.util.EnumSet; import java.util.Set; import java.util.zip.Deflater; +import javax.servlet.DispatcherType; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -65,6 +68,7 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory private int _compressionLevel=Deflater.DEFAULT_COMPRESSION; private boolean _checkGzExists = true; private boolean _syncFlush = false; + private EnumSet _dispatchers = EnumSet.of(DispatcherType.REQUEST); // non-static, as other GzipHandler instances may have different configurations private final ThreadLocal _deflater = new ThreadLocal<>(); @@ -126,6 +130,25 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory _methods.exclude(m); } + + /* ------------------------------------------------------------ */ + public EnumSet getDispatcherTypes() + { + return _dispatchers; + } + + /* ------------------------------------------------------------ */ + public void setDispatcherTypes(EnumSet dispatchers) + { + _dispatchers = dispatchers; + } + + /* ------------------------------------------------------------ */ + public void setDispatcherTypes(DispatcherType... dispatchers) + { + _dispatchers = EnumSet.copyOf(Arrays.asList(dispatchers)); + } + /* ------------------------------------------------------------ */ /** * Set the mime types. @@ -349,15 +372,16 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory /* ------------------------------------------------------------ */ /** - * Get the minimum reponse size. + * Get the minimum response size. * - * @return minimum reponse size + * @return minimum response size */ public int getMinGzipSize() { return _minGzipSize; } + /* ------------------------------------------------------------ */ protected HttpField getVaryField() { return _vary; @@ -374,8 +398,15 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory String path = context==null?baseRequest.getRequestURI():URIUtil.addPaths(baseRequest.getServletPath(),baseRequest.getPathInfo()); LOG.debug("{} handle {} in {}",this,baseRequest,context); - HttpOutput out = baseRequest.getResponse().getHttpOutput(); + if (!_dispatchers.contains(baseRequest.getDispatcherType())) + { + LOG.debug("{} excluded by dispatcherType {}",this,baseRequest.getDispatcherType()); + _handler.handle(target,baseRequest, request, response); + return; + } + // Are we already being gzipped? + HttpOutput out = baseRequest.getResponse().getHttpOutput(); HttpOutput.Interceptor interceptor = out.getInterceptor(); while (interceptor!=null) { @@ -406,7 +437,7 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory } // Exclude non compressible mime-types known from URI extension. - no Vary because no matter what client, this URI is always excluded - String mimeType = context==null?null:context.getMimeType(path); + String mimeType = context==null?MimeTypes.getDefaultMimeByExtension(path):context.getMimeType(path); if (mimeType!=null) { mimeType = MimeTypes.getContentTypeWithoutCharset(mimeType);