Skip to content

Commit

Permalink
Merge pull request #43694 from gsmet/undertow-load-known
Browse files Browse the repository at this point in the history
Undertow - Only load resources that are known
  • Loading branch information
gsmet authored Oct 14, 2024
2 parents fa93db7 + 8d5680a commit 46f6063
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.NavigableSet;
import java.util.Set;
import java.util.SortedSet;
Expand All @@ -25,8 +24,6 @@

public class KnownPathResourceManager implements ResourceManager {

public static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows");

private final NavigableSet<String> files;
private final NavigableSet<String> directories;
private final ResourceManager underlying;
Expand Down Expand Up @@ -69,6 +66,9 @@ public Resource getResource(String path) throws IOException {
if (directories.contains(path)) {
return new DirectoryResource(path);
}
if (!files.contains(path)) {
return null;
}
return underlying.getResource(path);
}

Expand All @@ -86,7 +86,7 @@ private DirectoryResource(String path) {
}

private String evaluatePath(String path) {
return path.replaceAll("\\\\", "/");
return path.replace('\\', '/');
}

@Override
Expand Down Expand Up @@ -127,9 +127,7 @@ public boolean isDirectory() {
public List<Resource> list() {
List<Resource> ret = new ArrayList<>();
String slashPath = path.isEmpty() ? path : path + "/";
if (IS_WINDOWS) {
slashPath = slashPath.replaceAll("/", "\\\\"); // correct Windows paths
}

SortedSet<String> fileSet = files.tailSet(slashPath);
SortedSet<String> dirSet = directories.tailSet(slashPath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public static void setHotDeploymentResources(List<Path> resources) {
hotDeploymentResourcePaths = resources;
}

public RuntimeValue<DeploymentInfo> createDeployment(String name, Set<String> knownFile, Set<String> knownDirectories,
public RuntimeValue<DeploymentInfo> createDeployment(String name, Set<String> knownFiles, Set<String> knownDirectories,
LaunchMode launchMode, ShutdownContext context, String mountPoint, String defaultCharset,
String requestCharacterEncoding, String responseCharacterEncoding, boolean proactiveAuth,
List<String> welcomeFiles, final boolean hasSecurityCapability) {
Expand All @@ -188,7 +188,7 @@ public RuntimeValue<DeploymentInfo> createDeployment(String name, Set<String> kn
//TODO: we need better handling of static resources
ResourceManager resourceManager;
if (hotDeploymentResourcePaths == null) {
resourceManager = new KnownPathResourceManager(knownFile, knownDirectories,
resourceManager = new KnownPathResourceManager(knownFiles, knownDirectories,
new ClassPathResourceManager(d.getClassLoader(), "META-INF/resources"));
} else {
List<ResourceManager> managers = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.runtime.LaunchMode;
import io.smallrye.common.os.OS;

/**
* NOTE: Shared with Resteasy standalone!
Expand Down Expand Up @@ -103,19 +104,27 @@ void scanStaticResources(Capabilities capabilities, ApplicationArchivesBuildItem
private void collectKnownPaths(Path resource, Set<String> knownFiles, Set<String> knownDirectories) {
try {
Files.walkFileTree(resource, new SimpleFileVisitor<Path>() {

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs)
throws IOException {
knownFiles.add(resource.relativize(file).toString());
knownFiles.add(normalizePath(resource.relativize(path).toString()));
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult preVisitDirectory(Path file, BasicFileAttributes attrs)
public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs)
throws IOException {
knownDirectories.add(resource.relativize(file).toString());
knownDirectories.add(normalizePath(resource.relativize(path).toString()));
return FileVisitResult.CONTINUE;
}

private String normalizePath(String path) {
if (OS.WINDOWS.isCurrent()) {
path = path.replace('\\', '/');
}
return path;
}
});
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down

0 comments on commit 46f6063

Please sign in to comment.