Commit 624a5f82 authored by Phillip Webb's avatar Phillip Webb

Apply URL decoding for nested jar entry names

Update `JarURLConnection` to apply URL decoding to nested jar entry
names.

Fixes gh-12765
parent f08c4968
...@@ -258,10 +258,10 @@ final class JarURLConnection extends java.net.JarURLConnection { ...@@ -258,10 +258,10 @@ final class JarURLConnection extends java.net.JarURLConnection {
int separator; int separator;
int index = 0; int index = 0;
while ((separator = spec.indexOf(SEPARATOR, index)) > 0) { while ((separator = spec.indexOf(SEPARATOR, index)) > 0) {
String entryName = spec.substring(index, separator); JarEntryName entryName = JarEntryName.get(spec.substring(index, separator));
JarEntry jarEntry = jarFile.getJarEntry(entryName); JarEntry jarEntry = jarFile.getJarEntry(entryName.toString());
if (jarEntry == null) { if (jarEntry == null) {
return JarURLConnection.notFound(jarFile, JarEntryName.get(entryName)); return JarURLConnection.notFound(jarFile, entryName);
} }
jarFile = jarFile.getNestedJarFile(jarEntry); jarFile = jarFile.getNestedJarFile(jarEntry);
index += separator + SEPARATOR.length(); index += separator + SEPARATOR.length();
......
...@@ -52,6 +52,7 @@ public abstract class TestJarCreator { ...@@ -52,6 +52,7 @@ public abstract class TestJarCreator {
writeNestedEntry("nested.jar", unpackNested, jarOutputStream); writeNestedEntry("nested.jar", unpackNested, jarOutputStream);
writeNestedEntry("another-nested.jar", unpackNested, jarOutputStream); writeNestedEntry("another-nested.jar", unpackNested, jarOutputStream);
writeNestedEntry("space nested.jar", unpackNested, jarOutputStream);
} }
finally { finally {
jarOutputStream.close(); jarOutputStream.close();
......
...@@ -108,7 +108,7 @@ public class ExplodedArchiveTests { ...@@ -108,7 +108,7 @@ public class ExplodedArchiveTests {
@Test @Test
public void getEntries() throws Exception { public void getEntries() throws Exception {
Map<String, Archive.Entry> entries = getEntriesMap(this.archive); Map<String, Archive.Entry> entries = getEntriesMap(this.archive);
assertThat(entries.size()).isEqualTo(10); assertThat(entries.size()).isEqualTo(11);
} }
@Test @Test
......
...@@ -82,7 +82,7 @@ public class JarFileArchiveTests { ...@@ -82,7 +82,7 @@ public class JarFileArchiveTests {
@Test @Test
public void getEntries() throws Exception { public void getEntries() throws Exception {
Map<String, Archive.Entry> entries = getEntriesMap(this.archive); Map<String, Archive.Entry> entries = getEntriesMap(this.archive);
assertThat(entries.size()).isEqualTo(10); assertThat(entries.size()).isEqualTo(11);
} }
@Test @Test
......
...@@ -90,6 +90,7 @@ public class CentralDirectoryParserTests { ...@@ -90,6 +90,7 @@ public class CentralDirectoryParserTests {
assertThat(headers.next().getName().toString()).isEqualTo("special/\u00EB.dat"); assertThat(headers.next().getName().toString()).isEqualTo("special/\u00EB.dat");
assertThat(headers.next().getName().toString()).isEqualTo("nested.jar"); assertThat(headers.next().getName().toString()).isEqualTo("nested.jar");
assertThat(headers.next().getName().toString()).isEqualTo("another-nested.jar"); assertThat(headers.next().getName().toString()).isEqualTo("another-nested.jar");
assertThat(headers.next().getName().toString()).isEqualTo("space nested.jar");
assertThat(headers.hasNext()).isFalse(); assertThat(headers.hasNext()).isFalse();
} }
......
...@@ -93,6 +93,7 @@ public class JarFileTests { ...@@ -93,6 +93,7 @@ public class JarFileTests {
assertThat(entries.nextElement().getName()).isEqualTo("special/\u00EB.dat"); assertThat(entries.nextElement().getName()).isEqualTo("special/\u00EB.dat");
assertThat(entries.nextElement().getName()).isEqualTo("nested.jar"); assertThat(entries.nextElement().getName()).isEqualTo("nested.jar");
assertThat(entries.nextElement().getName()).isEqualTo("another-nested.jar"); assertThat(entries.nextElement().getName()).isEqualTo("another-nested.jar");
assertThat(entries.nextElement().getName()).isEqualTo("space nested.jar");
assertThat(entries.hasMoreElements()).isFalse(); assertThat(entries.hasMoreElements()).isFalse();
URL jarUrl = new URL("jar:" + this.rootJarFile.toURI() + "!/"); URL jarUrl = new URL("jar:" + this.rootJarFile.toURI() + "!/");
URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { jarUrl }); URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { jarUrl });
...@@ -135,6 +136,7 @@ public class JarFileTests { ...@@ -135,6 +136,7 @@ public class JarFileTests {
assertThat(entries.nextElement().getName()).isEqualTo("special/\u00EB.dat"); assertThat(entries.nextElement().getName()).isEqualTo("special/\u00EB.dat");
assertThat(entries.nextElement().getName()).isEqualTo("nested.jar"); assertThat(entries.nextElement().getName()).isEqualTo("nested.jar");
assertThat(entries.nextElement().getName()).isEqualTo("another-nested.jar"); assertThat(entries.nextElement().getName()).isEqualTo("another-nested.jar");
assertThat(entries.nextElement().getName()).isEqualTo("space nested.jar");
assertThat(entries.hasMoreElements()).isFalse(); assertThat(entries.hasMoreElements()).isFalse();
} }
......
...@@ -137,6 +137,21 @@ public class JarURLConnectionTests { ...@@ -137,6 +137,21 @@ public class JarURLConnectionTests {
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 })); .hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
} }
@Test
public void connectionToEntryWithSpaceNestedEntry() throws Exception {
URL url = new URL("jar:file:" + getRelativePath() + "!/space nested.jar!/3.dat");
assertThat(JarURLConnection.get(url, this.jarFile).getInputStream())
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
}
@Test
public void connectionToEntryWithEncodedSpaceNestedEntry() throws Exception {
URL url = new URL(
"jar:file:" + getRelativePath() + "!/space%20nested.jar!/3.dat");
assertThat(JarURLConnection.get(url, this.jarFile).getInputStream())
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
}
@Test @Test
public void getContentLengthReturnsLengthOfUnderlyingEntry() throws Exception { public void getContentLengthReturnsLengthOfUnderlyingEntry() throws Exception {
URL url = new URL(new URL("jar", null, -1, URL url = new URL(new URL("jar", null, -1,
......
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