Skip to content
Permalink
Browse files
Add a switch to decide which regex engine should be used
  • Loading branch information
FSchumacher committed Mar 5, 2022
1 parent 432cda4 commit 283e3b32209c3124937feb6533f5f1bf2d930314
Showing 1 changed file with 55 additions and 14 deletions.
@@ -44,6 +44,7 @@
import java.util.Map;
import java.util.Set;
import java.util.prefs.Preferences;
import java.util.regex.PatternSyntaxException;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
@@ -245,6 +246,8 @@ public enum KeystoreMode {
// Although this field is mutable, it is only accessed within the synchronized method deliverSampler()
private static String LAST_REDIRECT = null;

private boolean useJavaRegex = JMeterUtils.getPropDefault("jmeter.use_java_regex", false);

private transient Daemon server;

private long lastTime = 0;// When was the last sample seen?
@@ -868,26 +871,42 @@ boolean filterContentType(SampleResult result) {
* @return boolean true if Matching expression
*/
private boolean testPattern(String expression, String sampleContentType, boolean expectedToMatch) {
if(expression != null && !expression.isEmpty()) {
if(log.isDebugEnabled()) {
log.debug(
"Testing Expression : {} on sampleContentType: {}, expected to match: {}",
expression, sampleContentType, expectedToMatch);
}
if (expression == null || expression.isEmpty()) {
return true;
}
if(log.isDebugEnabled()) {
log.debug(
"Testing Expression : {} on sampleContentType: {}, expected to match: {}",
expression, sampleContentType, expectedToMatch);
}

Pattern pattern = null;
try {
pattern = JMeterUtils.getPatternCache().getPattern(expression, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.SINGLELINE_MASK);
if(JMeterUtils.getMatcher().contains(sampleContentType, pattern) != expectedToMatch) {
return false;
}
} catch (MalformedCachePatternException e) {
log.warn("Skipped invalid content pattern: {}", expression, e);
try {
boolean contains;
if (useJavaRegex) {
contains = isContainedWithJavaRegex(expression, sampleContentType);
} else {
contains = isContainedWithOroRegex(expression, sampleContentType);
}
if (contains != expectedToMatch) {
return false;
}
} catch (PatternSyntaxException | MalformedCachePatternException e) {
log.warn("Skipped invalid content pattern: {}", expression, e);
}
return true;
}

private boolean isContainedWithJavaRegex(String expression, String sampleContentType) {
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(expression);
return pattern.matcher(sampleContentType).find();
}

private boolean isContainedWithOroRegex(String expression, String sampleContentType) {
Pattern pattern = JMeterUtils.getPatternCache().getPattern(expression,
Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.SINGLELINE_MASK);
return JMeterUtils.getMatcher().contains(sampleContentType, pattern);
}

/**
* Find if there is any AuthManager in JMeterTreeModel
* If there is no one, create and add it to tree
@@ -1348,6 +1367,28 @@ private String generateMatchUrl(HTTPSamplerBase sampler) {
}

private boolean matchesPatterns(String url, CollectionProperty patterns) {
if (useJavaRegex) {
return matchesPatternsWithJavaRegex(url, patterns);
}
return matchesPatternsWithOroRegex(url, patterns);
}

private boolean matchesPatternsWithJavaRegex(String url, CollectionProperty patterns) {
for (JMeterProperty jMeterProperty : patterns) {
String item = jMeterProperty.getStringValue();
try {
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(item);
if (pattern.matcher(url).matches()) {
return true;
}
} catch (PatternSyntaxException e) {
log.warn("Skipped invalid pattern: {}", item, e);
}
}
return false;
}

private boolean matchesPatternsWithOroRegex(String url, CollectionProperty patterns) {
for (JMeterProperty jMeterProperty : patterns) {
String item = jMeterProperty.getStringValue();
try {

0 comments on commit 283e3b3

Please sign in to comment.