package mircoders.abuse;
-import gnu.regexp.RE;
import mir.entity.Entity;
import mir.session.Request;
import java.util.Iterator;
import java.util.List;
+import org.apache.oro.text.regex.*;
+
/**
* A description of a regular expression filter.
public FilterInstance constructFilterInstance(final String anExpression) throws AbuseExc {
try {
- new RE(anExpression);
-
- return new FilterInstance() {
+ int flags = 0;
- public boolean test(Entity anEntity, Request aRequest) {
+ if (!caseSensitive) {
+ flags |= Perl5Compiler.CASE_INSENSITIVE_MASK;
+ }
- try {
- Iterator j;
- int flags = 0;
+ final Pattern pattern = (new Perl5Compiler()).compile(anExpression, flags);
- if (!caseSensitive) {
- flags |= RE.REG_ICASE;
- }
+ return new FilterInstance() {
- RE regularExpression = new RE(anExpression, flags);
+ public boolean test(Entity anEntity, Request aRequest) {
+ PatternMatcher matcher = new Perl5Matcher();
- switch (fieldKind) {
- case REQUEST_HEADERS:
- if (selectedFields != null) {
- j = selectedFields.iterator();
+ Iterator j;
- while (j.hasNext()) {
- String fieldName = (String) j.next();
- String field = aRequest.getHeader(fieldName);
-
- if (exactMatch) {
- if (field != null && regularExpression.isMatch(field)) {
- return true;
- }
- }
- else {
- if (field != null && regularExpression.getMatch(field) != null) {
- return true;
- }
- }
- }
- }
- break;
- case ENTITY_FIELDS:
- if (selectedFields != null) {
- j = selectedFields.iterator();
- }
- else {
- j = anEntity.getFieldNames().iterator();
- }
+ switch (fieldKind) {
+ case REQUEST_HEADERS:
+ if (selectedFields != null) {
+ j = selectedFields.iterator();
while (j.hasNext()) {
- String field = anEntity.getFieldValue( (String) j.next());
+ String fieldName = (String) j.next();
+ String field = aRequest.getHeader(fieldName);
if (exactMatch) {
- if (field != null && regularExpression.isMatch(field)) {
+ if (field != null && matcher.matches(field, pattern)) {
return true;
}
}
else {
- if (field != null && regularExpression.getMatch(field) != null) {
+ if (field != null && matcher.contains(field, pattern)) {
return true;
}
}
}
- }
- }
- catch (Throwable t) {
+ }
+ break;
+
+ case ENTITY_FIELDS:
+ if (selectedFields != null) {
+ j = selectedFields.iterator();
+ }
+ else {
+ j = anEntity.getFieldNames().iterator();
+ }
+
+ while (j.hasNext()) {
+ String field = anEntity.getFieldValue( (String) j.next());
+
+ if (exactMatch) {
+ if (field != null && matcher.matches(field, pattern)) {
+ return true;
+ }
+ }
+ else {
+ if (field != null && matcher.contains(field, pattern)) {
+ return true;
+ }
+ }
+ }
}
return false;
}
}
};
}
- catch (Throwable t) {
+ catch (MalformedPatternException t) {
throw new AbuseExc("Invalid expression: " + t.getMessage());
}
}