381521 allow compress methods to be configured
This commit is contained in:
parent
c7d985fa02
commit
2581eb130a
|
@ -90,6 +90,8 @@ import org.eclipse.jetty.util.log.Logger;
|
|||
* deflateNoWrap The noWrap setting for deflate compression. Defaults to true. (true/false)
|
||||
* See: {@link java.util.zip.Deflater#Deflater(int, boolean)}
|
||||
*
|
||||
* methods Comma separated list of HTTP methods to compress. If not set, only GET requests are compressed.
|
||||
*
|
||||
* mimeTypes Comma separated list of mime types to compress. See description above.
|
||||
*
|
||||
* excludedAgents Comma separated list of user agents to exclude from compression. Does a
|
||||
|
@ -127,6 +129,8 @@ public class GzipFilter extends UserAgentFilter
|
|||
protected int _minGzipSize=256;
|
||||
protected int _deflateCompressionLevel=Deflater.DEFAULT_COMPRESSION;
|
||||
protected boolean _deflateNoWrap = true;
|
||||
|
||||
protected final Set<String> _methods=new HashSet<String>();
|
||||
protected Set<String> _excludedAgents;
|
||||
protected Set<Pattern> _excludedAgentPatterns;
|
||||
protected Set<String> _excludedPaths;
|
||||
|
@ -166,6 +170,16 @@ public class GzipFilter extends UserAgentFilter
|
|||
if (tmp!=null)
|
||||
_deflateNoWrap=Boolean.parseBoolean(tmp);
|
||||
|
||||
tmp=filterConfig.getInitParameter("methods");
|
||||
if (tmp!=null)
|
||||
{
|
||||
StringTokenizer tok = new StringTokenizer(tmp,",",false);
|
||||
while (tok.hasMoreTokens())
|
||||
_methods.add(tok.nextToken().trim().toUpperCase());
|
||||
}
|
||||
else
|
||||
_methods.add(HttpMethods.GET);
|
||||
|
||||
tmp=filterConfig.getInitParameter("mimeTypes");
|
||||
if (tmp!=null)
|
||||
{
|
||||
|
@ -235,9 +249,9 @@ public class GzipFilter extends UserAgentFilter
|
|||
HttpServletRequest request=(HttpServletRequest)req;
|
||||
HttpServletResponse response=(HttpServletResponse)res;
|
||||
|
||||
// If not a GET or an Excluded URI - no Vary because no matter what client, this URI is always excluded
|
||||
// If not a supported method or it is an Excluded URI - no Vary because no matter what client, this URI is always excluded
|
||||
String requestURI = request.getRequestURI();
|
||||
if (!HttpMethods.GET.equalsIgnoreCase(request.getMethod()) || isExcludedPath(requestURI))
|
||||
if (!_methods.contains(request.getMethod()) || isExcludedPath(requestURI))
|
||||
{
|
||||
super.doFilter(request,response,chain);
|
||||
return;
|
||||
|
|
|
@ -111,7 +111,7 @@ public class GzipFilterContentLengthTest
|
|||
try
|
||||
{
|
||||
tester.start();
|
||||
tester.assertIsResponseGzipCompressed(testfile.getName());
|
||||
tester.assertIsResponseGzipCompressed("GET",testfile.getName());
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
@ -131,7 +131,7 @@ public class GzipFilterContentLengthTest
|
|||
try
|
||||
{
|
||||
tester.start();
|
||||
tester.assertIsResponseNotGzipCompressed(testfile.getName(),filesize,HttpStatus.OK_200);
|
||||
tester.assertIsResponseNotGzipCompressed("GET",testfile.getName(),filesize,HttpStatus.OK_200);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
|
@ -103,6 +103,50 @@ public class GzipFilterDefaultTest
|
|||
@Rule
|
||||
public TestingDir testingdir = new TestingDir();
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testIsGzipByMethod() throws Exception
|
||||
{
|
||||
GzipTester tester = new GzipTester(testingdir, compressionType);
|
||||
|
||||
// Test content that is smaller than the buffer.
|
||||
int filesize = CompressedResponseWrapper.DEFAULT_BUFFER_SIZE * 2;
|
||||
tester.prepareServerFile("file.txt",filesize);
|
||||
|
||||
FilterHolder holder = tester.setContentServlet(GetServlet.class);
|
||||
holder.setInitParameter("mimeTypes","text/plain");
|
||||
holder.setInitParameter("methods","POST,WIBBLE");
|
||||
|
||||
try
|
||||
{
|
||||
tester.start();
|
||||
tester.assertIsResponseGzipCompressed("POST","file.txt");
|
||||
tester.assertIsResponseGzipCompressed("WIBBLE","file.txt");
|
||||
tester.assertIsResponseNotGzipCompressed("GET","file.txt",filesize,200);
|
||||
}
|
||||
finally
|
||||
{
|
||||
tester.stop();
|
||||
}
|
||||
}
|
||||
|
||||
public static class GetServlet extends DefaultServlet
|
||||
{
|
||||
public GetServlet()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException,ServletException
|
||||
{
|
||||
doGet(req,resp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testIsGzipCompressedTiny() throws Exception
|
||||
{
|
||||
|
@ -118,7 +162,7 @@ public class GzipFilterDefaultTest
|
|||
try
|
||||
{
|
||||
tester.start();
|
||||
HttpTester http = tester.assertIsResponseGzipCompressed("file.txt");
|
||||
HttpTester http = tester.assertIsResponseGzipCompressed("GET","file.txt");
|
||||
Assert.assertEquals("Accept-Encoding",http.getHeader("Vary"));
|
||||
}
|
||||
finally
|
||||
|
@ -142,7 +186,7 @@ public class GzipFilterDefaultTest
|
|||
try
|
||||
{
|
||||
tester.start();
|
||||
HttpTester http = tester.assertIsResponseGzipCompressed("file.txt");
|
||||
HttpTester http = tester.assertIsResponseGzipCompressed("GET","file.txt");
|
||||
Assert.assertEquals("Accept-Encoding",http.getHeader("Vary"));
|
||||
}
|
||||
finally
|
||||
|
@ -166,7 +210,7 @@ public class GzipFilterDefaultTest
|
|||
try
|
||||
{
|
||||
tester.start();
|
||||
HttpTester http = tester.assertIsResponseGzipCompressed("file.txt");
|
||||
HttpTester http = tester.assertIsResponseGzipCompressed("GET","file.txt");
|
||||
Assert.assertEquals("Accept-Encoding",http.getHeader("Vary"));
|
||||
}
|
||||
finally
|
||||
|
@ -190,7 +234,7 @@ public class GzipFilterDefaultTest
|
|||
try
|
||||
{
|
||||
tester.start();
|
||||
HttpTester http = tester.assertIsResponseGzipCompressed("file.txt");
|
||||
HttpTester http = tester.assertIsResponseGzipCompressed("GET","file.txt");
|
||||
Assert.assertEquals("Accept-Encoding",http.getHeader("Vary"));
|
||||
}
|
||||
finally
|
||||
|
@ -213,7 +257,7 @@ public class GzipFilterDefaultTest
|
|||
try
|
||||
{
|
||||
tester.start();
|
||||
HttpTester http = tester.assertIsResponseNotGzipCompressed("file.txt", filesize, HttpStatus.OK_200);
|
||||
HttpTester http = tester.assertIsResponseNotGzipCompressed("GET","file.txt", filesize, HttpStatus.OK_200);
|
||||
Assert.assertEquals("Accept-Encoding",http.getHeader("Vary"));
|
||||
}
|
||||
finally
|
||||
|
@ -236,7 +280,7 @@ public class GzipFilterDefaultTest
|
|||
try
|
||||
{
|
||||
tester.start();
|
||||
HttpTester http = tester.assertIsResponseNotGzipCompressed("file.mp3", filesize, HttpStatus.OK_200);
|
||||
HttpTester http = tester.assertIsResponseNotGzipCompressed("GET","file.mp3", filesize, HttpStatus.OK_200);
|
||||
Assert.assertNull(http.getHeader("Vary"));
|
||||
}
|
||||
finally
|
||||
|
@ -257,7 +301,7 @@ public class GzipFilterDefaultTest
|
|||
try
|
||||
{
|
||||
tester.start();
|
||||
tester.assertIsResponseNotGzipCompressed(-1, 204);
|
||||
tester.assertIsResponseNotGzipCompressed("GET",-1, 204);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
@ -278,7 +322,7 @@ public class GzipFilterDefaultTest
|
|||
try
|
||||
{
|
||||
tester.start();
|
||||
tester.assertIsResponseNotGzipCompressedAndEqualToExpectedString("error message", -1, 400);
|
||||
tester.assertIsResponseNotGzipCompressedAndEqualToExpectedString("GET","error message", -1, 400);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
@ -302,7 +346,7 @@ public class GzipFilterDefaultTest
|
|||
try
|
||||
{
|
||||
tester.start();
|
||||
tester.assertIsResponseNotGzipCompressed("file.txt",filesize,HttpStatus.OK_200);
|
||||
tester.assertIsResponseNotGzipCompressed("GET","file.txt",filesize,HttpStatus.OK_200);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
@ -326,7 +370,7 @@ public class GzipFilterDefaultTest
|
|||
try
|
||||
{
|
||||
tester.start();
|
||||
tester.assertIsResponseNotGzipCompressed("file.txt",filesize,HttpStatus.OK_200);
|
||||
tester.assertIsResponseNotGzipCompressed("GET","file.txt",filesize,HttpStatus.OK_200);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
@ -348,7 +392,7 @@ public class GzipFilterDefaultTest
|
|||
try
|
||||
{
|
||||
tester.start();
|
||||
tester.assertIsResponseNotGzipCompressed("file.txt",filesize,HttpStatus.OK_200);
|
||||
tester.assertIsResponseNotGzipCompressed("GET","file.txt",filesize,HttpStatus.OK_200);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
@ -370,7 +414,7 @@ public class GzipFilterDefaultTest
|
|||
try
|
||||
{
|
||||
tester.start();
|
||||
tester.assertIsResponseNotGzipCompressed("file.txt",filesize,HttpStatus.OK_200);
|
||||
tester.assertIsResponseNotGzipCompressed("GET","file.txt",filesize,HttpStatus.OK_200);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
|
@ -107,7 +107,7 @@ public class IncludableGzipFilterMinSizeTest
|
|||
|
||||
try {
|
||||
tester.start();
|
||||
tester.assertIsResponseGzipCompressed("big_script.js");
|
||||
tester.assertIsResponseGzipCompressed("GET","big_script.js");
|
||||
} finally {
|
||||
tester.stop();
|
||||
}
|
||||
|
|
|
@ -73,18 +73,18 @@ public class GzipTester
|
|||
// DOES NOT WORK IN WINDOWS - this.testdir.ensureEmpty();
|
||||
}
|
||||
|
||||
public HttpTester assertIsResponseGzipCompressed(String filename) throws Exception
|
||||
public HttpTester assertIsResponseGzipCompressed(String method,String filename) throws Exception
|
||||
{
|
||||
return assertIsResponseGzipCompressed(filename,filename);
|
||||
return assertIsResponseGzipCompressed(method,filename,filename);
|
||||
}
|
||||
|
||||
public HttpTester assertIsResponseGzipCompressed(String requestedFilename, String serverFilename) throws Exception
|
||||
public HttpTester assertIsResponseGzipCompressed(String method,String requestedFilename, String serverFilename) throws Exception
|
||||
{
|
||||
System.err.printf("[GzipTester] requesting /context/%s%n",requestedFilename);
|
||||
HttpTester request = new HttpTester();
|
||||
HttpTester response = new HttpTester();
|
||||
|
||||
request.setMethod("GET");
|
||||
request.setMethod(method);
|
||||
request.setVersion("HTTP/1.0");
|
||||
request.setHeader("Host","tester");
|
||||
request.setHeader("Accept-Encoding",compressionType);
|
||||
|
@ -245,10 +245,10 @@ public class GzipTester
|
|||
* passing -1 will disable the Content-Length assertion)
|
||||
* @throws Exception
|
||||
*/
|
||||
public HttpTester assertIsResponseNotGzipCompressed(String filename, int expectedFilesize, int status) throws Exception
|
||||
public HttpTester assertIsResponseNotGzipCompressed(String method,String filename, int expectedFilesize, int status) throws Exception
|
||||
{
|
||||
String uri = "/context/"+filename;
|
||||
HttpTester response = executeRequest(uri);
|
||||
HttpTester response = executeRequest(method,uri);
|
||||
assertResponseHeaders(expectedFilesize,status,response);
|
||||
|
||||
// Assert that the contents are what we expect.
|
||||
|
@ -276,10 +276,10 @@ public class GzipTester
|
|||
* passing -1 will disable the Content-Length assertion)
|
||||
* @throws Exception
|
||||
*/
|
||||
public void assertIsResponseNotGzipCompressedAndEqualToExpectedString(String expectedResponse, int expectedFilesize, int status) throws Exception
|
||||
public void assertIsResponseNotGzipCompressedAndEqualToExpectedString(String method,String expectedResponse, int expectedFilesize, int status) throws Exception
|
||||
{
|
||||
String uri = "/context/";
|
||||
HttpTester response = executeRequest(uri);
|
||||
HttpTester response = executeRequest(method,uri);
|
||||
assertResponseHeaders(expectedFilesize,status,response);
|
||||
|
||||
String actual = readResponse(response);
|
||||
|
@ -295,10 +295,10 @@ public class GzipTester
|
|||
* passing -1 will disable the Content-Length assertion)
|
||||
* @throws Exception
|
||||
*/
|
||||
public void assertIsResponseNotGzipCompressed(int expectedFilesize, int status) throws Exception
|
||||
public void assertIsResponseNotGzipCompressed(String method,int expectedFilesize, int status) throws Exception
|
||||
{
|
||||
String uri = "/context/";
|
||||
HttpTester response = executeRequest(uri);
|
||||
HttpTester response = executeRequest(method, uri);
|
||||
assertResponseHeaders(expectedFilesize,status,response);
|
||||
}
|
||||
|
||||
|
@ -315,13 +315,13 @@ public class GzipTester
|
|||
}
|
||||
}
|
||||
|
||||
private HttpTester executeRequest(String uri) throws IOException, Exception
|
||||
private HttpTester executeRequest(String method,String uri) throws IOException, Exception
|
||||
{
|
||||
System.err.printf("[GzipTester] requesting %s%n",uri);
|
||||
HttpTester request = new HttpTester();
|
||||
HttpTester response = new HttpTester();
|
||||
|
||||
request.setMethod("GET");
|
||||
request.setMethod(method);
|
||||
request.setVersion("HTTP/1.0");
|
||||
request.setHeader("Host","tester");
|
||||
request.setHeader("Accept-Encoding",compressionType);
|
||||
|
|
Loading…
Reference in New Issue