From 47b7b6db70a8ba1381555e46e9b05311fb321bae Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Thu, 12 Jan 2012 10:33:25 +1100 Subject: [PATCH] 368113 Support servlet mapping to "" --- .../java/org/eclipse/jetty/http/PathMap.java | 26 ++++++++++++++++--- .../org/eclipse/jetty/http/PathMapTest.java | 5 +++- .../eclipse/jetty/servlet/ServletHandler.java | 6 ++--- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/PathMap.java b/jetty-http/src/main/java/org/eclipse/jetty/http/PathMap.java index 19da9e00104..12abbfee5b5 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/PathMap.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/PathMap.java @@ -146,7 +146,16 @@ public class PathMap extends HashMap implements Externalizable @Override public Object put(Object pathSpec, Object object) { - StringTokenizer tok = new StringTokenizer(pathSpec.toString(),__pathSpecSeparators); + String str = pathSpec.toString(); + if ("".equals(str.trim())) + { + Entry entry = new Entry("",object); + entry.setMapped(""); + _exactMap.put("", entry); + return super.put("", object); + } + + StringTokenizer tok = new StringTokenizer(str,__pathSpecSeparators); Object old =null; while (tok.hasMoreTokens()) @@ -218,13 +227,21 @@ public class PathMap extends HashMap implements Externalizable */ public Entry getMatch(String path) { - Map.Entry entry; + Map.Entry entry=null; if (path==null) return null; int l=path.length(); - + + //special case + if (l == 1 && path.charAt(0)=='/') + { + entry = (Map.Entry)_exactMap.get(""); + if (entry != null) + return (Entry)entry; + } + // try exact match entry=_exactMap.getEntry(path,0,l); if (entry!=null) @@ -455,6 +472,9 @@ public class PathMap extends HashMap implements Externalizable */ public static String pathInfo(String pathSpec, String path) { + if ("".equals(pathSpec)) + return path; //servlet 3 spec sec 12.2 will be '/' + char c = pathSpec.charAt(0); if (c=='/') diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/PathMapTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/PathMapTest.java index 630b35a8b9e..eb3e830f816 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/PathMapTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/PathMapTest.java @@ -36,6 +36,7 @@ public class PathMapTest extends TestCase p.put("*.gz", "7"); p.put("/", "8"); p.put("/XXX:/YYY", "9"); + p.put("", "10"); String[][] tests = { { "/abs/path", "1"}, @@ -68,7 +69,7 @@ public class PathMapTest extends TestCase assertEquals("Dir matches", "[/animal/fish/*=4, /animal/*=5, /=8]", p.getMatches("/animal/fish/").toString()); assertEquals("Dir matches", "[/animal/fish/*=4, /animal/*=5, /=8]", p.getMatches("/animal/fish").toString()); assertEquals("Dir matches", "[/=8]", p.getMatches("/").toString()); - assertEquals("Dir matches", "[/=8]", p.getMatches("").toString()); + assertEquals("Dir matches", "[=10, /=8]", p.getMatches("").toString()); assertEquals("pathMatch exact", "/Foo/bar", PathMap.pathMatch("/Foo/bar", "/Foo/bar")); assertEquals("pathMatch prefix", "/Foo", PathMap.pathMatch("/Foo/*", "/Foo/bar")); @@ -125,6 +126,8 @@ public class PathMapTest extends TestCase assertTrue("!match /foo/*", !PathMap.match("/foo/*", "/bar/anything")); assertTrue("match *.foo", PathMap.match("*.foo", "anything.foo")); assertTrue("!match *.foo", !PathMap.match("*.foo", "anything.bar")); + + assertEquals("match / with ''", "10", p.getMatch("/").getValue()); } /** 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 500e9074998..39acebbab12 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 @@ -374,8 +374,8 @@ public class ServletHandler extends ScopedHandler String servlet_path_spec=(String)entry.getKey(); String servlet_path=entry.getMapped()!=null?entry.getMapped():PathMap.pathMatch(servlet_path_spec,target); - String path_info=PathMap.pathInfo(servlet_path_spec,target); - + String path_info=PathMap.pathInfo(servlet_path_spec,target); + if (DispatcherType.INCLUDE.equals(type)) { baseRequest.setAttribute(Dispatcher.INCLUDE_SERVLET_PATH,servlet_path); @@ -393,7 +393,7 @@ public class ServletHandler extends ScopedHandler // look for a servlet by name! servlet_holder=(ServletHolder)_servletNameMap.get(target); } - + if (LOG.isDebugEnabled()) LOG.debug("servlet {}|{}|{} -> {}",baseRequest.getContextPath(),baseRequest.getServletPath(),baseRequest.getPathInfo(),servlet_holder);