Fixes #4542 Root pathspec mapping pathInfo (#4705)

* Fixes #4542 Root pathspec mapping pathInfo

For the "" root pathspec, the pathinfo should always be the full path and the matched path is ""

Signed-off-by: Greg Wilkins <gregw@webtide.com>

* updates from review

Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
Greg Wilkins 2020-03-30 16:38:42 +02:00 committed by GitHub
parent fb909057a4
commit e913e7970f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 31 deletions

View File

@ -39,7 +39,7 @@ public enum PathSpecGroup
*
* <pre>
* "" - servlet spec (Root Servlet)
* null - servlet spec (Root Servlet)
* null - legacy (Root Servlet)
* </pre>
*
* Note: there is no known uri-template spec variant of this kind of path spec

View File

@ -167,17 +167,19 @@ public class ServletPathSpec extends PathSpec
@Override
public String getPathInfo(String path)
{
// Path Info only valid for PREFIX_GLOB types
if (group == PathSpecGroup.PREFIX_GLOB)
switch (group)
{
if (path.length() == (specLength - 2))
{
return null;
}
return path.substring(specLength - 2);
}
case ROOT:
return path;
return null;
case PREFIX_GLOB:
if (path.length() == (specLength - 2))
return null;
return path.substring(specLength - 2);
default:
return null;
}
}
@Override
@ -185,35 +187,27 @@ public class ServletPathSpec extends PathSpec
{
switch (group)
{
case ROOT:
return "";
case EXACT:
if (pathSpec.equals(path))
{
return path;
}
else
{
return null;
}
return null;
case PREFIX_GLOB:
if (isWildcardMatch(path))
{
return path.substring(0, specLength - 2);
}
else
{
return null;
}
return null;
case SUFFIX_GLOB:
if (path.regionMatches(path.length() - (specLength - 1), pathSpec, 1, specLength - 1))
{
return path;
}
else
{
return null;
}
return null;
case DEFAULT:
return path;
default:
return null;
}

View File

@ -62,6 +62,7 @@ public class PathMappingsTest
{
PathMappings<String> p = new PathMappings<>();
p.put(new ServletPathSpec(""), "root");
p.put(new ServletPathSpec("/"), "default");
p.put(new ServletPathSpec("/animal/bird/*"), "birds");
p.put(new ServletPathSpec("/animal/fish/*"), "fishes");
@ -75,7 +76,8 @@ public class PathMappingsTest
assertMatch(p, "/animal/bird/eagle", "birds");
assertMatch(p, "/animal/fish/bass/sea", "fishes");
assertMatch(p, "/animal/peccary/javalina/evolution", "animals");
assertMatch(p, "/", "default");
assertMatch(p, "/", "root");
assertMatch(p, "/other", "default");
assertMatch(p, "/animal/bird/eagle/chat", "animalChat");
assertMatch(p, "/animal/bird/penguin/chat", "animalChat");
assertMatch(p, "/animal/fish/trout/cam", "animalCam");

View File

@ -117,7 +117,8 @@ public class ServletPathSpecTest
assertEquals(null, new ServletPathSpec("/Foo/*").getPathInfo("/Foo"), "pathInfo prefix");
assertEquals(null, new ServletPathSpec("*.ext").getPathInfo("/Foo/bar.ext"), "pathInfo suffix");
assertEquals(null, new ServletPathSpec("/").getPathInfo("/Foo/bar.ext"), "pathInfo default");
assertEquals("/", new ServletPathSpec("").getPathInfo("/"), "pathInfo root");
assertEquals("", new ServletPathSpec("").getPathInfo(""), "pathInfo root");
assertEquals("/xxx/zzz", new ServletPathSpec("/*").getPathInfo("/xxx/zzz"), "pathInfo default");
}
@ -146,7 +147,8 @@ public class ServletPathSpecTest
assertEquals("/Foo", new ServletPathSpec("/Foo/*").getPathMatch("/Foo"), "pathMatch prefix");
assertEquals("/Foo/bar.ext", new ServletPathSpec("*.ext").getPathMatch("/Foo/bar.ext"), "pathMatch suffix");
assertEquals("/Foo/bar.ext", new ServletPathSpec("/").getPathMatch("/Foo/bar.ext"), "pathMatch default");
assertEquals("", new ServletPathSpec("").getPathMatch("/"), "pathInfo root");
assertEquals("", new ServletPathSpec("").getPathMatch(""), "pathInfo root");
assertEquals("", new ServletPathSpec("/*").getPathMatch("/xxx/zzz"), "pathMatch default");
}