Skip to content

Commit

Permalink
Merge pull request #1283 from baranowb/UNDERTOW-2012
Browse files Browse the repository at this point in the history
[UNDERTOW-2012] create temp directory for uploads
  • Loading branch information
fl4via committed Feb 6, 2022
2 parents ad3c5db + b8672c0 commit c5298bd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
7 changes: 7 additions & 0 deletions core/src/main/java/io/undertow/UndertowMessages.java
Expand Up @@ -20,6 +20,7 @@

import java.io.IOException;
import java.nio.channels.ClosedChannelException;
import java.nio.file.Path;

import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
Expand Down Expand Up @@ -636,4 +637,10 @@ public interface UndertowMessages {

@Message(id = 204, value = "Out of flow control window: no WINDOW_UPDATE received from peer within %s miliseconds")
IOException noWindowUpdate(long timeoutMiliseconds);

@Message(id = 205, value = "Path is not a directory '%s'")
IOException pathNotADirectory(Path path);

@Message(id = 206, value = "Path '%s' is not a directory")
IOException pathElementIsRegularFile(Path path);
}
Expand Up @@ -44,12 +44,15 @@
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileAttribute;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executor;

Expand Down Expand Up @@ -251,7 +254,42 @@ public void beginPart(final HeaderMap headers) {
if (fileName != null && fileSizeThreshold == 0) {
try {
if (tempFileLocation != null) {
file = Files.createTempFile(tempFileLocation, "undertow", "upload");
//Files impl is buggy, hence zero len
final FileAttribute[] emptyFA = new FileAttribute[] {};
final LinkOption[] emptyLO = new LinkOption[] {};
final Path normalized = tempFileLocation.normalize();
if (!Files.exists(normalized)) {
final int pathElementsCount = normalized.getNameCount();
Path tmp = normalized;
LinkedList<Path> dirsToGuard = new LinkedList<>();
for(int i=0;i<pathElementsCount;i++) {
if(Files.exists(tmp, emptyLO)) {
if(!Files.isDirectory(tmp, emptyLO)) {
//First existing element in path is a file,
//this will cause java.nio.file.FileSystemException
throw UndertowMessages.MESSAGES.pathElementIsRegularFile(tmp);
}
break;
} else {
dirsToGuard.addFirst(tmp);
tmp = tmp.getParent();
}
}
try {
Files.createDirectories(normalized, emptyFA);
} finally {
for (Path p : dirsToGuard) {
try {
p.toFile().deleteOnExit();
} catch (Exception e) {
break;
}
}
}
} else if (!Files.isDirectory(normalized, emptyLO)) {
throw new IOException(UndertowMessages.MESSAGES.pathNotADirectory(normalized));
}
file = Files.createTempFile(normalized, "undertow", "upload");
} else {
file = Files.createTempFile("undertow", "upload");
}
Expand Down

0 comments on commit c5298bd

Please sign in to comment.