From 9bfa5cc65e8fa090a437510f0f3f30d144fac298 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Thu, 21 Sep 2023 05:49:54 +0200 Subject: [PATCH] Issue #10463 Fix lastModified header when using HttpServletResponseWrapper (#10556) * Issue #10463 Fix lastModified header when using HttpServletResponseWrapper --- .../test/resources/contextResources/test.txt | 1 + .../eclipse/jetty/ee9/nested/Response.java | 2 +- .../ee9/servlet/ResponseHeadersTest.java | 38 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 jetty-ee8/jetty-ee8-servlet/src/test/resources/contextResources/test.txt diff --git a/jetty-ee8/jetty-ee8-servlet/src/test/resources/contextResources/test.txt b/jetty-ee8/jetty-ee8-servlet/src/test/resources/contextResources/test.txt new file mode 100644 index 000000000000..cf3582b72034 --- /dev/null +++ b/jetty-ee8/jetty-ee8-servlet/src/test/resources/contextResources/test.txt @@ -0,0 +1 @@ +Test 2 to too two \ No newline at end of file diff --git a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/Response.java b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/Response.java index fadd8fd65fce..893e68726610 100644 --- a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/Response.java +++ b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/Response.java @@ -1402,7 +1402,7 @@ else if (contentLength > NO_CONTENT_LENGTH) public static void putHeaders(HttpServletResponse response, HttpContent content, long contentLength, boolean etag) { - long lml = content.getLastModified().getLongValue(); + long lml = content.getResource().lastModified().toEpochMilli(); if (lml >= 0) response.setDateHeader(HttpHeader.LAST_MODIFIED.asString(), lml); diff --git a/jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/ResponseHeadersTest.java b/jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/ResponseHeadersTest.java index 6feabac95ee8..9e10423f4949 100644 --- a/jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/ResponseHeadersTest.java +++ b/jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/ResponseHeadersTest.java @@ -18,15 +18,24 @@ import java.net.URLDecoder; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.util.EnumSet; +import jakarta.servlet.DispatcherType; +import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpFilter; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpServletResponseWrapper; import org.eclipse.jetty.http.HttpTester; import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.server.LocalConnector; import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.util.StringUtil; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; @@ -34,9 +43,19 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertTrue; public class ResponseHeadersTest { + public static class WrappingFilter extends HttpFilter + { + public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) + throws IOException, ServletException + { + this.doFilter((HttpServletRequest)req, new HttpServletResponseWrapper((HttpServletResponse)res), chain); + } + } + public static class SimulateUpgradeServlet extends HttpServlet { @Override @@ -135,14 +154,19 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t @BeforeAll public static void startServer() throws Exception { + Path staticContentPath = MavenTestingUtils.getTestResourcePath("contextResources"); server = new Server(); connector = new LocalConnector(server); server.addConnector(connector); ServletContextHandler context = new ServletContextHandler(); context.setContextPath("/"); + context.setBaseResourceAsPath(staticContentPath); + context.setInitParameter("org.eclipse.jetty.servlet.Default.pathInfoOnly", "TRUE"); server.setHandler(context); + context.addServlet(new ServletHolder(new DefaultServlet()), "/default/*"); + context.addFilter(new FilterHolder(new WrappingFilter()), "/default/*", EnumSet.allOf(DispatcherType.class)); context.addServlet(new ServletHolder(new SimulateUpgradeServlet()), "/ws/*"); context.addServlet(new ServletHolder(new MultilineResponseValueServlet()), "/multiline/*"); context.addServlet(CharsetResetToJsonMimeTypeServlet.class, "/charset/json-reset/*"); @@ -165,6 +189,20 @@ public static void stopServer() } } + @Test + public void testWrappedResponseWithStaticContent() throws Exception + { + HttpTester.Request request = new HttpTester.Request(); + request.setMethod("GET"); + request.setURI("/default/test.txt"); + request.setVersion(HttpVersion.HTTP_1_1); + request.setHeader("Host", "test"); + + ByteBuffer responseBuffer = connector.getResponse(request.generate()); + HttpTester.Response response = HttpTester.parseResponse(responseBuffer); + assertTrue(response.getContent().startsWith("Test 2")); + } + @Test public void testResponseWebSocketHeaderFormat() throws Exception {