Author: markt
Date: Wed Dec 10 23:24:22 2014
New Revision: 1644535

URL: http://svn.apache.org/r1644535
Log:
Follow-up to 1644529. Better fix that actually passes the unit tests.

Modified:
    tomcat/trunk/java/org/apache/catalina/connector/Request.java

Modified: tomcat/trunk/java/org/apache/catalina/connector/Request.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/connector/Request.java?rev=1644535&r1=1644534&r2=1644535&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/connector/Request.java (original)
+++ tomcat/trunk/java/org/apache/catalina/connector/Request.java Wed Dec 10 
23:24:22 2014
@@ -1881,37 +1881,53 @@ public class Request
         String uri = getRequestURI();
         char[] uriChars = uri.toCharArray();
         int lastSlash = mappingData.contextSlashCount;
+        // Special case handling for the root context
+        if (lastSlash == 0) {
+            return "";
+        }
         int pos = 0;
-        // Need at least the number of slashed in the context path
+        // Need at least the number of slashes in the context path
         while (lastSlash > 0) {
             pos = nextSlash(uriChars, pos + 1);
             if (pos == -1) {
-                // Should never happen
-                throw new IllegalStateException(sm.getString(
-                        "coyoteRequest.getContextPath.ise", 
canonicalContextPath, uri));
+                break;
             }
             lastSlash--;
         }
         // Now allow for normalization and/or encoding. Essentially, keep
         // extending the candidate path up to the next slash until the decoded
         // and normalized candidate path is the same as the canonical path.
-        String candidate = uri.substring(0, pos);
-        if (pos > 0) {
-            candidate = UDecoder.URLDecode(candidate, 
connector.getURIEncoding());
-            candidate = 
org.apache.tomcat.util.http.RequestUtil.normalize(candidate);
+        String candidate;
+        if (pos == -1) {
+            candidate = uri;
+        } else {
+            candidate = uri.substring(0, pos);
         }
-        while (!canonicalContextPath.equals(candidate)) {
+        candidate = UDecoder.URLDecode(candidate, connector.getURIEncoding());
+        candidate = 
org.apache.tomcat.util.http.RequestUtil.normalize(candidate);
+        boolean match = canonicalContextPath.equals(candidate);
+        while (!match && pos != -1) {
             pos = nextSlash(uriChars, pos + 1);
             if (pos == -1) {
-                // Should never happen
-                throw new IllegalStateException(sm.getString(
-                        "coyoteRequest.getContextPath.ise", 
canonicalContextPath, uri));
+                candidate = uri;
+            } else {
+                candidate = uri.substring(0, pos);
             }
-            candidate = uri.substring(0, pos);
             candidate = UDecoder.URLDecode(candidate, 
connector.getURIEncoding());
             candidate = 
org.apache.tomcat.util.http.RequestUtil.normalize(candidate);
+            match = canonicalContextPath.equals(candidate);
+        }
+        if (match) {
+            if (pos == -1) {
+                return uri;
+            } else {
+                return uri.substring(0, pos);
+            }
+        } else {
+            // Should never happen
+            throw new IllegalStateException(sm.getString(
+                    "coyoteRequest.getContextPath.ise", canonicalContextPath, 
uri));
         }
-        return uri.substring(0, pos);
     }
 
 



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

Reply via email to