From 3c92ddc94b04f1dc057125d336e5220c92351442 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 4 May 2016 22:28:06 +0200 Subject: [PATCH] Fix path matching for paths containing spaces Prior to this commit, the latest optimizations introduced in SPR-13913 would prevent matching when patterns contained spaces. Indeed, the optimized path would not fully tokenize the paths nor trim the tokens, as the "longer" code path does. This commit disables this optimized path when the `trimTokens` option is set to `true`. Also, the `trimTokens` setting is now set to `false` by default. Issue: SPR-14247 --- .../springframework/util/AntPathMatcher.java | 26 ++++++++++--------- .../util/AntPathMatcherTests.java | 8 ++++++ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java index 633a932a27..2d050218ec 100644 --- a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java +++ b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java @@ -83,7 +83,7 @@ public class AntPathMatcher implements PathMatcher { private boolean caseSensitive = true; - private boolean trimTokens = true; + private boolean trimTokens = false; private volatile Boolean cachePatterns; @@ -314,19 +314,21 @@ public class AntPathMatcher implements PathMatcher { } private boolean isPotentialMatch(String path, String[] pattDirs) { - char[] pathChars = path.toCharArray(); - int pos = 0; - for (String pattDir : pattDirs) { - int skipped = skipSeparator(path, pos, this.pathSeparator); - pos += skipped; - skipped = skipSegment(pathChars, pos, pattDir); - if (skipped < pattDir.length()) { - if (skipped > 0) { - return true; + if (!this.trimTokens) { + char[] pathChars = path.toCharArray(); + int pos = 0; + for (String pattDir : pattDirs) { + int skipped = skipSeparator(path, pos, this.pathSeparator); + pos += skipped; + skipped = skipSegment(pathChars, pos, pattDir); + if (skipped < pattDir.length()) { + if (skipped > 0) { + return true; + } + return (pattDir.length() > 0) && isWildcardChar(pattDir.charAt(0)); } - return (pattDir.length() > 0) && isWildcardChar(pattDir.charAt(0)); + pos += skipped; } - pos += skipped; } return true; } diff --git a/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java b/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java index f75e637c50..d810c33014 100644 --- a/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java +++ b/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java @@ -136,6 +136,14 @@ public class AntPathMatcherTests { assertTrue(pathMatcher.match("/{bla}.*", "/testing.html")); } + // SPR-14247 + @Test + public void matchWithTrimTokensEnabled() throws Exception { + pathMatcher.setTrimTokens(true); + + assertTrue(pathMatcher.match("/foo/bar", "/foo /bar")); + } + @Test public void withMatchStart() { // test exact matching