Skip to content

Commit

Permalink
Fixing #9517 - bad url-pattern prefix match behavior
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed Mar 17, 2023
1 parent f2b0f21 commit 3904e4a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,19 @@ public MatchedResource<E> getMatched(String path)
return exact.getPreMatched();

// Try a prefix match
MappedResource<E> prefix = _prefixMap.getBest(path);
if (prefix != null)
if (!_prefixMap.isEmpty())
{
MatchedPath matchedPath = prefix.getPathSpec().matched(path);
if (matchedPath != null)
return new MatchedResource<>(prefix.getResource(), prefix.getPathSpec(), matchedPath);
int i = path.length();
while (i >= 0)
{
MappedResource<E> candidate = _prefixMap.getBest(path, 0, i--);
if (candidate == null)
continue;

MatchedPath matchedPath = candidate.getPathSpec().matched(path);
if (matchedPath != null)
return new MatchedResource<>(candidate.getResource(), candidate.getPathSpec(), matchedPath);
}
}

// Try a suffix match
Expand All @@ -223,13 +230,13 @@ public MatchedResource<E> getMatched(String path)
// Loop 3: "foo"
while ((i = path.indexOf('.', i + 1)) > 0)
{
prefix = _suffixMap.get(path, i + 1, path.length() - i - 1);
if (prefix == null)
MappedResource<E> suffix = _suffixMap.get(path, i + 1, path.length() - i - 1);
if (suffix == null)
continue;

MatchedPath matchedPath = prefix.getPathSpec().matched(path);
MatchedPath matchedPath = suffix.getPathSpec().matched(path);
if (matchedPath != null)
return new MatchedResource<>(prefix.getResource(), prefix.getPathSpec(), matchedPath);
return new MatchedResource<>(suffix.getResource(), suffix.getPathSpec(), matchedPath);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ public void testServletMatchDefault()
assertMatch(p, "/", "any");
}

/**
* Test the match order rules imposed by the Servlet API (any vs specific sub-dir)
*/
@Test
public void testServletMatchPrefix()
{
PathMappings<String> p = new PathMappings<>();

p.put(new ServletPathSpec("/*"), "any");
p.put(new ServletPathSpec("/foo/*"), "foo");

// assertMatch(p, "/abs/path", "any");
// assertMatch(p, "/abs/foo/bar", "any");
// assertMatch(p, "/foo/bar", "foo");
// assertMatch(p, "/", "any");
assertMatch(p, "/foobar", "any");
}

/**
* Test the match order rules with a mixed Servlet and URI Template path specs
*
Expand Down

0 comments on commit 3904e4a

Please sign in to comment.