diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java index c2c3b49e52d..e1c7b2b06df 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/gzip/GzipHandler.java @@ -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 _methods = new IncludeExclude<>(); private final IncludeExclude _paths = new IncludeExclude<>(PathSpecSet.class); - private final IncludeExclude _mimeTypes = new IncludeExclude<>(); + private final IncludeExclude _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 + { + @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); + } + } } diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipDefaultServletTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipDefaultServletTest.java index 3d60c01eac9..178fe7b1fe0 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipDefaultServletTest.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipDefaultServletTest.java @@ -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)); + } }