Merge pull request #4767 from eclipse/jetty-10.0.x-4765-gziphandler_servletcontexthandler

Issue #4765 - Review GzipHandler inside ServletContextHandler.
This commit is contained in:
Simone Bordet 2020-04-14 23:30:29 +02:00 committed by GitHub
commit 5bad85b37b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 47 additions and 104 deletions

View File

@ -95,7 +95,6 @@ public class ServletContextHandler extends ContextHandler
public static final int SESSIONS = 1;
public static final int SECURITY = 2;
public static final int GZIP = 4;
public static final int NO_SESSIONS = 0;
public static final int NO_SECURITY = 0;
@ -128,7 +127,6 @@ public class ServletContextHandler extends ContextHandler
protected SessionHandler _sessionHandler;
protected SecurityHandler _securityHandler;
protected ServletHandler _servletHandler;
protected GzipHandler _gzipHandler;
protected int _options;
protected JspConfigDescriptor _jspConfig;
@ -250,10 +248,8 @@ public class ServletContextHandler extends ContextHandler
// link session handler
if (getSessionHandler() != null)
{
while (!(handler.getHandler() instanceof SessionHandler) &&
!(handler.getHandler() instanceof SecurityHandler) &&
!(handler.getHandler() instanceof GzipHandler) &&
!(handler.getHandler() instanceof ServletHandler) &&
handler.getHandler() instanceof HandlerWrapper)
{
@ -269,7 +265,6 @@ public class ServletContextHandler extends ContextHandler
if (getSecurityHandler() != null)
{
while (!(handler.getHandler() instanceof SecurityHandler) &&
!(handler.getHandler() instanceof GzipHandler) &&
!(handler.getHandler() instanceof ServletHandler) &&
handler.getHandler() instanceof HandlerWrapper)
{
@ -281,21 +276,6 @@ public class ServletContextHandler extends ContextHandler
handler = _securityHandler;
}
// link gzip handler
if (getGzipHandler() != null)
{
while (!(handler.getHandler() instanceof GzipHandler) &&
!(handler.getHandler() instanceof ServletHandler) &&
handler.getHandler() instanceof HandlerWrapper)
{
handler = (HandlerWrapper)handler.getHandler();
}
if (handler.getHandler() != _gzipHandler)
doSetHandler(handler, _gzipHandler);
handler = _gzipHandler;
}
// link servlet handler
if (getServletHandler() != null)
{
@ -369,8 +349,6 @@ public class ServletContextHandler extends ContextHandler
/**
* Finish constructing handlers and link them together.
*
* @see org.eclipse.jetty.server.handler.ContextHandler#startContext()
*/
@Override
protected void startContext() throws Exception
@ -440,17 +418,6 @@ public class ServletContextHandler extends ContextHandler
return _sessionHandler;
}
/**
* @return Returns the gzipHandler.
*/
@ManagedAttribute(value = "context gzip handler", readonly = true)
public GzipHandler getGzipHandler()
{
if (_gzipHandler == null && (_options & GZIP) != 0 && !isStarted())
_gzipHandler = new GzipHandler();
return _gzipHandler;
}
/**
* Convenience method to add a servlet.
*
@ -652,16 +619,6 @@ public class ServletContextHandler extends ContextHandler
relinkHandlers();
}
/**
* @param gzipHandler The {@link GzipHandler} to set on this context.
*/
public void setGzipHandler(GzipHandler gzipHandler)
{
replaceHandler(_gzipHandler, gzipHandler);
_gzipHandler = gzipHandler;
relinkHandlers();
}
/**
* @param servletHandler The servletHandler to set.
*/
@ -673,7 +630,18 @@ public class ServletContextHandler extends ContextHandler
}
/**
* Insert a HandlerWrapper before the first Session,Security or ServletHandler
* @param gzipHandler the GzipHandler for this ServletContextHandler
* @deprecated use {@link #insertHandler(HandlerWrapper)} instead
*/
@Deprecated
public void setGzipHandler(GzipHandler gzipHandler)
{
insertHandler(gzipHandler);
LOG.warn("ServletContextHandler.setGzipHandler(GzipHandler) is deprecated, use insertHandler(HandlerWrapper) instead.");
}
/**
* Insert a HandlerWrapper before the first Session, Security or ServletHandler
* but after any other HandlerWrappers.
*/
@Override
@ -683,8 +651,6 @@ public class ServletContextHandler extends ContextHandler
setSessionHandler((SessionHandler)handler);
else if (handler instanceof SecurityHandler)
setSecurityHandler((SecurityHandler)handler);
else if (handler instanceof GzipHandler)
setGzipHandler((GzipHandler)handler);
else if (handler instanceof ServletHandler)
setServletHandler((ServletHandler)handler);
else

View File

@ -79,7 +79,6 @@ import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.util.DecoratedObjectFactory;
import org.eclipse.jetty.util.Decorator;
@ -1630,30 +1629,6 @@ public class ServletContextHandlerTest
assertEquals(extra, context.getSessionHandler().getHandler());
}
@Test
public void testGzipHandlerOption() throws Exception
{
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS | ServletContextHandler.GZIP);
GzipHandler gzip = context.getGzipHandler();
_server.start();
assertEquals(context.getSessionHandler(), context.getHandler());
assertEquals(gzip, context.getSessionHandler().getHandler());
assertEquals(context.getServletHandler(), gzip.getHandler());
}
@Test
public void testGzipHandlerSet() throws Exception
{
ServletContextHandler context = new ServletContextHandler();
context.setSessionHandler(new SessionHandler());
context.setGzipHandler(new GzipHandler());
GzipHandler gzip = context.getGzipHandler();
_server.start();
assertEquals(context.getSessionHandler(), context.getHandler());
assertEquals(gzip, context.getSessionHandler().getHandler());
assertEquals(context.getServletHandler(), gzip.getHandler());
}
@Test
public void testReplaceServletHandlerWithServlet() throws Exception
{
@ -1689,14 +1664,12 @@ public class ServletContextHandlerTest
@Test
public void testSetSecurityHandler() throws Exception
{
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS | ServletContextHandler.SECURITY | ServletContextHandler.GZIP);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS | ServletContextHandler.SECURITY);
assertNotNull(context.getSessionHandler());
SessionHandler sessionHandler = context.getSessionHandler();
assertNotNull(context.getSecurityHandler());
SecurityHandler securityHandler = context.getSecurityHandler();
assertNotNull(context.getGzipHandler());
GzipHandler gzipHandler = context.getGzipHandler();
//check the handler linking order
HandlerWrapper h = (HandlerWrapper)context.getHandler();
assertSame(h, sessionHandler);
@ -1704,9 +1677,6 @@ public class ServletContextHandlerTest
h = (HandlerWrapper)h.getHandler();
assertSame(h, securityHandler);
h = (HandlerWrapper)h.getHandler();
assertSame(h, gzipHandler);
//replace the security handler
SecurityHandler myHandler = new SecurityHandler()
{
@ -1747,9 +1717,6 @@ public class ServletContextHandlerTest
h = (HandlerWrapper)h.getHandler();
assertSame(h, myHandler);
h = (HandlerWrapper)h.getHandler();
assertSame(h, gzipHandler);
}
@Test

