Fix AntPathMatcher multiple segments matching

Prior to this commit, the new match algorithm wouldn't work for multiple
consecutive path separators.
This commit separately matches path segments and path separators and
allows for multiple, consecutive path separators.

Issue: SPR-14141
This commit is contained in:
Brian Clozel
2016-04-13 18:20:15 +02:00
parent e38bfb1a68
commit 368f29d5bc
2 changed files with 23 additions and 14 deletions

View File

@@ -317,34 +317,42 @@ public class AntPathMatcher implements PathMatcher {
char[] pathChars = path.toCharArray(); char[] pathChars = path.toCharArray();
int pos = 0; int pos = 0;
for (String pattDir : pattDirs) { for (String pattDir : pattDirs) {
int count = countStartsWith(pathChars, pos, this.pathSeparator, false); int skipped = skipSeparator(path, pos, this.pathSeparator);
pos += (count == this.pathSeparator.length() ? count : 0); pos += skipped;
count = countStartsWith(pathChars, pos, pattDir, true); skipped = skipSegment(pathChars, pos, pattDir);
if (count < pattDir.length()) { if (skipped < pattDir.length()) {
if (count > 0) { if (skipped > 0) {
return true; return true;
} }
return (pattDir.length() > 0) && isWildcardChar(pattDir.charAt(0)); return (pattDir.length() > 0) && isWildcardChar(pattDir.charAt(0));
} }
pos += count; pos += skipped;
} }
return true; return true;
} }
private int countStartsWith(char[] chars, int pos, String prefix, boolean stopOnWildcard) { private int skipSegment(char[] chars, int pos, String prefix) {
int count = 0; int skipped = 0;
for (char c : prefix.toCharArray()) { for (char c : prefix.toCharArray()) {
if (stopOnWildcard && isWildcardChar(c)) { if (isWildcardChar(c)) {
return count; return skipped;
} }
if (pos + count >= chars.length) { else if (pos + skipped >= chars.length) {
return 0; return 0;
} }
if (chars[pos + count] == c) { else if (chars[pos + skipped] == c) {
count++; skipped++;
} }
} }
return count; return skipped;
}
private int skipSeparator(String path, int pos, String separator) {
int skipped = 0;
while (path.startsWith(separator, pos + skipped)) {
skipped += separator.length();
}
return skipped;
} }
private boolean isWildcardChar(char c) { private boolean isWildcardChar(char c) {

View File

@@ -53,6 +53,7 @@ public class AntPathMatcherTests {
// test exact matching // test exact matching
assertTrue(pathMatcher.match("test", "test")); assertTrue(pathMatcher.match("test", "test"));
assertTrue(pathMatcher.match("/test", "/test")); assertTrue(pathMatcher.match("/test", "/test"));
assertTrue(pathMatcher.match("http://example.org", "http://example.org")); // SPR-14141
assertFalse(pathMatcher.match("/test.jpg", "test.jpg")); assertFalse(pathMatcher.match("/test.jpg", "test.jpg"));
assertFalse(pathMatcher.match("test", "/test")); assertFalse(pathMatcher.match("test", "/test"));
assertFalse(pathMatcher.match("/test", "test")); assertFalse(pathMatcher.match("/test", "test"));