Skip to content

Commit 8031a1b

Browse files
alibk0rdwilkinsona
authored andcommitted
Fix handling of spaces in container's document root
See gh-10706
1 parent 276a9a0 commit 8031a1b

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

spring-boot/src/main/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactory.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.File;
2020
import java.io.IOException;
2121
import java.net.JarURLConnection;
22+
import java.net.URISyntaxException;
2223
import java.net.URL;
2324
import java.net.URLClassLoader;
2425
import java.net.URLConnection;
@@ -87,7 +88,7 @@ else if (this.logger.isDebugEnabled()) {
8788
}
8889

8990
private File getExplodedWarFileDocumentRoot() {
90-
return getExplodedWarFileDocumentRoot(getCodeSourceArchive());
91+
return getExplodedWarFileDocumentRoot(getCodeSourceArchive(getCodeSource()));
9192
}
9293

9394
protected List<URL> getUrlsOfJarsWithMetaInfResources() {
@@ -172,7 +173,7 @@ private File getWarFileDocumentRoot() {
172173
}
173174

174175
private File getArchiveFileDocumentRoot(String extension) {
175-
File file = getCodeSourceArchive();
176+
File file = getCodeSourceArchive(getCodeSource());
176177
if (this.logger.isDebugEnabled()) {
177178
this.logger.debug("Code archive: " + file);
178179
}
@@ -193,26 +194,35 @@ private File getCommonDocumentRoot() {
193194
return null;
194195
}
195196

196-
private File getCodeSourceArchive() {
197+
File getCodeSourceArchive(CodeSource codeSource) {
197198
try {
198-
CodeSource codeSource = getClass().getProtectionDomain().getCodeSource();
199199
URL location = (codeSource == null ? null : codeSource.getLocation());
200200
if (location == null) {
201201
return null;
202202
}
203-
String path = location.getPath();
203+
String path;
204204
URLConnection connection = location.openConnection();
205205
if (connection instanceof JarURLConnection) {
206206
path = ((JarURLConnection) connection).getJarFile().getName();
207207
}
208-
if (path.indexOf("!/") != -1) {
208+
else {
209+
path = location.toURI().getPath();
210+
}
211+
if (path.contains("!/")) {
209212
path = path.substring(0, path.indexOf("!/"));
210213
}
211214
return new File(path);
212215
}
213216
catch (IOException ex) {
214217
return null;
215218
}
219+
catch (URISyntaxException e) {
220+
return null;
221+
}
222+
}
223+
224+
private CodeSource getCodeSource() {
225+
return getClass().getProtectionDomain().getCodeSource();
216226
}
217227

218228
protected final File getValidSessionStoreDir() {

spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
import java.net.URISyntaxException;
3131
import java.net.URL;
3232
import java.nio.charset.Charset;
33+
import java.security.CodeSource;
3334
import java.security.KeyStore;
3435
import java.security.KeyStoreException;
3536
import java.security.NoSuchAlgorithmException;
37+
import java.security.cert.Certificate;
3638
import java.security.cert.CertificateException;
3739
import java.security.cert.X509Certificate;
3840
import java.util.Arrays;
@@ -673,6 +675,22 @@ public void cannotReadClassPathFiles() throws Exception {
673675
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
674676
}
675677

678+
@Test
679+
public void codeSourceArchivePath() throws Exception {
680+
AbstractEmbeddedServletContainerFactory factory = getFactory();
681+
final CodeSource codeSource = new CodeSource(new URL("file", "", "/some/test/path/"), (Certificate[]) null);
682+
final File codeSourceArchive = factory.getCodeSourceArchive(codeSource);
683+
assertThat(codeSourceArchive).isEqualTo(new File("/some/test/path/"));
684+
}
685+
686+
@Test
687+
public void codeSourceArchivePathContainingSpaces() throws Exception {
688+
AbstractEmbeddedServletContainerFactory factory = getFactory();
689+
final CodeSource codeSource = new CodeSource(new URL("file", "", "/test/path/with%20space/"), (Certificate[]) null);
690+
final File codeSourceArchive = factory.getCodeSourceArchive(codeSource);
691+
assertThat(codeSourceArchive).isEqualTo(new File("/test/path/with space/"));
692+
}
693+
676694
protected Ssl getSsl(ClientAuth clientAuth, String keyPassword, String keyStore) {
677695
return getSsl(clientAuth, keyPassword, keyStore, null, null, null);
678696
}

0 commit comments

Comments
 (0)