Skip to content

Commit e034be4

Browse files
committed
Fix handling of resources with trailing slashes
Fix #4326
1 parent 0a46643 commit e034be4

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Resources.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.io.InputStream;
3030
import java.net.MalformedURLException;
3131
import java.net.URL;
32-
import java.nio.charset.StandardCharsets;
3332
import java.util.ArrayList;
3433
import java.util.Collections;
3534
import java.util.Enumeration;
@@ -43,7 +42,6 @@
4342
import org.graalvm.nativeimage.hosted.Feature;
4443

4544
import com.oracle.svm.core.annotate.AutomaticFeature;
46-
import com.oracle.svm.core.jdk.resources.NativeImageResourcePath;
4745
import com.oracle.svm.core.jdk.resources.ResourceStorageEntry;
4846
import com.oracle.svm.core.util.ImageHeapMap;
4947
import com.oracle.svm.core.util.VMError;
@@ -85,9 +83,13 @@ public static byte[] inputStreamToByteArray(InputStream is) {
8583
}
8684
}
8785

86+
private static String getResourceWithoutTrailingSlash(String name) {
87+
return name.endsWith("/") ? name.substring(0, name.length() - 1) : name;
88+
}
89+
8890
private static void addEntry(String moduleName, String resourceName, boolean isDirectory, byte[] data) {
8991
Resources support = singleton();
90-
Pair<String, String> key = Pair.create(moduleName, resourceName);
92+
Pair<String, String> key = Pair.create(moduleName, getResourceWithoutTrailingSlash(resourceName));
9193
ResourceStorageEntry entry = support.resources.get(key);
9294
if (entry == null) {
9395
entry = new ResourceStorageEntry(isDirectory);
@@ -121,21 +123,17 @@ public static void registerDirectoryResource(String moduleName, String resourceD
121123
addEntry(moduleName, resourceDirName, true, content.getBytes());
122124
}
123125

124-
/**
125-
* Avoid pulling native file system by using {@link NativeImageResourcePath} implementation to
126-
* convert <code>resourceName</code> to canonical variant.
127-
*/
128-
public static String toCanonicalForm(String resourceName) {
129-
NativeImageResourcePath path = new NativeImageResourcePath(null, resourceName.getBytes(StandardCharsets.UTF_8), true);
130-
return new String(NativeImageResourcePath.getResolved(path));
131-
}
132-
133126
public static ResourceStorageEntry get(String name) {
134-
return singleton().resources.get(Pair.createRight(name));
127+
return get(null, name);
135128
}
136129

137130
public static ResourceStorageEntry get(String moduleName, String resourceName) {
138-
return singleton().resources.get(Pair.create(moduleName, resourceName));
131+
ResourceStorageEntry resourceStorageEntry = singleton().resources.get(Pair.create(moduleName, getResourceWithoutTrailingSlash(resourceName)));
132+
if (resourceStorageEntry != null && (resourceStorageEntry.isDirectory() || !resourceName.endsWith("/"))) {
133+
return resourceStorageEntry;
134+
} else {
135+
return null;
136+
}
139137
}
140138

141139
private static URL createURL(String moduleName, String resourceName, int index) {
@@ -156,7 +154,7 @@ public static URL createURL(String moduleName, String resourceName) {
156154
return null;
157155
}
158156

159-
Enumeration<URL> urls = createURLs(moduleName, toCanonicalForm(resourceName));
157+
Enumeration<URL> urls = createURLs(moduleName, resourceName);
160158
return urls.hasMoreElements() ? urls.nextElement() : null;
161159
}
162160

@@ -170,7 +168,7 @@ public static InputStream createInputStream(String moduleName, String resourceNa
170168
return null;
171169
}
172170

173-
ResourceStorageEntry entry = Resources.get(moduleName, toCanonicalForm(resourceName));
171+
ResourceStorageEntry entry = Resources.get(moduleName, resourceName);
174172
if (entry == null) {
175173
return null;
176174
}
@@ -186,33 +184,36 @@ public static Enumeration<URL> createURLs(String moduleName, String resourceName
186184
if (resourceName == null) {
187185
return null;
188186
}
189-
String canonicalResourceName = toCanonicalForm(resourceName);
190187

191188
List<URL> resourcesURLs = new ArrayList<>();
192189

193-
/* If moduleName was unspecified we have to consider all modules in the image */
190+
ResourceStorageEntry explicitEntry = Resources.get(moduleName, resourceName);
191+
addURLEntries(resourcesURLs, explicitEntry, moduleName, resourceName);
192+
/*
193+
* If moduleName was unspecified we have to consider all modules in the image. We append
194+
* those after the explicit entry in order to prioritize the explicit entry in case it
195+
* exists
196+
*/
194197
if (moduleName == null) {
195198
for (Module module : BootModuleLayerSupport.instance().getBootLayer().modules()) {
196-
ResourceStorageEntry entry = Resources.get(module.getName(), canonicalResourceName);
197-
addURLEntries(resourcesURLs, entry, module.getName(), canonicalResourceName);
199+
ResourceStorageEntry entry = Resources.get(module.getName(), resourceName);
200+
addURLEntries(resourcesURLs, entry, module.getName(), resourceName);
198201
}
199202
}
200-
ResourceStorageEntry explicitEntry = Resources.get(moduleName, canonicalResourceName);
201-
addURLEntries(resourcesURLs, explicitEntry, moduleName, canonicalResourceName);
202203

203204
if (resourcesURLs.isEmpty()) {
204205
return Collections.emptyEnumeration();
205206
}
206207
return Collections.enumeration(resourcesURLs);
207208
}
208209

209-
private static void addURLEntries(List<URL> resourcesURLs, ResourceStorageEntry entry, String moduleName, String canonicalResourceName) {
210+
private static void addURLEntries(List<URL> resourcesURLs, ResourceStorageEntry entry, String moduleName, String resourceName) {
210211
if (entry == null) {
211212
return;
212213
}
213214
int numberOfResources = entry.getData().size();
214215
for (int index = 0; index < numberOfResources; index++) {
215-
resourcesURLs.add(createURL(moduleName, canonicalResourceName, index));
216+
resourcesURLs.add(createURL(moduleName, resourceName, index));
216217
}
217218
}
218219
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/resources/ResourceURLConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void connect() {
5858
throw new IllegalArgumentException("Empty URL path not allowed in " + JavaNetSubstitutions.RESOURCE_PROTOCOL + " URL");
5959
}
6060
String resourceName = urlPath.substring(1);
61-
ResourceStorageEntry entry = Resources.get(hostNameOrNull, Resources.toCanonicalForm(resourceName));
61+
ResourceStorageEntry entry = Resources.get(hostNameOrNull, resourceName);
6262
if (entry != null) {
6363
List<byte[]> bytes = entry.getData();
6464
String urlRef = url.getRef();

0 commit comments

Comments
 (0)