Author: markt Date: Tue Apr 26 13:20:32 2016 New Revision: 1741015 URL: http://svn.apache.org/viewvc?rev=1741015&view=rev Log: Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=59317 After a dispatch (async and non-async) ensure that getRequestURI() returns an encoded URI.
Modified: tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java tomcat/trunk/test/org/apache/catalina/core/TestAsyncContextImpl.java tomcat/trunk/webapps/docs/changelog.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=1741015&r1=1741014&r2=1741015&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java (original) +++ tomcat/trunk/java/org/apache/catalina/core/ApplicationContext.java Tue Apr 26 13:20:32 2016 @@ -65,6 +65,7 @@ import org.apache.catalina.Wrapper; import org.apache.catalina.connector.Connector; import org.apache.catalina.mapper.MappingData; import org.apache.catalina.util.ServerInfo; +import org.apache.catalina.util.URLEncoder; import org.apache.tomcat.util.ExceptionUtils; import org.apache.tomcat.util.buf.CharChunk; import org.apache.tomcat.util.buf.MessageBytes; @@ -465,11 +466,11 @@ public class ApplicationContext mappingData.recycle(); - // Construct a RequestDispatcher to process this request - return new ApplicationDispatcher - (wrapper, uriCC.toString(), wrapperPath, pathInfo, - queryString, mapping, null); + String encodedUri = URLEncoder.DEFAULT.encode(uriCC.toString()); + // Construct a RequestDispatcher to process this request + return new ApplicationDispatcher(wrapper, encodedUri, wrapperPath, pathInfo, + queryString, mapping, null); } 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=1741015&r1=1741014&r2=1741015&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/core/TestAsyncContextImpl.java (original) +++ tomcat/trunk/test/org/apache/catalina/core/TestAsyncContextImpl.java Tue Apr 26 13:20:32 2016 @@ -50,7 +50,6 @@ import static org.junit.Assert.assertNot import static org.junit.Assert.assertTrue; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; import org.apache.catalina.Context; @@ -62,6 +61,7 @@ import org.apache.catalina.startup.Tomca import org.apache.catalina.valves.TesterAccessLogValve; import org.apache.tomcat.unittest.TesterContext; import org.apache.tomcat.util.buf.ByteChunk; +import org.apache.tomcat.util.buf.UDecoder; import org.apache.tomcat.util.descriptor.web.ErrorPage; import org.easymock.EasyMock; @@ -2240,11 +2240,10 @@ public class TestAsyncContextImpl extend tomcat.start(); String uri = "/foo/%24/bar"; - String uriDecoded = "/foo/$/bar"; ByteChunk body = getUrl("http://localhost:"; + getPort()+ uri); - Assert.assertEquals(uriDecoded, body.toString()); + Assert.assertEquals(uri, body.toString()); } private static class AsyncRequestUriServlet extends HttpServlet { @@ -2482,17 +2481,32 @@ public class TestAsyncContextImpl extend ac.fireOnComplete(); } + /* * https://bz.apache.org/bugzilla/show_bug.cgi?id=59317 */ - @Ignore // Currently fails. Disabled while investigations continue. @Test - public void testAsyncDistachUrlWithSpaces() throws Exception { + public void testAsyncDispatchUrlWithSpaces() throws Exception { + doTestDispatchWithSpaces(true); + } + + + @Test + public void testForwardDispatchUrlWithSpaces() throws Exception { + doTestDispatchWithSpaces(false); + } + + + private void doTestDispatchWithSpaces(boolean async) throws Exception { Tomcat tomcat = getTomcatInstance(); Context context = tomcat.addContext("", null); - Servlet s = new AsyncDispatchUrlWithSpacesServlet(); - Wrapper w = Tomcat.addServlet(context, "space", s); - w.setAsyncSupported(true); + if (async) { + Servlet s = new AsyncDispatchUrlWithSpacesServlet(); + Wrapper w = Tomcat.addServlet(context, "space", s); + w.setAsyncSupported(true); + } else { + Tomcat.addServlet(context, "space", new ForwardDispatchUrlWithSpacesServlet()); + } context.addServletMapping("/space/*", "space"); tomcat.start(); @@ -2519,10 +2533,13 @@ public class TestAsyncContextImpl extend count++; req.setAttribute("count", Integer.valueOf(count)); + String encodedUri = req.getRequestURI(); + String decodedUri = UDecoder.URLDecode(encodedUri); + try { // Just here to trigger the error @SuppressWarnings("unused") - URI u = new URI(req.getRequestURI()); + URI u = new URI(encodedUri); } catch (URISyntaxException e) { throw new ServletException(e); } @@ -2532,7 +2549,45 @@ public class TestAsyncContextImpl extend resp.getWriter().print("OK"); } else { AsyncContext ac = req.startAsync(); - ac.dispatch("/sp%61ce/foo%20bar"); + ac.dispatch(decodedUri); + } + } + } + + + private static class ForwardDispatchUrlWithSpacesServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + Integer countObj = (Integer) req.getAttribute("count"); + int count = 0; + if (countObj != null) { + count = countObj.intValue(); + } + count++; + req.setAttribute("count", Integer.valueOf(count)); + + String encodedUri = req.getRequestURI(); + String decodedUri = UDecoder.URLDecode(encodedUri); + + try { + // Just here to trigger the error + @SuppressWarnings("unused") + URI u = new URI(req.getRequestURI()); + } catch (URISyntaxException e) { + throw new ServletException(e); + } + + if (count > 3) { + resp.setContentType("text/plain"); + resp.getWriter().print("OK"); + } else { + RequestDispatcher rd = req.getRequestDispatcher(decodedUri); + rd.forward(req, resp); } } } Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1741015&r1=1741014&r2=1741015&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Tue Apr 26 13:20:32 2016 @@ -160,6 +160,11 @@ consistent with respect to whether or not they end with <code>/</code>. (markt) </fix> + <fix> + <bug>59317</bug>: Ensure that + <code>HttpServletRequest.getRequestURI()</code> returns an encoded URI + rather than a decoded URI after a dispatch. (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