Skip to content

Commit

Permalink
Issue #4988 - make the MIME type comparison in GzipHandler case insen…
Browse files Browse the repository at this point in the history
…sitive

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Sep 7, 2020
1 parent 76b38d2 commit 7642be8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -900,4 +901,21 @@ public String toString()
{
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -945,4 +945,62 @@ public void testIsNotGzipCompressedSVGZ() throws Exception
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));
}
}

0 comments on commit 7642be8

Please sign in to comment.