From 3a6ad49271a86f5b0842b2cef7628bd2d7e30198 Mon Sep 17 00:00:00 2001 From: Ludovic Orban Date: Wed, 21 Feb 2024 09:26:34 +0100 Subject: [PATCH] Jetty 12: `ContextHandler.getTempDirectory()` does not respect the `Context.getTempDirectory()` contract (#11397) #11396 fix ContextHandler.getTempDirectory() so it never returns null as the contract mandates Signed-off-by: Ludovic Orban --- .../jetty/server/handler/ContextHandler.java | 2 +- .../org/eclipse/jetty/server/ServerTest.java | 9 ++++ .../server/handler/ContextHandlerTest.java | 12 +++++ .../ee10/servlet/MultiPartServletTest.java | 52 +++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index a4324eb90fc..3071a517085 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -1126,7 +1126,7 @@ public class ContextHandler extends Handler.Wrapper implements Attributes, Alias { File tempDirectory = ContextHandler.this.getTempDirectory(); if (tempDirectory == null) - tempDirectory = getServer().getTempDirectory(); + tempDirectory = getServer().getContext().getTempDirectory(); return tempDirectory; } diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ServerTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ServerTest.java index a29615be93e..2fd15118a15 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ServerTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/ServerTest.java @@ -13,6 +13,7 @@ package org.eclipse.jetty.server; +import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicReference; @@ -44,6 +45,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; public class ServerTest { @@ -109,6 +111,13 @@ public class ServerTest _connector = null; } + @Test + public void testContextTempDirectory() + { + File tempDirectory = _server.getContext().getTempDirectory(); + assertThat(tempDirectory, not(nullValue())); + } + @Test public void testSimpleGET() throws Exception { diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java index 38f575f9902..3b82e1d8817 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java @@ -86,6 +86,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.sameInstance; @@ -136,6 +137,17 @@ public class ContextHandlerTest FS.ensureDeleted(TEST_BAD.toPath()); } + @Test + public void testContextTempDirectory() throws Exception + { + HelloHandler helloHandler = new HelloHandler(); + _contextHandler.setHandler(helloHandler); + _server.start(); + + File tempDirectory = _contextHandler.getContext().getTempDirectory(); + assertThat(tempDirectory, not(nullValue())); + } + @Test public void testMiss() throws Exception { diff --git a/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/MultiPartServletTest.java b/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/MultiPartServletTest.java index 9e46e56f678..a4e6d4fe5b9 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/MultiPartServletTest.java +++ b/jetty-ee10/jetty-ee10-servlet/src/test/java/org/eclipse/jetty/ee10/servlet/MultiPartServletTest.java @@ -371,6 +371,58 @@ public class MultiPartServletTest assertThat(fileList.length, is(0)); } + @ParameterizedTest + @ValueSource(booleans = {true, false}) + public void testDefaultTempDirectory(boolean eager) throws Exception + { + start(new HttpServlet() + { + @Override + protected void service(HttpServletRequest request, HttpServletResponse response1) throws ServletException, IOException + { + Collection parts = request.getParts(); + assertNotNull(parts); + assertEquals(1, parts.size()); + Part part = parts.iterator().next(); + assertEquals("part1", part.getName()); + Collection headerNames = part.getHeaderNames(); + assertNotNull(headerNames); + assertEquals(2, headerNames.size()); + String content1 = IO.toString(part.getInputStream(), UTF_8); + assertEquals("content1", content1); + } + }, new MultipartConfigElement(null, MAX_FILE_SIZE, -1, 0), eager); + + try (Socket socket = new Socket("localhost", connector.getLocalPort())) + { + OutputStream output = socket.getOutputStream(); + + String content = """ + --A1B2C3 + Content-Disposition: form-data; name="part1" + Content-Type: text/plain; charset="UTF-8" + + content1 + --A1B2C3-- + """; + String header = """ + POST / HTTP/1.1 + Host: localhost + Content-Type: multipart/form-data; boundary="A1B2C3" + Content-Length: $L + + """.replace("$L", String.valueOf(content.length())); + + output.write(header.getBytes(UTF_8)); + output.write(content.getBytes(UTF_8)); + output.flush(); + + HttpTester.Response response = HttpTester.parseResponse(socket.getInputStream()); + assertNotNull(response); + assertEquals(HttpStatus.OK_200, response.getStatus()); + } + } + @ParameterizedTest @ValueSource(booleans = {true, false}) public void testMultiPartGzip(boolean eager) throws Exception