Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #11064 - Jetty 12: NPE if MultiPartFormData.setFilesDirectory()… #11065

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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