Author: markt
Date: Wed Aug 12 09:27:29 2015
New Revision: 1695459

URL: http://svn.apache.org/r1695459
Log:
Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=58228
Make behaviour of ServletContext.getResource() and 
ServletContext.getResourceAsStream() consistent with each other and the 
expected behaviour of the GET_RESOURCE_REQUIRE_SLASH system property.

Modified:
    tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java
    tomcat/trunk/webapps/docs/config/systemprops.xml

Modified: tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java?rev=1695459&r1=1695458&r2=1695459&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java 
(original)
+++ tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java Wed Aug 
12 09:27:29 2015
@@ -539,17 +539,18 @@ public class ApplicationContext
      *  in the correct form
      */
     @Override
-    public URL getResource(String path)
-        throws MalformedURLException {
+    public URL getResource(String path) throws MalformedURLException {
 
-        if (path == null ||
-                !path.startsWith("/") && GET_RESOURCE_REQUIRE_SLASH)
-            throw new MalformedURLException(sm.getString(
-                    "applicationContext.requestDispatcher.iae", path));
+        String validatedPath = validateResourcePath(path);
+
+        if (validatedPath == null) {
+            throw new MalformedURLException(
+                    sm.getString("applicationContext.requestDispatcher.iae", 
path));
+        }
 
         WebResourceRoot resources = context.getResources();
         if (resources != null) {
-            return resources.getResource(path).getURL();
+            return resources.getResource(validatedPath).getURL();
         }
 
         return null;
@@ -567,21 +568,42 @@ public class ApplicationContext
     @Override
     public InputStream getResourceAsStream(String path) {
 
-        if (path == null)
-            return (null);
+        String validatedPath = validateResourcePath(path);
 
-        if (!path.startsWith("/") && GET_RESOURCE_REQUIRE_SLASH)
+        if (validatedPath == null) {
             return null;
+        }
 
         WebResourceRoot resources = context.getResources();
         if (resources != null) {
-            return resources.getResource(path).getInputStream();
+            return resources.getResource(validatedPath).getInputStream();
         }
 
         return null;
     }
 
 
+    /*
+     * Returns null if the input path is not valid or a path that will be
+     * acceptable to resoucres.getResource().
+     */
+    private String validateResourcePath(String path) {
+        if (path == null) {
+            return null;
+        }
+
+        if (!path.startsWith("/")) {
+            if (GET_RESOURCE_REQUIRE_SLASH) {
+                return null;
+            } else {
+                return "/" + path;
+            }
+        }
+
+        return path;
+    }
+
+
     /**
      * Return a Set containing the resource paths of resources member of the
      * specified collection. Each path will be a String starting with

Modified: tomcat/trunk/webapps/docs/config/systemprops.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/systemprops.xml?rev=1695459&r1=1695458&r2=1695459&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/config/systemprops.xml (original)
+++ tomcat/trunk/webapps/docs/config/systemprops.xml Wed Aug 12 09:27:29 2015
@@ -331,7 +331,8 @@
       <code>ServletContext.getResource()</code> or
       <code>ServletContext.getResourceAsStream()</code> must start with
       &quot;/&quot;. If <code>false</code>, code like
-      <code>getResource("myfolder/myresource.txt")</code> will work.</p>
+      <code>getResource("myfolder/myresource.txt")</code> will work as Tomcat
+      will prepend &quot;/&quot; to the provided path.</p>
       <p>If <code>org.apache.catalina.STRICT_SERVLET_COMPLIANCE</code> is set 
to
       <code>true</code>, the default of this setting will be <code>true</code>,
       else the default value will be <code>false</code>.</p>



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

Reply via email to