Skip to content

Commit aba7d30

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

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

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

Lines changed: 19 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,12 @@ 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 singleton().resources.get(Pair.createRight(getResourceWithoutTrailingSlash(name)));
135128
}
136129

137130
public static ResourceStorageEntry get(String moduleName, String resourceName) {
138-
return singleton().resources.get(Pair.create(moduleName, resourceName));
131+
return singleton().resources.get(Pair.create(moduleName, getResourceWithoutTrailingSlash(resourceName)));
139132
}
140133

141134
private static URL createURL(String moduleName, String resourceName, int index) {
@@ -156,7 +149,7 @@ public static URL createURL(String moduleName, String resourceName) {
156149
return null;
157150
}
158151

159-
Enumeration<URL> urls = createURLs(moduleName, toCanonicalForm(resourceName));
152+
Enumeration<URL> urls = createURLs(moduleName, resourceName);
160153
return urls.hasMoreElements() ? urls.nextElement() : null;
161154
}
162155

@@ -170,7 +163,7 @@ public static InputStream createInputStream(String moduleName, String resourceNa
170163
return null;
171164
}
172165

173-
ResourceStorageEntry entry = Resources.get(moduleName, toCanonicalForm(resourceName));
166+
ResourceStorageEntry entry = Resources.get(moduleName, getResourceWithoutTrailingSlash(resourceName));
174167
if (entry == null) {
175168
return null;
176169
}
@@ -186,33 +179,35 @@ public static Enumeration<URL> createURLs(String moduleName, String resourceName
186179
if (resourceName == null) {
187180
return null;
188181
}
189-
String canonicalResourceName = toCanonicalForm(resourceName);
182+
String resourceNameWithoutTrailingSlash = getResourceWithoutTrailingSlash(resourceName);
190183

191184
List<URL> resourcesURLs = new ArrayList<>();
192185

193-
/* If moduleName was unspecified we have to consider all modules in the image */
186+
ResourceStorageEntry explicitEntry = Resources.get(moduleName, resourceNameWithoutTrailingSlash);
187+
addURLEntries(resourcesURLs, explicitEntry, moduleName, resourceName);
188+
/* If moduleName was unspecified we have to consider all modules in the image
189+
* We append those after the explicit entry in order to prioritize the explicit entry in case it exists
190+
*/
194191
if (moduleName == null) {
195192
for (Module module : BootModuleLayerSupport.instance().getBootLayer().modules()) {
196-
ResourceStorageEntry entry = Resources.get(module.getName(), canonicalResourceName);
197-
addURLEntries(resourcesURLs, entry, module.getName(), canonicalResourceName);
193+
ResourceStorageEntry entry = Resources.get(module.getName(), resourceNameWithoutTrailingSlash);
194+
addURLEntries(resourcesURLs, entry, module.getName(), resourceName);
198195
}
199196
}
200-
ResourceStorageEntry explicitEntry = Resources.get(moduleName, canonicalResourceName);
201-
addURLEntries(resourcesURLs, explicitEntry, moduleName, canonicalResourceName);
202197

203198
if (resourcesURLs.isEmpty()) {
204199
return Collections.emptyEnumeration();
205200
}
206201
return Collections.enumeration(resourcesURLs);
207202
}
208203

209-
private static void addURLEntries(List<URL> resourcesURLs, ResourceStorageEntry entry, String moduleName, String canonicalResourceName) {
204+
private static void addURLEntries(List<URL> resourcesURLs, ResourceStorageEntry entry, String moduleName, String resourceName) {
210205
if (entry == null) {
211206
return;
212207
}
213208
int numberOfResources = entry.getData().size();
214209
for (int index = 0; index < numberOfResources; index++) {
215-
resourcesURLs.add(createURL(moduleName, canonicalResourceName, index));
210+
resourcesURLs.add(createURL(moduleName, resourceName, index));
216211
}
217212
}
218213
}

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)