Skip to content

Commit

Permalink
Issue #8184 - Correcting match logic for multiple servlet suffix url-…
Browse files Browse the repository at this point in the history
…pattern (#8186)

Cherry-pick of commit d1ecdf6

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed Jun 21, 2022
1 parent 3886ac5 commit fa40ad7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
Expand Up @@ -171,20 +171,17 @@ public MatchedResource<E> getMatched(String path)
if (_optimizedExact)
{
int i = path.length();

final Trie<MappedResource<E>> exact_map = _exactMap;
while (i >= 0)
{
MappedResource<E> candidate = exact_map.getBest(path, 0, i);
MappedResource<E> candidate = _exactMap.getBest(path, 0, i--);
if (candidate == null)
break;
continue;

matchedPath = candidate.getPathSpec().matched(path);
if (matchedPath != null)
{
return new MatchedResource<>(candidate.getResource(), candidate.getPathSpec(), matchedPath);
}
i--;
}
// If we reached here, there's NO optimized EXACT Match possible, skip simple match below
skipRestOfGroup = true;
Expand All @@ -197,17 +194,15 @@ public MatchedResource<E> getMatched(String path)
if (_optimizedPrefix)
{
int i = path.length();
final Trie<MappedResource<E>> prefix_map = _prefixMap;
while (i >= 0)
{
MappedResource<E> candidate = prefix_map.getBest(path, 0, i);
MappedResource<E> candidate = _prefixMap.getBest(path, 0, i--);
if (candidate == null)
break;
continue;

matchedPath = candidate.getPathSpec().matched(path);
if (matchedPath != null)
return new MatchedResource<>(candidate.getResource(), candidate.getPathSpec(), matchedPath);
i--;
}
// If we reached here, there's NO optimized PREFIX Match possible, skip simple match below
skipRestOfGroup = true;
Expand All @@ -220,12 +215,16 @@ public MatchedResource<E> getMatched(String path)
if (_optimizedSuffix)
{
int i = 0;
final Trie<MappedResource<E>> suffix_map = _suffixMap;
// Loop through each suffix mark
// Input is "/a.b.c.foo"
// Loop 1: "b.c.foo"
// Loop 2: "c.foo"
// Loop 3: "foo"
while ((i = path.indexOf('.', i + 1)) > 0)
{
MappedResource<E> candidate = suffix_map.get(path, i + 1, path.length() - i - 1);
MappedResource<E> candidate = _suffixMap.get(path, i + 1, path.length() - i - 1);
if (candidate == null)
break;
continue;

matchedPath = candidate.getPathSpec().matched(path);
if (matchedPath != null)
Expand Down
Expand Up @@ -324,6 +324,32 @@ public void testGetServletPathSpec()
assertThat(p.get(new RegexPathSpec("/a/b/c")), nullValue());
}

@Test
public void testServletMultipleSuffixMappings()
{
PathMappings<String> p = new PathMappings<>();
p.put(new ServletPathSpec("*.foo"), "resourceFoo");
p.put(new ServletPathSpec("*.bar"), "resourceBar");
p.put(new ServletPathSpec("*.zed"), "resourceZed");

MatchedResource<String> matched;

matched = p.getMatched("/a.b.c.foo");
assertThat(matched.getResource(), is("resourceFoo"));

matched = p.getMatched("/a.b.c.bar");
assertThat(matched.getResource(), is("resourceBar"));

matched = p.getMatched("/a.b.c.pop");
assertNull(matched);

matched = p.getMatched("/a.foo.c.pop");
assertNull(matched);

matched = p.getMatched("/a%2Efoo");
assertNull(matched);
}

@Test
public void testRemoveUriTemplatePathSpec()
{
Expand Down

0 comments on commit fa40ad7

Please sign in to comment.