Commit 6a644e2e authored by Phillip Webb's avatar Phillip Webb

Protect against malformed URLs on Windows

Update JarFile to correctly create system independent URLs to prevent
potential URISyntaxExceptions when running on Windows.

Fixes gh-836
parent 845a86d5
...@@ -365,11 +365,6 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<JarEntryD ...@@ -365,11 +365,6 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<JarEntryD
throw new IllegalArgumentException("ZipEntry must be contained in this file"); throw new IllegalArgumentException("ZipEntry must be contained in this file");
} }
@Override
public String getName() {
return this.name;
}
@Override @Override
public int size() { public int size() {
return (int) this.size; return (int) this.size;
...@@ -380,11 +375,6 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<JarEntryD ...@@ -380,11 +375,6 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<JarEntryD
this.rootFile.close(); this.rootFile.close();
} }
@Override
public String toString() {
return getName();
}
/** /**
* Return a URL that can be used to access this JAR file. NOTE: the specified URL * Return a URL that can be used to access this JAR file. NOTE: the specified URL
* cannot be serialized and or cloned. * cannot be serialized and or cloned.
...@@ -393,7 +383,26 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<JarEntryD ...@@ -393,7 +383,26 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<JarEntryD
*/ */
public URL getUrl() throws MalformedURLException { public URL getUrl() throws MalformedURLException {
Handler handler = new Handler(this); Handler handler = new Handler(this);
return new URL("jar", "", -1, "file:" + getName() + "!/", handler); String file = "file:" + getName(PathForm.SYSTEM_INDEPENDENT) + "!/";
return new URL("jar", "", -1, file, handler);
}
@Override
public String toString() {
return getName();
}
@Override
public String getName() {
return getName(PathForm.SYSTEM_DEPENDENT);
}
private String getName(PathForm pathForm) {
if (pathForm == PathForm.SYSTEM_INDEPENDENT && File.separatorChar != '/') {
return this.name.replace(File.separatorChar, '/');
}
return this.name;
} }
/** /**
...@@ -420,4 +429,20 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<JarEntryD ...@@ -420,4 +429,20 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<JarEntryD
// Ignore // Ignore
} }
} }
/**
* Different forms that paths can be returned.
*/
private static enum PathForm {
/**
* Use system dependent paths (i.e. include backslashes on Windows)
*/
SYSTEM_DEPENDENT,
/**
* Use system independent paths (i.e. replace backslashes on Windows)
*/
SYSTEM_INDEPENDENT
}
} }
...@@ -92,7 +92,6 @@ public class JarFileTests { ...@@ -92,7 +92,6 @@ public class JarFileTests {
assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue()); assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue());
assertThat(urlClassLoader.getResource("d/9.dat"), notNullValue()); assertThat(urlClassLoader.getResource("d/9.dat"), notNullValue());
jarFile.close(); jarFile.close();
urlClassLoader.close();
} }
@Test @Test
...@@ -135,7 +134,6 @@ public class JarFileTests { ...@@ -135,7 +134,6 @@ public class JarFileTests {
URLClassLoader urlClassLoader = new URLClassLoader( URLClassLoader urlClassLoader = new URLClassLoader(
new URL[] { this.jarFile.getUrl() }); new URL[] { this.jarFile.getUrl() });
assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue()); assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue());
urlClassLoader.close();
} }
@Test @Test
......
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