View File

@ -673,7 +673,7 @@ public class GzipDefaultTest
GzipTester tester = new GzipTester(testingdir.getEmptyPathDir(), compressionType);
// Configure Gzip Handler
tester.getGzipHandler().setExcludedPaths("/bad.txt");
tester.getGzipHandler().setExcludedPaths(tester.getContextPath() + "/bad.txt");
tester.getGzipHandler().setIncludedPaths("*.txt");
// Prepare server file

View File

@ -44,7 +44,6 @@ import org.eclipse.jetty.http.tools.HttpTester;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.ServletTester;
import org.eclipse.jetty.toolchain.test.FS;
@ -82,28 +81,33 @@ public class GzipTester
private String encoding = "ISO8859_1";
private String userAgent = null;
private final ServletTester tester = new ServletTester("/context", ServletContextHandler.GZIP);
private final GzipHandler gzipHandler = new GzipHandler();
private final ServletTester tester = new ServletTester("/context");
private Path testdir;
private String accept;
private String compressionType;
public GzipTester(Path testingdir, String compressionType)
{
this(testingdir, compressionType, compressionType);
}
public GzipTester(Path testingdir, String compressionType, String accept)
{
this.testdir = testingdir;
this.compressionType = compressionType;
this.accept = accept;
this.tester.getServer().insertHandler(gzipHandler);
}
public GzipTester(Path testingdir, String compressionType)
public String getContextPath()
{
this.testdir = testingdir;
this.compressionType = compressionType;
this.accept = compressionType;
return tester.getContextPath();
}
public GzipHandler getGzipHandler()
{
return tester.getContext().getGzipHandler();
return gzipHandler;
}
public int getOutputBufferSize()

View File

@ -6,11 +6,13 @@
<New class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/test-jetty-webapp</Set>
<Set name="war"><Property name="test.webapps" default="." />/test-jetty-webapp.war</Set>
<Set name="gzipHandler">
<New class="org.eclipse.jetty.server.handler.gzip.GzipHandler">
<Set name="minGzipSize">1024</Set>
</New>
</Set>
<Call name="insertHandler">
<Arg>
<New class="org.eclipse.jetty.server.handler.gzip.GzipHandler">
<Set name="minGzipSize">1024</Set>
</New>
</Arg>
</Call>
</New>
</Call>
</Configure>

View File

@ -3,9 +3,11 @@
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/rfc2616-webapp</Set>
<Set name="war"><Property name="test.webapps" default="." />/test-webapp-rfc2616.war</Set>
<Set name="gzipHandler">
<New class="org.eclipse.jetty.server.handler.gzip.GzipHandler">
<Set name="minGzipSize">1024</Set>
</New>
</Set>
<Call name="insertHandler">
<Arg>
<New class="org.eclipse.jetty.server.handler.gzip.GzipHandler">
<Set name="minGzipSize">1024</Set>
</New>
</Arg>
</Call>
</Configure>

View File

@ -35,11 +35,13 @@ detected.
<Arg type="Boolean">true</Arg>
</Call>
<Set name="gzipHandler">
<New class="org.eclipse.jetty.server.handler.gzip.GzipHandler">
<Set name="minGzipSize">2048</Set>
</New>
</Set>
<Call name="insertHandler">
<Arg>
<New class="org.eclipse.jetty.server.handler.gzip.GzipHandler">
<Set name="minGzipSize">2048</Set>
</New>
</Arg>
</Call>
<!-- Set Caching Classloader that improves performance on resource searching webapps -->
<!--