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 bmarwell committed Feb 1, 2021
1 parent e5d8134 commit 0842c27
Show file tree
Hide file tree
Showing 7 changed files with 575 additions and 56 deletions.
8 changes: 4 additions & 4 deletions 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, IpAddressMatcher, 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).
Certain parts (StringUtils, IpAddressMatcher, 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 @@ -79,8 +79,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 @@ -108,12 +115,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 @@ -395,33 +402,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 0842c27

Please sign in to comment.