From 82c6b0148066a7ca58536225a35a16f98ade79a8 Mon Sep 17 00:00:00 2001 From: maxcooper Date: Sun, 5 Jan 2003 09:23:53 +0000 Subject: [PATCH] added pattern type for / (default) --- .../org/securityfilter/filter/URLPattern.java | 108 ++++++++++++--------- 1 file changed, 60 insertions(+), 48 deletions(-) diff --git a/src/share/org/securityfilter/filter/URLPattern.java b/src/share/org/securityfilter/filter/URLPattern.java index 83f325f..0ae6190 100644 --- a/src/share/org/securityfilter/filter/URLPattern.java +++ b/src/share/org/securityfilter/filter/URLPattern.java @@ -1,7 +1,7 @@ /* - * $Header: /cvsroot/securityfilter/securityfilter/src/share/org/securityfilter/filter/URLPattern.java,v 1.2 2003/01/02 19:21:28 maxcooper Exp $ - * $Revision: 1.2 $ - * $Date: 2003/01/02 19:21:28 $ + * $Header: /cvsroot/securityfilter/securityfilter/src/share/org/securityfilter/filter/URLPattern.java,v 1.3 2003/01/05 09:23:53 maxcooper Exp $ + * $Revision: 1.3 $ + * $Date: 2003/01/05 09:23:53 $ * * ==================================================================== * The SecurityFilter Software License, Version 1.1 @@ -67,12 +67,12 @@ import org.securityfilter.config.WebResourceCollection; * Also supports sorting according to the Servlet Spec v2.3. * * @author Max Cooper (max@maxcooper.com) - * @version $Revision: 1.2 $ $Date: 2003/01/02 19:21:28 $ + * @version $Revision: 1.3 $ $Date: 2003/01/05 09:23:53 $ */ public class URLPattern implements Comparable { /** - * Pattern type for patterns that do not meet the PATH_TYPE - * or EXTENSION_TYPE pattern type specifications. + * Pattern type for patterns that do not meet the specifications for the + * other pattern types. */ public static final int EXACT_TYPE = 1; /** @@ -83,7 +83,10 @@ public class URLPattern implements Comparable { * Pattern type for EXTENSION_TYPE mappings. Starts with '*.' */ public static final int EXTENSION_TYPE = 3; - + /** + * Pattern type for EXTENSION_TYPE mappings. Starts with '*.' + */ + public static final int DEFAULT_TYPE = 4; protected String pattern; protected String convertedPattern; @@ -187,12 +190,14 @@ public class URLPattern implements Comparable { * Initialize the patternType protected member. */ protected void initPatternType() { - if (pattern.startsWith("*.")) { - patternType = URLPattern.EXTENSION_TYPE; + if ("/".equals(pattern)) { + patternType = DEFAULT_TYPE; + } else if (pattern.startsWith("*.")) { + patternType = EXTENSION_TYPE; } else if (pattern.startsWith("/") && pattern.endsWith("/*")) { - patternType = URLPattern.PATH_TYPE; + patternType = PATH_TYPE; } else { - patternType = URLPattern.EXACT_TYPE; + patternType = EXACT_TYPE; } } @@ -212,44 +217,51 @@ public class URLPattern implements Comparable { * Initialize the convertedPattern protected member. */ protected void initConvertedPattern() { - StringBuffer buf = new StringBuffer(pattern); - int pos; - // escape '.' characters - pos = buf.toString().indexOf('.'); - while (pos != -1) { - buf.insert(pos, "\\"); - pos = buf.toString().indexOf('.', pos + 2); - } - // replace '*' chars in the compiledPattern with '.*' - pos = buf.toString().indexOf('*'); - while (pos != -1) { - buf.replace(pos, pos + 1, ".*"); - pos = buf.toString().indexOf('*', pos + 2); - } - // replace '/' chars with '/+' to match one or more consecutive slashes - pos = buf.toString().indexOf('/'); - while (pos != -1) { - buf.replace(pos, pos + 1, "/+"); - pos = buf.toString().indexOf('/', pos + 2); - } - // adjustments for the different expression types - switch (patternType) { - case PATH_TYPE: - // make sure it matches from the start of the string - buf.insert(0, '^'); - // make sure /foo/* matches /foo and /foo/morestuff, but not /foobar - buf.insert(buf.length()-4, "("); - buf.append(")?$"); - break; - case EXTENSION_TYPE: - buf.append('$'); - break; - case EXACT_TYPE: - buf.insert(0, '^'); - buf.append('$'); - break; + if (patternType == DEFAULT_TYPE) { + // match anything for default pattern + convertedPattern = ".*"; + } else { + StringBuffer buf = new StringBuffer(pattern); + int pos; + // escape '.' characters + pos = buf.toString().indexOf('.'); + while (pos != -1) { + buf.insert(pos, "\\"); + pos = buf.toString().indexOf('.', pos + 2); + } + // replace '*' chars in the compiledPattern with '.*' + pos = buf.toString().indexOf('*'); + while (pos != -1) { + buf.replace(pos, pos + 1, ".*"); + pos = buf.toString().indexOf('*', pos + 2); + } + // replace '/' chars with '/+' to match one or more consecutive slashes + // the spec hints that containers are supposed to normalize the extra slashes out, + // but testing revealed that sometimes the extra slashes are not normalized out + pos = buf.toString().indexOf('/'); + while (pos != -1) { + buf.replace(pos, pos + 1, "/+"); + pos = buf.toString().indexOf('/', pos + 2); + } + // adjustments for the different expression types + switch (patternType) { + case PATH_TYPE: + // make sure it matches from the start of the string + buf.insert(0, '^'); + // make sure /foo/* matches /foo and /foo/morestuff, but not /foobar + buf.insert(buf.length()-4, "("); + buf.append(")?$"); + break; + case EXTENSION_TYPE: + buf.append('$'); + break; + case EXACT_TYPE: + buf.insert(0, '^'); + buf.append('$'); + break; + } + convertedPattern = buf.toString(); } - convertedPattern = buf.toString(); } /** -- 2.11.0