This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/9.0.x by this push: new 354d6e8 Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=64074 354d6e8 is described below commit 354d6e8938c918dbfba21e99fdd663609947864e Author: Mark Thomas <ma...@apache.org> AuthorDate: Tue Jan 14 17:09:01 2020 +0000 Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=64074 InputStreams for directories obtained from resource URLs now return a directory listing consistent with the behaviour of FileURLConnection. In addition to restoring the behaviour that was lost as a result of the introduction of CachedResourceURLConnection, it expands the feature to include packedWARs and to take account of resource JARs. --- .../catalina/webresources/CachedResource.java | 34 ++++++++++++- .../catalina/webresources/TestCachedResource.java | 57 +++++++++++++++++++++- webapps/docs/changelog.xml | 8 +++ 3 files changed, 96 insertions(+), 3 deletions(-) diff --git a/java/org/apache/catalina/webresources/CachedResource.java b/java/org/apache/catalina/webresources/CachedResource.java index 712a463..b77862a 100644 --- a/java/org/apache/catalina/webresources/CachedResource.java +++ b/java/org/apache/catalina/webresources/CachedResource.java @@ -24,8 +24,12 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; +import java.nio.charset.Charset; import java.security.Permission; import java.security.cert.Certificate; +import java.text.Collator; +import java.util.Arrays; +import java.util.Locale; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.Manifest; @@ -412,6 +416,22 @@ public class CachedResource implements WebResource { } + /* + * Mimics the behaviour of FileURLConnection.getInputStream for a directory. + * Deliberately uses default locale. + */ + private static InputStream buildInputStream(String[] files) { + Arrays.sort(files, Collator.getInstance(Locale.getDefault())); + StringBuilder result = new StringBuilder(); + for (String file : files) { + result.append(file); + // Every entry is followed by \n including the last + result.append('\n'); + } + return new ByteArrayInputStream(result.toString().getBytes(Charset.defaultCharset())); + } + + private static class CachedResourceURLStreamHandler extends URLStreamHandler { private final URL resourceURL; @@ -480,7 +500,12 @@ public class CachedResource implements WebResource { @Override public InputStream getInputStream() throws IOException { - return getResource().getInputStream(); + WebResource resource = getResource(); + if (resource.isDirectory()) { + return buildInputStream(resource.getWebResourceRoot().list(webAppPath)); + } else { + return getResource().getInputStream(); + } } @Override @@ -531,7 +556,12 @@ public class CachedResource implements WebResource { @Override public InputStream getInputStream() throws IOException { - return getResource().getInputStream(); + WebResource resource = getResource(); + if (resource.isDirectory()) { + return buildInputStream(resource.getWebResourceRoot().list(webAppPath)); + } else { + return getResource().getInputStream(); + } } @Override diff --git a/test/org/apache/catalina/webresources/TestCachedResource.java b/test/org/apache/catalina/webresources/TestCachedResource.java index 0215349..7e890c7 100644 --- a/test/org/apache/catalina/webresources/TestCachedResource.java +++ b/test/org/apache/catalina/webresources/TestCachedResource.java @@ -26,6 +26,7 @@ import org.junit.Test; import org.apache.catalina.Context; import org.apache.catalina.WebResourceRoot; +import org.apache.catalina.core.StandardHost; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.TomcatBaseTest; @@ -63,7 +64,7 @@ public class TestCachedResource extends TomcatBaseTest { WebResourceRoot root = ctx.getResources(); - // WAR contains a resoucres JAR so this should return a JAR URL + // WAR contains a resources JAR so this should return a JAR URL URL webinf = root.getResource("/index.html").getURL(); Assert.assertEquals("jar", webinf.getProtocol()); @@ -76,4 +77,58 @@ public class TestCachedResource extends TomcatBaseTest { Assert.assertNotNull(jarConn); } + + @Test + public void testDirectoryListingsPackedWar() throws Exception { + + Tomcat tomcat = getTomcatInstance(); + File docBase = new File("test/webresources/war-url-connection.war"); + Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath()); + ((StandardHost) tomcat.getHost()).setUnpackWARs(false); + tomcat.start(); + + WebResourceRoot root = ctx.getResources(); + + URL d1 = root.getResource("/").getURL(); + + try (InputStream is = d1.openStream()) { + Assert.assertNotNull(is); + } + } + + + @Test + public void testDirectoryListingsWar() throws Exception { + + Tomcat tomcat = getTomcatInstance(); + File docBase = new File("test/webresources/war-url-connection.war"); + Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath()); + tomcat.start(); + + WebResourceRoot root = ctx.getResources(); + + URL d1 = root.getResource("/").getURL(); + + try (InputStream is = d1.openStream()) { + Assert.assertNotNull(is); + } + } + + + @Test + public void testDirectoryListingsDir() throws Exception { + + Tomcat tomcat = getTomcatInstance(); + File docBase = new File("test/webresources/dir1"); + Context ctx = tomcat.addWebapp("/test", docBase.getAbsolutePath()); + tomcat.start(); + + WebResourceRoot root = ctx.getResources(); + + URL d1 = root.getResource("/d1").getURL(); + + try (InputStream is = d1.openStream()) { + Assert.assertNotNull(is); + } + } } diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index dafce54..dacf6f5 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -100,6 +100,14 @@ <bug>64067</bug>: Allow more than one parameter when defining RewriteMaps. (fschumacher) </update> + <fix> + <bug>64074</bug>: <code>InputStream</code>s for directories obtained + from resource URLs now return a directory listing consistent with the + behaviour of <code>FileURLConnection</code>. In addition to restoring + the behaviour that was lost as a result of the introduction of + <code>CachedResourceURLConnection</code>, it expands the feature to + include packedWARs and to take account of resource JARs. (markt) + </fix> </changelog> </subsection> <subsection name="Coyote"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org