mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-15 01:07:30 +08:00
Update JarURLConnection to support jar:file://… URLs
Previously, JarURLConnection would fail when created with a URL that began with jar:file:// as the double-slash is not included in jarFile.getUrl().getFile(). This commit updates JarURLConnection to normalise the value return from url.getFile() to remove a double-slash when present. Fixes gh-5287 Closes gh-5289
This commit is contained in:
parent
d6ad4dcc11
commit
a0f76eab47
@ -73,7 +73,11 @@ class JarURLConnection extends java.net.JarURLConnection {
|
||||
// What we pass to super is ultimately ignored
|
||||
super(EMPTY_JAR_URL);
|
||||
this.url = url;
|
||||
String spec = url.getFile().substring(jarFile.getUrl().getFile().length());
|
||||
String urlFile = url.getFile();
|
||||
if (urlFile.startsWith("file://")) {
|
||||
urlFile = "file:" + urlFile.substring("file://".length());
|
||||
}
|
||||
String spec = urlFile.substring(jarFile.getUrl().getFile().length());
|
||||
int separator;
|
||||
while ((separator = spec.indexOf(SEPARATOR)) > 0) {
|
||||
jarFile = getNestedJarFile(jarFile, spec.substring(0, separator));
|
||||
|
@ -334,6 +334,22 @@ public class JarFileTests {
|
||||
assertThat(connection.getEntryName(), equalTo("2.dat"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createNonNestedUrlFromStringCreatedUsingPath() throws Exception {
|
||||
// gh-5287
|
||||
JarFile.registerUrlProtocolHandler();
|
||||
String spec = "jar:" + this.rootJarFile.toPath().toUri() + "!/2.dat";
|
||||
URL url = new URL(spec);
|
||||
assertThat(url.toString(), equalTo(spec));
|
||||
InputStream inputStream = url.openStream();
|
||||
assertThat(inputStream, notNullValue());
|
||||
assertThat(inputStream.read(), equalTo(2));
|
||||
JarURLConnection connection = (JarURLConnection) url.openConnection();
|
||||
assertThat(connection.getURL().toString(), equalTo(spec));
|
||||
assertThat(connection.getJarFileURL().toURI(), equalTo(this.rootJarFile.toURI()));
|
||||
assertThat(connection.getEntryName(), equalTo("2.dat"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDirectoryInputStream() throws Exception {
|
||||
InputStream inputStream = this.jarFile
|
||||
|
Loading…
Reference in New Issue
Block a user