Commit 7d59b788 authored by Andy Wilkinson's avatar Andy Wilkinson

Fix handling of jar files with + chars in their path

Closes gh-17208
parent 4894affb
...@@ -20,9 +20,9 @@ import java.io.File; ...@@ -20,9 +20,9 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLStreamHandler; import java.net.URLStreamHandler;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
...@@ -302,8 +302,7 @@ public class Handler extends URLStreamHandler { ...@@ -302,8 +302,7 @@ public class Handler extends URLStreamHandler {
if (!name.startsWith(FILE_PROTOCOL)) { if (!name.startsWith(FILE_PROTOCOL)) {
throw new IllegalStateException("Not a file URL"); throw new IllegalStateException("Not a file URL");
} }
String path = name.substring(FILE_PROTOCOL.length()); File file = new File(URI.create(name));
File file = new File(URLDecoder.decode(path, "UTF-8"));
Map<File, JarFile> cache = rootFileCache.get(); Map<File, JarFile> cache = rootFileCache.get();
JarFile result = (cache != null) ? cache.get(file) : null; JarFile result = (cache != null) ? cache.get(file) : null;
if (result == null) { if (result == null) {
......
...@@ -167,6 +167,34 @@ public class HandlerTests { ...@@ -167,6 +167,34 @@ public class HandlerTests {
assertThat(jdkConnection).isNotInstanceOf(JarURLConnection.class); assertThat(jdkConnection).isNotInstanceOf(JarURLConnection.class);
} }
@Test
public void whenJarHasAPlusInItsPathConnectionJarFileMatchesOriginalJarFile() throws Exception {
File testJar = this.temporaryFolder.newFile("t+e+s+t.jar");
TestJarCreator.createTestJar(testJar);
URL url = new URL(null, "jar:" + testJar.toURI().toURL() + "!/nested.jar!/3.dat", this.handler);
JarURLConnection connection = (JarURLConnection) url.openConnection();
try {
assertThat(connection.getJarFile().getRootJarFile().getFile()).isEqualTo(testJar);
}
finally {
connection.getJarFile().close();
}
}
@Test
public void whenJarHasASpaceInItsPathConnectionJarFileMatchesOriginalJarFile() throws Exception {
File testJar = this.temporaryFolder.newFile("t e s t.jar");
TestJarCreator.createTestJar(testJar);
URL url = new URL(null, "jar:" + testJar.toURI().toURL() + "!/nested.jar!/3.dat", this.handler);
JarURLConnection connection = (JarURLConnection) url.openConnection();
try {
assertThat(connection.getJarFile().getRootJarFile().getFile()).isEqualTo(testJar);
}
finally {
connection.getJarFile().close();
}
}
private void assertStandardAndCustomHandlerUrlsAreEqual(String context, String spec) throws MalformedURLException { private void assertStandardAndCustomHandlerUrlsAreEqual(String context, String spec) throws MalformedURLException {
URL standardUrl = new URL(new URL("jar:" + context), spec); URL standardUrl = new URL(new URL("jar:" + context), spec);
URL customHandlerUrl = new URL(new URL("jar", null, -1, context, this.handler), spec); URL customHandlerUrl = new URL(new URL("jar", null, -1, context, this.handler), spec);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment