From 27b6d431640680414aa52a9f640a9a24efccb10e Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Wed, 6 Jun 2012 11:19:15 +0200 Subject: [PATCH] 381825 leave URI params in forwarded requestURI --- .../jetty/server/handler/ContextHandler.java | 4 +- .../eclipse/jetty/servlet/ServletHandler.java | 39 +------------------ .../eclipse/jetty/servlet/DispatcherTest.java | 38 ++++++++++++++++++ .../java/org/eclipse/jetty/util/URIUtil.java | 39 +++++++++++-------- .../java/org/eclipse/jetty/util/URITest.java | 4 +- 5 files changed, 66 insertions(+), 58 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index 1baee0070b0..cf25d5d6dd9 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -1782,8 +1782,8 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server. query = uriInContext.substring(q + 1); uriInContext = uriInContext.substring(0,q); } - if ((q = uriInContext.indexOf(';')) > 0) - uriInContext = uriInContext.substring(0,q); + // if ((q = uriInContext.indexOf(';')) > 0) + // uriInContext = uriInContext.substring(0,q); String pathInContext = URIUtil.canonicalPath(URIUtil.decodePath(uriInContext)); String uri = URIUtil.addPaths(getContextPath(),uriInContext); diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java index 76f360592ad..80574bdc303 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/ServletHandler.java @@ -252,44 +252,7 @@ public class ServletHandler extends ScopedHandler return null; return _servletPathMap.getMatch(pathInContext); } - - /* ------------------------------------------------------------ */ - /** - * @param uriInContext uri to get dispatcher for - * @return A {@link RequestDispatcher dispatcher} wrapping the resource at uriInContext, - * or null if the specified uri cannot be dispatched to. - */ - public RequestDispatcher getRequestDispatcher(String uriInContext) - { - if (uriInContext == null || _contextHandler==null) - return null; - - if (!uriInContext.startsWith("/")) - return null; - - try - { - String query=null; - int q; - if ((q=uriInContext.indexOf('?'))>0) - { - query=uriInContext.substring(q+1); - uriInContext=uriInContext.substring(0,q); - } - if ((q=uriInContext.indexOf(';'))>0) - uriInContext=uriInContext.substring(0,q); - - String pathInContext=URIUtil.canonicalPath(URIUtil.decodePath(uriInContext)); - String uri=URIUtil.addPaths(_contextHandler.getContextPath(), uriInContext); - return new Dispatcher(_contextHandler, uri, pathInContext, query); - } - catch(Exception e) - { - LOG.ignore(e); - } - return null; - } - + /* ------------------------------------------------------------ */ public ServletContext getServletContext() { diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DispatcherTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DispatcherTest.java index 6aede63657e..2ec9d9c5bd9 100644 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DispatcherTest.java +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DispatcherTest.java @@ -105,6 +105,27 @@ public class DispatcherTest assertEquals(expected, responses); } + + @Test + public void testForwardWithParam() throws Exception + { + _contextHandler.addServlet(ForwardServlet.class, "/ForwardServlet/*"); + _contextHandler.addServlet(EchoURIServlet.class, "/EchoURI/*"); + + String expected= + "HTTP/1.1 200 OK\r\n"+ + "Content-Type: text/plain\r\n"+ + "Content-Length: 54\r\n"+ + "\r\n"+ + "/context\r\n"+ + "/EchoURI\r\n"+ + "/x x\r\n"+ + "/context/EchoURI/x%20x;a=1\r\n"; + + String responses = _connector.getResponses("GET /context/ForwardServlet;ignore=true?do=req.echo&uri=EchoURI%2Fx%2520x%3Ba=1%3Fb=2 HTTP/1.1\n" + "Host: localhost\n\n"); + + assertEquals(expected, responses); + } @Test public void testInclude() throws Exception @@ -279,6 +300,10 @@ public class DispatcherTest dispatcher = getServletContext().getRequestDispatcher("/AssertIncludeForwardServlet/assertpath?do=end"); else if(request.getParameter("do").equals("assertforward")) dispatcher = getServletContext().getRequestDispatcher("/AssertForwardServlet?do=end&do=the"); + else if(request.getParameter("do").equals("ctx.echo")) + dispatcher = getServletContext().getRequestDispatcher(request.getParameter("uri")); + else if(request.getParameter("do").equals("req.echo")) + dispatcher = request.getRequestDispatcher(request.getParameter("uri")); dispatcher.forward(request, response); } } @@ -462,6 +487,19 @@ public class DispatcherTest } } + public static class EchoURIServlet extends HttpServlet implements Servlet + { + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + response.setContentType("text/plain"); + response.setStatus(HttpServletResponse.SC_OK); + response.getOutputStream().println(request.getContextPath()); + response.getOutputStream().println(request.getServletPath()); + response.getOutputStream().println(request.getPathInfo()); + response.getOutputStream().println(request.getRequestURI()); + } + } + public static class AssertForwardServlet extends HttpServlet implements Servlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java index 6a4c066a3f5..d18d9a3f727 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java @@ -252,7 +252,7 @@ public class URIUtil } /* ------------------------------------------------------------ */ - /* Decode a URI path. + /* Decode a URI path and strip parameters * @param path The path the encode * @param buf StringBuilder to encode path into */ @@ -260,8 +260,10 @@ public class URIUtil { if (path==null) return null; + // Array to hold all converted characters char[] chars=null; int n=0; + // Array to hold a sequence of %encodings byte[] bytes=null; int b=0; @@ -283,14 +285,26 @@ public class URIUtil i+=2; continue; } + else if (c==';') + { + if (chars==null) + { + chars=new char[len]; + path.getChars(0,i,chars,0); + n=i; + } + break; + } else if (bytes==null) { n++; continue; } + // Do we have some bytes to convert? if (b>0) { + // convert series of bytes and add to chars String s; try { @@ -311,8 +325,10 @@ public class URIUtil if (chars==null) return path; + // if we have a remaining sequence of bytes if (b>0) { + // convert series of bytes and add to chars String s; try { @@ -330,7 +346,7 @@ public class URIUtil } /* ------------------------------------------------------------ */ - /* Decode a URI path. + /* Decode a URI path and strip parameters. * @param path The path the encode * @param buf StringBuilder to encode path into */ @@ -348,6 +364,11 @@ public class URIUtil b=(byte)(0xff&TypeUtil.parseInt(buf,i+offset+1,2,16)); i+=2; } + else if (b==';') + { + length=i; + break; + } else if (bytes==null) { n++; @@ -438,20 +459,6 @@ public class URIUtil return null; } - /* ------------------------------------------------------------ */ - /** Strip parameters from a path. - * Return path upto any semicolon parameters. - */ - public static String stripPath(String path) - { - if (path==null) - return null; - int semi=path.indexOf(';'); - if (semi<0) - return path; - return path.substring(0,semi); - } - /* ------------------------------------------------------------ */ /** Convert a path to a cananonical form. * All instances of "." and ".." are factored out. Null is returned diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/URITest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/URITest.java index 79f350ff1c1..b0e8fc8fa79 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/URITest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/URITest.java @@ -51,8 +51,8 @@ public class URITest @Test public void testDecodePath() { - assertEquals("foo%23;,:=b a r",URIUtil.decodePath("foo%2523%3b%2c:%3db%20a%20r")); - assertEquals("foo%23;,:=b a r=",URIUtil.decodePath("xxxfoo%2523%3b%2c:%3db%20a%20r%3Dxxx".getBytes(),3,30)); + assertEquals("foo%23;,:=b a r",URIUtil.decodePath("foo%2523%3b%2c:%3db%20a%20r;rubbish")); + assertEquals("foo%23;,:=b a r=",URIUtil.decodePath("xxxfoo%2523%3b%2c:%3db%20a%20r%3Dxxx;rubbish".getBytes(),3,30)); assertEquals("fää%23;,:=b a r=",URIUtil.decodePath("fää%2523%3b%2c:%3db%20a%20r%3D")); assertEquals("f\u0629\u0629%23;,:=b a r",URIUtil.decodePath("f%d8%a9%d8%a9%2523%3b%2c:%3db%20a%20r")); }