Merge branch 'master' of ssh://git.eclipse.org/gitroot/jetty/org.eclipse.jetty.project

This commit is contained in:
Jan Bartel 2012-06-06 15:02:01 +02:00
commit 468d7da719
5 changed files with 66 additions and 58 deletions

View File

@ -1782,8 +1782,8 @@ public class ContextHandler extends ScopedHandler implements Attributes, Server.
query = uriInContext.substring(q + 1); query = uriInContext.substring(q + 1);
uriInContext = uriInContext.substring(0,q); uriInContext = uriInContext.substring(0,q);
} }
if ((q = uriInContext.indexOf(';')) > 0) // if ((q = uriInContext.indexOf(';')) > 0)
uriInContext = uriInContext.substring(0,q); // uriInContext = uriInContext.substring(0,q);
String pathInContext = URIUtil.canonicalPath(URIUtil.decodePath(uriInContext)); String pathInContext = URIUtil.canonicalPath(URIUtil.decodePath(uriInContext));
String uri = URIUtil.addPaths(getContextPath(),uriInContext); String uri = URIUtil.addPaths(getContextPath(),uriInContext);

View File

@ -253,43 +253,6 @@ public class ServletHandler extends ScopedHandler
return _servletPathMap.getMatch(pathInContext); return _servletPathMap.getMatch(pathInContext);
} }
/* ------------------------------------------------------------ */
/**
* @param uriInContext uri to get dispatcher for
* @return A {@link RequestDispatcher dispatcher} wrapping the resource at <code>uriInContext</code>,
* or <code>null</code> 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() public ServletContext getServletContext()
{ {

View File

@ -106,6 +106,27 @@ public class DispatcherTest
assertEquals(expected, responses); 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 @Test
public void testInclude() throws Exception public void testInclude() throws Exception
{ {
@ -279,6 +300,10 @@ public class DispatcherTest
dispatcher = getServletContext().getRequestDispatcher("/AssertIncludeForwardServlet/assertpath?do=end"); dispatcher = getServletContext().getRequestDispatcher("/AssertIncludeForwardServlet/assertpath?do=end");
else if(request.getParameter("do").equals("assertforward")) else if(request.getParameter("do").equals("assertforward"))
dispatcher = getServletContext().getRequestDispatcher("/AssertForwardServlet?do=end&do=the"); 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); 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 public static class AssertForwardServlet extends HttpServlet implements Servlet
{ {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

View File

@ -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 path The path the encode
* @param buf StringBuilder to encode path into * @param buf StringBuilder to encode path into
*/ */
@ -260,8 +260,10 @@ public class URIUtil
{ {
if (path==null) if (path==null)
return null; return null;
// Array to hold all converted characters
char[] chars=null; char[] chars=null;
int n=0; int n=0;
// Array to hold a sequence of %encodings
byte[] bytes=null; byte[] bytes=null;
int b=0; int b=0;
@ -283,14 +285,26 @@ public class URIUtil
i+=2; i+=2;
continue; continue;
} }
else if (c==';')
{
if (chars==null)
{
chars=new char[len];
path.getChars(0,i,chars,0);
n=i;
}
break;
}
else if (bytes==null) else if (bytes==null)
{ {
n++; n++;
continue; continue;
} }
// Do we have some bytes to convert?
if (b>0) if (b>0)
{ {
// convert series of bytes and add to chars
String s; String s;
try try
{ {
@ -311,8 +325,10 @@ public class URIUtil
if (chars==null) if (chars==null)
return path; return path;
// if we have a remaining sequence of bytes
if (b>0) if (b>0)
{ {
// convert series of bytes and add to chars
String s; String s;
try 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 path The path the encode
* @param buf StringBuilder to encode path into * @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)); b=(byte)(0xff&TypeUtil.parseInt(buf,i+offset+1,2,16));
i+=2; i+=2;
} }
else if (b==';')
{
length=i;
break;
}
else if (bytes==null) else if (bytes==null)
{ {
n++; n++;
@ -438,20 +459,6 @@ public class URIUtil
return null; 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. /** Convert a path to a cananonical form.
* All instances of "." and ".." are factored out. Null is returned * All instances of "." and ".." are factored out. Null is returned

View File

@ -51,8 +51,8 @@ public class URITest
@Test @Test
public void testDecodePath() 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("foo%2523%3b%2c:%3db%20a%20r;rubbish"));
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("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ää%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")); assertEquals("f\u0629\u0629%23;,:=b a r",URIUtil.decodePath("f%d8%a9%d8%a9%2523%3b%2c:%3db%20a%20r"));
} }