Skip to content

Commit

Permalink
Improving tests for path matching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemers authored and fpapon committed Jan 21, 2021
1 parent e35a669 commit ab1ea4a
Show file tree
Hide file tree
Showing 7 changed files with 574 additions and 54 deletions.
2 changes: 1 addition & 1 deletion NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on initial ideas from Dr. Heinz Kabutz's publicly posted version
available at http://www.javaspecialists.eu/archive/Issue015.html,
with continued modifications.

Certain parts (StringUtils etc.) of the source code for this
Certain parts (StringUtils, AntPathMatcherTests, etc.) of the source code for this
product was copied for simplicity and to reduce dependencies
from the source code developed by the Spring Framework Project
(http://www.springframework.org).
54 changes: 27 additions & 27 deletions core/src/main/java/org/apache/shiro/util/AntPathMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,15 @@ public void setPathSeparator(String pathSeparator) {
this.pathSeparator = (pathSeparator != null ? pathSeparator : DEFAULT_PATH_SEPARATOR);
}


/**
* Checks if {@code path} is a pattern (i.e. contains a '*', or '?'). For example the {@code /foo/**} would return {@code true}, while {@code /bar/} would return {@code false}.
* @param path the string to check
* @return this method returns {@code true} if {@code path} contains a '*' or '?', otherwise, {@code false}
*/
public boolean isPattern(String path) {
if (path == null) {
return false;
}
return (path.indexOf('*') != -1 || path.indexOf('?') != -1);
}

Expand Down Expand Up @@ -106,12 +113,12 @@ public boolean matchStart(String pattern, String path) {
* <code>false</code> if it didn't
*/
protected boolean doMatch(String pattern, String path, boolean fullMatch) {
if (path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) {
if (path == null || path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) {
return false;
}

String[] pattDirs = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator);
String[] pathDirs = StringUtils.tokenizeToStringArray(path, this.pathSeparator);
String[] pattDirs = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, false, true);
String[] pathDirs = StringUtils.tokenizeToStringArray(path, this.pathSeparator, false, true);

int pattIdxStart = 0;
int pattIdxEnd = pattDirs.length - 1;
Expand Down Expand Up @@ -393,33 +400,26 @@ private boolean matchStrings(String pattern, String str) {
* and '<code>path</code>', but does <strong>not</strong> enforce this.
*/
public String extractPathWithinPattern(String pattern, String path) {
String[] patternParts = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator);
String[] pathParts = StringUtils.tokenizeToStringArray(path, this.pathSeparator);

StringBuilder buffer = new StringBuilder();

// Add any path parts that have a wildcarded pattern part.
int puts = 0;
for (int i = 0; i < patternParts.length; i++) {
String patternPart = patternParts[i];
if ((patternPart.indexOf('*') > -1 || patternPart.indexOf('?') > -1) && pathParts.length >= i + 1) {
if (puts > 0 || (i == 0 && !pattern.startsWith(this.pathSeparator))) {
buffer.append(this.pathSeparator);
String[] patternParts = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, false, true);
String[] pathParts = StringUtils.tokenizeToStringArray(path, this.pathSeparator, false, true);
StringBuilder builder = new StringBuilder();
boolean pathStarted = false;

for (int segment = 0; segment < patternParts.length; segment++) {
String patternPart = patternParts[segment];
if (patternPart.indexOf('*') > -1 || patternPart.indexOf('?') > -1) {
for (; segment < pathParts.length; segment++) {
if (pathStarted || (segment == 0 && !pattern.startsWith(this.pathSeparator))) {
builder.append(this.pathSeparator);
}
builder.append(pathParts[segment]);
pathStarted = true;
}
buffer.append(pathParts[i]);
puts++;
}
}

// Append any trailing path parts.
for (int i = patternParts.length; i < pathParts.length; i++) {
if (puts > 0 || i > 0) {
buffer.append(this.pathSeparator);
}
buffer.append(pathParts[i]);
}

return buffer.toString();
return builder.toString();
}


}
Loading

0 comments on commit ab1ea4a

Please sign in to comment.