Skip to content

Commit

Permalink
Fixes #11064 - Jetty 12: NPE if MultiPartFormData.setFilesDirectory()…
Browse files Browse the repository at this point in the history
… is not called.

Now trying to use $JETTY_BASE/work, otherwise throwing IllegalArgumentException with a descriptive message.

Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed Dec 14, 2023
1 parent 3681d77 commit 8f019b9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,21 @@ public void setFilesDirectory(Path filesDirectory)
this.filesDirectory = filesDirectory;
}

private Path findFilesDirectory()
{
Path dir = getFilesDirectory();
if (dir != null)
return dir;
String jettyBase = System.getProperty("jetty.base");
if (jettyBase != null)
{
dir = Path.of(jettyBase).resolve("work");
if (Files.exists(dir))
return dir;
}
throw new IllegalArgumentException("No files directory configured");
}

/**
* @return the maximum file size in bytes, or -1 for unlimited file size
*/
Expand Down Expand Up @@ -627,7 +642,7 @@ private void createFileChannel()
{
try (AutoLock ignored = lock.lock())
{
Path directory = getFilesDirectory();
Path directory = findFilesDirectory();
Files.createDirectories(directory);
String fileName = "MultiPart";
filePath = Files.createTempFile(directory, fileName, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,28 @@ public void testTerminalFailureIsTerminal()
assertThat(chunk.getFailure(), instanceOf(NumberFormatException.class));
}

@Test
public void testMissingFilesDirectory()
{
AsyncContent source = new TestContent();
MultiPartFormData.Parser formData = new MultiPartFormData.Parser("AaB03x");
// Always save to disk.
formData.setMaxMemoryFileSize(0);

String body = """
--AaB03x\r
Content-Disposition: form-data; name="file1"; filename="file.txt"\r
Content-Type: text/plain\r
\r
ABCDEFGHIJKLMNOPQRSTUVWXYZ\r
--AaB03x--\r
""";
Content.Sink.write(source, true, body, Callback.NOOP);

Throwable cause = assertThrows(ExecutionException.class, () -> formData.parse(source).get(5, TimeUnit.SECONDS)).getCause();
assertInstanceOf(IllegalArgumentException.class, cause);
}

private class TestContent extends AsyncContent
{
@Override
Expand Down

0 comments on commit 8f019b9

Please sign in to comment.