Merge branch 'jetty-9.4.x' into jetty-10.0.x

This commit is contained in:
olivier lamy 2021-02-23 10:38:05 +10:00
commit 0f32e264a2
2 changed files with 54 additions and 7 deletions

View File

@ -859,10 +859,10 @@ public class ResourceService
else else
{ {
Response.putHeaders(response, content, contentLength, _etags); Response.putHeaders(response, content, contentLength, _etags);
if (_acceptRanges && !response.containsHeader(HttpHeader.ACCEPT_RANGES.name())) if (_acceptRanges && !response.containsHeader(HttpHeader.ACCEPT_RANGES.asString()))
response.setHeader(ACCEPT_RANGES.getName(), ACCEPT_RANGES.getValue()); response.setHeader(ACCEPT_RANGES.getName(), ACCEPT_RANGES.getValue());
if (_cacheControl != null && !response.containsHeader(HttpHeader.CACHE_CONTROL.name())) if (_cacheControl != null && !response.containsHeader(HttpHeader.CACHE_CONTROL.asString()))
response.setHeader(_cacheControl.getName(), _cacheControl.getValue()); response.setHeader(_cacheControl.getName(), _cacheControl.getValue());
} }
} }

View File

@ -24,6 +24,7 @@ import javax.servlet.ServletException;
import javax.servlet.ServletRequest; import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.server.HttpConfiguration; import org.eclipse.jetty.server.HttpConfiguration;
@ -41,11 +42,22 @@ public class CacheControlHeaderTest
private Server server; private Server server;
private LocalConnector connector; private LocalConnector connector;
public static class SimpleResponseWrapper extends HttpServletResponseWrapper
{
public SimpleResponseWrapper(HttpServletResponse response)
{
super(response);
}
}
public static class ForceCacheControlFilter implements Filter public static class ForceCacheControlFilter implements Filter
{ {
private boolean forceWrapper;
@Override @Override
public void init(FilterConfig filterConfig) throws ServletException public void init(FilterConfig filterConfig) throws ServletException
{ {
forceWrapper = Boolean.parseBoolean(filterConfig.getInitParameter("FORCE_WRAPPER"));
} }
@Override @Override
@ -53,7 +65,14 @@ public class CacheControlHeaderTest
{ {
HttpServletResponse httpResponse = (HttpServletResponse)response; HttpServletResponse httpResponse = (HttpServletResponse)response;
httpResponse.setHeader(HttpHeader.CACHE_CONTROL.asString(), "max-age=0,private"); httpResponse.setHeader(HttpHeader.CACHE_CONTROL.asString(), "max-age=0,private");
chain.doFilter(request, response); if (forceWrapper)
{
chain.doFilter(request, new SimpleResponseWrapper((HttpServletResponse)response));
}
else
{
chain.doFilter(request, response);
}
} }
@Override @Override
@ -62,7 +81,7 @@ public class CacheControlHeaderTest
} }
} }
public void startServer(boolean forceFilter) throws Exception public void startServer(boolean forceFilter, boolean forceWrapping) throws Exception
{ {
server = new Server(); server = new Server();
@ -79,7 +98,8 @@ public class CacheControlHeaderTest
context.addServlet(servletHolder, "/*"); context.addServlet(servletHolder, "/*");
if (forceFilter) if (forceFilter)
{ {
context.addFilter(ForceCacheControlFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)); FilterHolder filterHolder = context.addFilter(ForceCacheControlFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
filterHolder.setInitParameter("FORCE_WRAPPER", Boolean.toString(forceWrapping));
} }
server.setHandler(context); server.setHandler(context);
server.addConnector(connector); server.addConnector(connector);
@ -100,7 +120,7 @@ public class CacheControlHeaderTest
{ {
try try
{ {
startServer(true); startServer(true, false);
StringBuffer req1 = new StringBuffer(); StringBuffer req1 = new StringBuffer();
req1.append("GET /content.txt HTTP/1.1\r\n"); req1.append("GET /content.txt HTTP/1.1\r\n");
req1.append("Host: local\r\n"); req1.append("Host: local\r\n");
@ -122,12 +142,39 @@ public class CacheControlHeaderTest
} }
} }
@Test
public void testCacheControlFilterOverrideWithWrapper() throws Exception
{
try
{
startServer(true, true);
StringBuffer req1 = new StringBuffer();
req1.append("GET /content.txt HTTP/1.1\r\n");
req1.append("Host: local\r\n");
req1.append("Accept: */*\r\n");
req1.append("Connection: close\r\n");
req1.append("\r\n");
String response = connector.getResponse(req1.toString());
assertThat("Response status",
response,
containsString("HTTP/1.1 200 OK"));
assertThat("Response headers",
response,
containsString(HttpHeader.CACHE_CONTROL.asString() + ": max-age=0,private"));
}
finally
{
stopServer();
}
}
@Test @Test
public void testCacheControlDefaultServlet() throws Exception public void testCacheControlDefaultServlet() throws Exception
{ {
try try
{ {
startServer(false); startServer(false, false);
StringBuffer req1 = new StringBuffer(); StringBuffer req1 = new StringBuffer();
req1.append("GET /content.txt HTTP/1.1\r\n"); req1.append("GET /content.txt HTTP/1.1\r\n");
req1.append("Host: local\r\n"); req1.append("Host: local\r\n");