user agent filter now works with substrings
[mir.git] / source / mircoders / abuse / RegularExpressionFilterType.java
index 8eceafa..c62abbe 100755 (executable)
@@ -28,7 +28,6 @@
 
 package mircoders.abuse;
 
-import gnu.regexp.RE;
 import mir.entity.Entity;
 import mir.session.Request;
 
@@ -36,6 +35,8 @@ import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.oro.text.regex.*;
+
 
 /**
   * A description of a regular expression filter.
@@ -75,69 +76,66 @@ import java.util.List;
 
   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;
         }
@@ -147,7 +145,7 @@ import java.util.List;
         }
       };
     }
-    catch (Throwable t) {
+    catch (MalformedPatternException t) {
       throw new AbuseExc("Invalid expression: " + t.getMessage());
     }
   }