Issue #4988 - make the MIME type comparison in GzipHandler case insensitive

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-09-07 17:29:13 +10:00
parent 76b38d20d4
commit 7642be8fd0
2 changed files with 77 additions and 1 deletions

View File

@ -21,6 +21,7 @@ package org.eclipse.jetty.server.handler.gzip;
import java.io.IOException;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.zip.Deflater;
@ -168,7 +169,7 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
// non-static, as other GzipHandler instances may have different configurations
private final IncludeExclude<String> _methods = new IncludeExclude<>();
private final IncludeExclude<String> _paths = new IncludeExclude<>(PathSpecSet.class);
private final IncludeExclude<String> _mimeTypes = new IncludeExclude<>();
private final IncludeExclude<String> _mimeTypes = new IncludeExclude<>(CaseInsensitiveSet.class);
private HttpField _vary = GzipHttpOutputInterceptor.VARY_ACCEPT_ENCODING;
/**
@ -900,4 +901,21 @@ public class GzipHandler extends HandlerWrapper implements GzipFactory
{
return String.format("%s@%x{%s,min=%s,inflate=%s}", getClass().getSimpleName(), hashCode(), getState(), _minGzipSize, _inflateBufferSize);
}
private static class CaseInsensitiveSet extends HashSet<String>
{
@Override
public boolean add(String s)
{
return super.add(s == null ? null : s.toLowerCase());
}
@Override
public boolean contains(Object o)
{
if (o instanceof String)
return super.contains(((String)o).toLowerCase());
return super.contains(o);
}
}
}

View File

@ -945,4 +945,62 @@ public class GzipDefaultServletTest extends AbstractGzipTest
UncompressedMetadata metadata = parseResponseContent(response);
assertThat("Response Content Length", metadata.contentLength, is(fileSize));
}
@Test
public void testUpperCaseMimeType() throws Exception
{
GzipHandler gzipHandler = new GzipHandler();
gzipHandler.addExcludedMimeTypes("text/PLAIN");
server = new Server();
LocalConnector localConnector = new LocalConnector(server);
server.addConnector(localConnector);
Path contextDir = workDir.resolve("context");
FS.ensureDirExists(contextDir);
ServletContextHandler servletContextHandler = new ServletContextHandler();
servletContextHandler.setContextPath("/context");
servletContextHandler.setBaseResource(new PathResource(contextDir));
ServletHolder holder = new ServletHolder("default", DefaultServlet.class);
holder.setInitParameter("etags", "true");
servletContextHandler.addServlet(holder, "/");
servletContextHandler.insertHandler(gzipHandler);
server.setHandler(servletContextHandler);
// Prepare Server File
int fileSize = DEFAULT_OUTPUT_BUFFER_SIZE * 4;
Path file = createFile(contextDir, "file.txt", fileSize);
String expectedSha1Sum = Sha1Sum.calculate(file);
server.start();
// Setup request
HttpTester.Request request = HttpTester.newRequest();
request.setMethod("GET");
request.setVersion(HttpVersion.HTTP_1_1);
request.setHeader("Host", "tester");
request.setHeader("Connection", "close");
request.setHeader("Accept-Encoding", "gzip");
request.setURI("/context/file.txt");
// Issue request
ByteBuffer rawResponse = localConnector.getResponse(request.generate(), 5, TimeUnit.SECONDS);
// Parse response
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
assertThat("Response status", response.getStatus(), is(HttpStatus.OK_200));
// Response Content-Encoding check
assertThat("Response[Content-Encoding]", response.get("Content-Encoding"), not(containsString("gzip")));
assertThat("Response[Vary]", response.get("Vary"), is(nullValue()));
// Response Content checks
UncompressedMetadata metadata = parseResponseContent(response);
assertThat("Response Content Length", metadata.contentLength, is(fileSize));
assertThat("(Uncompressed) Content Length", metadata.uncompressedSize, is(fileSize));
assertThat("(Uncompressed) Content Hash", metadata.uncompressedSha1Sum, is(expectedSha1Sum));
}
}