Skip to content

Commit c5298bd

Browse files
authored
Merge pull request #1283 from baranowb/UNDERTOW-2012
[UNDERTOW-2012] create temp directory for uploads
2 parents ad3c5db + b8672c0 commit c5298bd

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

core/src/main/java/io/undertow/UndertowMessages.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.IOException;
2222
import java.nio.channels.ClosedChannelException;
23+
import java.nio.file.Path;
2324

2425
import javax.net.ssl.SSLException;
2526
import javax.net.ssl.SSLHandshakeException;
@@ -636,4 +637,10 @@ public interface UndertowMessages {
636637

637638
@Message(id = 204, value = "Out of flow control window: no WINDOW_UPDATE received from peer within %s miliseconds")
638639
IOException noWindowUpdate(long timeoutMiliseconds);
640+
641+
@Message(id = 205, value = "Path is not a directory '%s'")
642+
IOException pathNotADirectory(Path path);
643+
644+
@Message(id = 206, value = "Path '%s' is not a directory")
645+
IOException pathElementIsRegularFile(Path path);
639646
}

core/src/main/java/io/undertow/server/handlers/form/MultiPartParserDefinition.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@
4444
import java.nio.channels.FileChannel;
4545
import java.nio.charset.StandardCharsets;
4646
import java.nio.file.Files;
47+
import java.nio.file.LinkOption;
4748
import java.nio.file.NoSuchFileException;
4849
import java.nio.file.Path;
4950
import java.nio.file.Paths;
5051
import java.nio.file.StandardOpenOption;
52+
import java.nio.file.attribute.FileAttribute;
5153
import java.util.ArrayList;
5254
import java.util.Arrays;
55+
import java.util.LinkedList;
5356
import java.util.List;
5457
import java.util.concurrent.Executor;
5558

@@ -251,7 +254,42 @@ public void beginPart(final HeaderMap headers) {
251254
if (fileName != null && fileSizeThreshold == 0) {
252255
try {
253256
if (tempFileLocation != null) {
254-
file = Files.createTempFile(tempFileLocation, "undertow", "upload");
257+
//Files impl is buggy, hence zero len
258+
final FileAttribute[] emptyFA = new FileAttribute[] {};
259+
final LinkOption[] emptyLO = new LinkOption[] {};
260+
final Path normalized = tempFileLocation.normalize();
261+
if (!Files.exists(normalized)) {
262+
final int pathElementsCount = normalized.getNameCount();
263+
Path tmp = normalized;
264+
LinkedList<Path> dirsToGuard = new LinkedList<>();
265+
for(int i=0;i<pathElementsCount;i++) {
266+
if(Files.exists(tmp, emptyLO)) {
267+
if(!Files.isDirectory(tmp, emptyLO)) {
268+
//First existing element in path is a file,
269+
//this will cause java.nio.file.FileSystemException
270+
throw UndertowMessages.MESSAGES.pathElementIsRegularFile(tmp);
271+
}
272+
break;
273+
} else {
274+
dirsToGuard.addFirst(tmp);
275+
tmp = tmp.getParent();
276+
}
277+
}
278+
try {
279+
Files.createDirectories(normalized, emptyFA);
280+
} finally {
281+
for (Path p : dirsToGuard) {
282+
try {
283+
p.toFile().deleteOnExit();
284+
} catch (Exception e) {
285+
break;
286+
}
287+
}
288+
}
289+
} else if (!Files.isDirectory(normalized, emptyLO)) {
290+
throw new IOException(UndertowMessages.MESSAGES.pathNotADirectory(normalized));
291+
}
292+
file = Files.createTempFile(normalized, "undertow", "upload");
255293
} else {
256294
file = Files.createTempFile("undertow", "upload");
257295
}

0 commit comments

Comments
 (0)