From 4729f7e16f6bcab28c61bf5f5bac546a45542c64 Mon Sep 17 00:00:00 2001 From: remm Date: Wed, 11 Oct 2023 09:41:14 +0200 Subject: [PATCH] Revert compression refactoring BZ 67670 Add test case to verify content-length is not present when using the connector compression (also that checks DefaultServlet is working with it). --- .../apache/coyote/http11/Http11Processor.java | 13 ++++--- .../catalina/servlets/TestDefaultServlet.java | 38 +++++++++++++++++++ webapps/docs/changelog.xml | 8 ++++ 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/java/org/apache/coyote/http11/Http11Processor.java b/java/org/apache/coyote/http11/Http11Processor.java index 6041e85ba28d..5b84ec3cc712 100644 --- a/java/org/apache/coyote/http11/Http11Processor.java +++ b/java/org/apache/coyote/http11/Http11Processor.java @@ -915,6 +915,12 @@ protected final void prepareResponse() throws IOException { prepareSendfile(outputFilters); } + // Check for compression + boolean useCompression = false; + if (entityBody && sendfileData == null) { + useCompression = protocol.useCompression(request, response); + } + MimeHeaders headers = response.getMimeHeaders(); // A SC_NO_CONTENT response may include entity headers if (entityBody || statusCode == HttpServletResponse.SC_NO_CONTENT) { @@ -951,11 +957,8 @@ protected final void prepareResponse() throws IOException { } } - // Check for compression - if (entityBody && sendfileData == null) { - if (protocol.useCompression(request, response)) { - outputBuffer.addActiveFilter(outputFilters[Constants.GZIP_FILTER]); - } + if (useCompression) { + outputBuffer.addActiveFilter(outputFilters[Constants.GZIP_FILTER]); } // Add date header unless application has already set one (e.g. in a diff --git a/test/org/apache/catalina/servlets/TestDefaultServlet.java b/test/org/apache/catalina/servlets/TestDefaultServlet.java index 364fee635475..c8321cee2563 100644 --- a/test/org/apache/catalina/servlets/TestDefaultServlet.java +++ b/test/org/apache/catalina/servlets/TestDefaultServlet.java @@ -40,6 +40,7 @@ import org.apache.catalina.startup.SimpleHttpClient; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.TomcatBaseTest; +import org.apache.coyote.http11.AbstractHttp11Protocol; import org.apache.tomcat.util.buf.ByteChunk; import org.apache.tomcat.util.descriptor.web.ErrorPage; import org.apache.tomcat.websocket.server.WsContextListener; @@ -86,6 +87,43 @@ public void testGetSpecials() throws Exception { } + @Test + public void testDefaultCompression() throws Exception { + + Tomcat tomcat = getTomcatInstance(); + ((AbstractHttp11Protocol) tomcat.getConnector().getProtocolHandler()).setCompression("force"); + + File appDir = new File("test/webapp"); + + // app dir is relative to server home + Context ctxt = tomcat.addContext("", appDir.getAbsolutePath()); + Wrapper defaultServlet = Tomcat.addServlet(ctxt, "default", + "org.apache.catalina.servlets.DefaultServlet"); + defaultServlet.addInitParameter("fileEncoding", "ISO-8859-1"); + ctxt.addServletMappingDecoded("/", "default"); + + ctxt.addMimeMapping("html", "text/html"); + + tomcat.start(); + + TestCompressedClient gzipClient = new TestCompressedClient(getPort()); + + gzipClient.reset(); + gzipClient.setRequest(new String[] { + "GET /index.html HTTP/1.1" + CRLF + + "Host: localhost" + CRLF + + "Connection: Close" + CRLF + + "Accept-Encoding: gzip" + CRLF + CRLF }); + gzipClient.connect(); + gzipClient.processRequest(); + Assert.assertTrue(gzipClient.isResponse200()); + List responseHeaders = gzipClient.getResponseHeaders(); + Assert.assertTrue(responseHeaders.contains("Content-Encoding: gzip")); + for (String header : responseHeaders) { + Assert.assertFalse(header.startsWith("Content-Length: ")); + } + } + /* * Verify serving of gzipped resources from context root. */ diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 44045b1f3041..a56ab65a7b85 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -105,6 +105,14 @@ issues do not "pop up" wrt. others). -->
+ + + + 67670: Fix regression with HTTP compression after code + refactoring. (remm) + + +