Skip to content

Commit

Permalink
Issue #10463 Fix lastModified header when using HttpServletResponseWr…
Browse files Browse the repository at this point in the history
…apper
  • Loading branch information
janbartel committed Sep 20, 2023
1 parent e68fd6c commit d32cb44
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,44 @@
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;
import org.junit.jupiter.api.Test;

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
Expand Down Expand Up @@ -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/*");
Expand All @@ -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
{
Expand Down

0 comments on commit d32cb44

Please sign in to comment.