Skip to content

Commit 368f29d

Browse files
committed
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
1 parent e38bfb1 commit 368f29d

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

spring-core/src/main/java/org/springframework/util/AntPathMatcher.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -317,34 +317,42 @@ private boolean isPotentialMatch(String path, String[] pattDirs) {
317317
char[] pathChars = path.toCharArray();
318318
int pos = 0;
319319
for (String pattDir : pattDirs) {
320-
int count = countStartsWith(pathChars, pos, this.pathSeparator, false);
321-
pos += (count == this.pathSeparator.length() ? count : 0);
322-
count = countStartsWith(pathChars, pos, pattDir, true);
323-
if (count < pattDir.length()) {
324-
if (count > 0) {
320+
int skipped = skipSeparator(path, pos, this.pathSeparator);
321+
pos += skipped;
322+
skipped = skipSegment(pathChars, pos, pattDir);
323+
if (skipped < pattDir.length()) {
324+
if (skipped > 0) {
325325
return true;
326326
}
327327
return (pattDir.length() > 0) && isWildcardChar(pattDir.charAt(0));
328328
}
329-
pos += count;
329+
pos += skipped;
330330
}
331331
return true;
332332
}
333333

334-
private int countStartsWith(char[] chars, int pos, String prefix, boolean stopOnWildcard) {
335-
int count = 0;
334+
private int skipSegment(char[] chars, int pos, String prefix) {
335+
int skipped = 0;
336336
for (char c : prefix.toCharArray()) {
337-
if (stopOnWildcard && isWildcardChar(c)) {
338-
return count;
337+
if (isWildcardChar(c)) {
338+
return skipped;
339339
}
340-
if (pos + count >= chars.length) {
340+
else if (pos + skipped >= chars.length) {
341341
return 0;
342342
}
343-
if (chars[pos + count] == c) {
344-
count++;
343+
else if (chars[pos + skipped] == c) {
344+
skipped++;
345345
}
346346
}
347-
return count;
347+
return skipped;
348+
}
349+
350+
private int skipSeparator(String path, int pos, String separator) {
351+
int skipped = 0;
352+
while (path.startsWith(separator, pos + skipped)) {
353+
skipped += separator.length();
354+
}
355+
return skipped;
348356
}
349357

350358
private boolean isWildcardChar(char c) {

spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public void match() {
5353
// test exact matching
5454
assertTrue(pathMatcher.match("test", "test"));
5555
assertTrue(pathMatcher.match("/test", "/test"));
56+
assertTrue(pathMatcher.match("http://example.org", "http://example.org")); // SPR-14141
5657
assertFalse(pathMatcher.match("/test.jpg", "test.jpg"));
5758
assertFalse(pathMatcher.match("test", "/test"));
5859
assertFalse(pathMatcher.match("/test", "test"));

0 commit comments

Comments
 (0)