Skip to content

Commit

Permalink
Jetty 12: ContextHandler.getTempDirectory() does not respect the `C…
Browse files Browse the repository at this point in the history
…ontext.getTempDirectory()` contract (#11397)

#11396 fix ContextHandler.getTempDirectory() so it never returns null as the contract mandates

Signed-off-by: Ludovic Orban <lorban@bitronix.be>
  • Loading branch information
lorban authored Feb 21, 2024
1 parent aa5eff9 commit 3a6ad49
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ public File getTempDirectory()
{
File tempDirectory = ContextHandler.this.getTempDirectory();
if (tempDirectory == null)
tempDirectory = getServer().getTempDirectory();
tempDirectory = getServer().getContext().getTempDirectory();
return tempDirectory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,6 +45,7 @@
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
{
Expand Down Expand Up @@ -109,6 +111,13 @@ public void dispose() throws Exception
_connector = null;
}

@Test
public void testContextTempDirectory()
{
File tempDirectory = _server.getContext().getTempDirectory();
assertThat(tempDirectory, not(nullValue()));
}

@Test
public void testSimpleGET() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
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;
Expand Down Expand Up @@ -136,6 +137,17 @@ public static void afterAll()
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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,58 @@ protected void service(HttpServletRequest request, HttpServletResponse response1
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<Part> parts = request.getParts();
assertNotNull(parts);
assertEquals(1, parts.size());
Part part = parts.iterator().next();
assertEquals("part1", part.getName());
Collection<String> 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
Expand Down

0 comments on commit 3a6ad49

Please sign in to comment.