Author: violetagg
Date: Thu Jun 27 17:41:06 2013
New Revision: 1497474

URL: http://svn.apache.org/r1497474
Log:
When calculating the path in AsyncContext.dispatch(), methods 
ServletRequest.getRequestURI and ServletRequest.getContextPath cannot be used. 
That's because the first one returns a string that is not decoded and not 
normalized, but the second one returns decoded string. Instead of this methods 
ServletRequest.getServletPath and ServletRequest.getPathInfo will be used as 
requestURI = contextPath + servletPath + pathInfo.

Modified:
    tomcat/trunk/java/org/apache/catalina/core/AsyncContextImpl.java
    tomcat/trunk/test/org/apache/catalina/core/TestAsyncContextImpl.java

Modified: tomcat/trunk/java/org/apache/catalina/core/AsyncContextImpl.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/AsyncContextImpl.java?rev=1497474&r1=1497473&r2=1497474&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/AsyncContextImpl.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/AsyncContextImpl.java Thu Jun 27 
17:41:06 2013
@@ -169,17 +169,19 @@ public class AsyncContextImpl implements
     public void dispatch() {
         check();
         String path;
-        String cpath;
+        String pathInfo;
         ServletRequest servletRequest = getRequest();
         if (servletRequest instanceof HttpServletRequest) {
             HttpServletRequest sr = (HttpServletRequest) servletRequest;
-            path = sr.getRequestURI();
-            cpath = sr.getContextPath();
+            path = sr.getServletPath();
+            pathInfo = sr.getPathInfo();
         } else {
-            path = request.getRequestURI();
-            cpath = request.getContextPath();
+            path = request.getServletPath();
+            pathInfo = request.getPathInfo();
+        }
+        if (pathInfo != null) {
+            path += pathInfo;
         }
-        if (cpath.length()>1) path = path.substring(cpath.length());
         dispatch(path);
     }
 

Modified: tomcat/trunk/test/org/apache/catalina/core/TestAsyncContextImpl.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TestAsyncContextImpl.java?rev=1497474&r1=1497473&r2=1497474&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/core/TestAsyncContextImpl.java 
(original)
+++ tomcat/trunk/test/org/apache/catalina/core/TestAsyncContextImpl.java Thu 
Jun 27 17:41:06 2013
@@ -1879,42 +1879,18 @@ public class TestAsyncContextImpl extend
 
     @Test
     public void testDispatchWithCustomRequestResponse() throws Exception {
-        // Setup Tomcat instance
-        Tomcat tomcat = getTomcatInstance();
-
-        // Must have a real docBase - just use temp
-        File docBase = new File(System.getProperty("java.io.tmpdir"));
-
-        Context ctx = tomcat.addContext("", docBase.getAbsolutePath());
-
-        DispatchingGenericServlet dispatch = new DispatchingGenericServlet();
-        Wrapper wrapper = Tomcat.addServlet(ctx, "dispatch", dispatch);
-        wrapper.setAsyncSupported(true);
-        ctx.addServletMapping("/dispatch", "dispatch");
-
-        CustomGenericServlet customGeneric = new CustomGenericServlet();
-        Wrapper wrapper2 = Tomcat.addServlet(ctx, "customGeneric",
-                customGeneric);
-        wrapper2.setAsyncSupported(true);
-        ctx.addServletMapping("/target", "customGeneric");
-
-        tomcat.start();
-
-        ByteChunk res = getUrl("http://localhost:"; + getPort()
-                + "/dispatch?crr=y");
+        prepareApplicationWithGenericServlet("");
 
         StringBuilder expected = new StringBuilder();
         expected.append("OK");
         expected.append("CustomGenericServletGet-");
-        assertEquals(expected.toString(), res.toString());
-
-        res = getUrl("http://localhost:"; + getPort()
-                + "/dispatch?crr=y&empty=y");
+        requestApplicationWithGenericServlet("/dispatch?crr=y", expected);
 
         expected = new StringBuilder();
         expected.append("OK");
         expected.append("DispatchingGenericServletGet-");
-        assertEquals(expected.toString(), res.toString());
+        requestApplicationWithGenericServlet("/dispatch?crr=y&empty=y",
+                expected);
     }
 
     private static class CustomGenericServlet extends GenericServlet {
@@ -1931,4 +1907,75 @@ public class TestAsyncContextImpl extend
         }
 
     }
+
+    @Test
+    public void testEmptyDispatch() throws Exception {
+        prepareApplicationWithGenericServlet("/fo o");
+        StringBuilder expected = new StringBuilder();
+        expected.append("OK");
+        expected.append("DispatchingGenericServletGet-");
+        requestApplicationWithGenericServlet("/fo%20o/dispatch?empty=y",
+                expected);
+        requestApplicationWithGenericServlet("//fo%20o/dispatch?empty=y",
+                expected);
+        requestApplicationWithGenericServlet("/./fo%20o/dispatch?empty=y",
+                expected);
+        requestApplicationWithGenericServlet("/fo%20o//dispatch?empty=y",
+                expected);
+        requestApplicationWithGenericServlet("/fo%20o/./dispatch?empty=y",
+                expected);
+        requestApplicationWithGenericServlet("/fo%20o/c/../dispatch?empty=y",
+                expected);
+    }
+
+    @Test
+    public void testEmptyDispatchWithCustomRequestResponse() throws Exception {
+        prepareApplicationWithGenericServlet("/fo o");
+        StringBuilder expected = new StringBuilder();
+        expected.append("OK");
+        expected.append("DispatchingGenericServletGet-");
+        requestApplicationWithGenericServlet("/fo%20o/dispatch?crr=y&empty=y",
+                expected);
+        requestApplicationWithGenericServlet("//fo%20o/dispatch?crr=y&empty=y",
+                expected);
+        requestApplicationWithGenericServlet(
+                "/./fo%20o/dispatch?crr=y&empty=y", expected);
+        requestApplicationWithGenericServlet("/fo%20o//dispatch?crr=y&empty=y",
+                expected);
+        requestApplicationWithGenericServlet(
+                "/fo%20o/./dispatch?crr=y&empty=y", expected);
+        requestApplicationWithGenericServlet(
+                "/fo%20o/c/../dispatch?crr=y&empty=y", expected);
+    }
+
+    private void prepareApplicationWithGenericServlet(String contextPath)
+            throws Exception {
+        // Setup Tomcat instance
+        Tomcat tomcat = getTomcatInstance();
+
+        // Must have a real docBase - just use temp
+        File docBase = new File(System.getProperty("java.io.tmpdir"));
+
+        Context ctx = tomcat.addContext(contextPath, 
docBase.getAbsolutePath());
+
+        DispatchingGenericServlet dispatch = new DispatchingGenericServlet();
+        Wrapper wrapper = Tomcat.addServlet(ctx, "dispatch", dispatch);
+        wrapper.setAsyncSupported(true);
+        ctx.addServletMapping("/dispatch", "dispatch");
+
+        CustomGenericServlet customGeneric = new CustomGenericServlet();
+        Wrapper wrapper2 = Tomcat.addServlet(ctx, "customGeneric",
+                customGeneric);
+        wrapper2.setAsyncSupported(true);
+        ctx.addServletMapping("/target", "customGeneric");
+
+        tomcat.start();
+    }
+
+    private void requestApplicationWithGenericServlet(String path,
+            StringBuilder expectedContent) throws Exception {
+        ByteChunk res = getUrl("http://localhost:"; + getPort() + path);
+
+        assertEquals(expectedContent.toString(), res.toString());
+    }
 }



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

Reply via email to