You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2020/01/14 17:09:25 UTC

[tomcat] branch master updated: Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=64074

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/master by this push:
     new e6f26fa  Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=64074
e6f26fa is described below

commit e6f26fa9d5daf18e994787de65b150f0fc03f29d
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 +++++++++++++++++++++-
 2 files changed, 88 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);
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Re: [tomcat] branch master updated: Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=64074

Posted by Michael Osipov <mi...@apache.org>.
Am 2020-01-14 um 19:55 schrieb Mark Thomas:
> On 14/01/2020 18:49, Michael Osipov wrote:
>> Am 2020-01-14 um 18:09 schrieb markt@apache.org:
>>> This is an automated email from the ASF dual-hosted git repository.
>>>
>>> markt pushed a commit to branch master
>>> in repository https://gitbox.apache.org/repos/asf/tomcat.git
>>>
>>>
>>> The following commit(s) were added to refs/heads/master by this push:
>>>        new e6f26fa  Fix
>>> https://bz.apache.org/bugzilla/show_bug.cgi?id=64074
>>> e6f26fa is described below
>>>
>>> commit e6f26fa9d5daf18e994787de65b150f0fc03f29d
>>> 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
>>> +++++++++++++++++++++-
>>>    2 files changed, 88 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');
>>
>> Why didn't you choose System.lineSeparator()?
> 
> See the method level comment.

I was afraid of that ;-)


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Re: [tomcat] branch master updated: Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=64074

Posted by Mark Thomas <ma...@apache.org>.
On 14/01/2020 18:49, Michael Osipov wrote:
> Am 2020-01-14 um 18:09 schrieb markt@apache.org:
>> This is an automated email from the ASF dual-hosted git repository.
>>
>> markt pushed a commit to branch master
>> in repository https://gitbox.apache.org/repos/asf/tomcat.git
>>
>>
>> The following commit(s) were added to refs/heads/master by this push:
>>       new e6f26fa  Fix
>> https://bz.apache.org/bugzilla/show_bug.cgi?id=64074
>> e6f26fa is described below
>>
>> commit e6f26fa9d5daf18e994787de65b150f0fc03f29d
>> 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
>> +++++++++++++++++++++-
>>   2 files changed, 88 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');
> 
> Why didn't you choose System.lineSeparator()?

See the method level comment.

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Re: [tomcat] branch master updated: Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=64074

Posted by Michael Osipov <mi...@apache.org>.
Am 2020-01-14 um 18:09 schrieb markt@apache.org:
> This is an automated email from the ASF dual-hosted git repository.
> 
> markt pushed a commit to branch master
> in repository https://gitbox.apache.org/repos/asf/tomcat.git
> 
> 
> The following commit(s) were added to refs/heads/master by this push:
>       new e6f26fa  Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=64074
> e6f26fa is described below
> 
> commit e6f26fa9d5daf18e994787de65b150f0fc03f29d
> 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 +++++++++++++++++++++-
>   2 files changed, 88 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');

Why didn't you choose System.lineSeparator()?

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org