优化WAF中前缀和后缀相关操作符性能

This commit is contained in:
GoEdgeLab
2023-12-07 12:05:08 +08:00
parent f55a49bb0d
commit 4f96c72212
2 changed files with 78 additions and 33 deletions

View File

@@ -510,13 +510,27 @@ func (this *Rule) Test(value any) bool {
}
case RuleOperatorPrefix:
if this.IsCaseInsensitive {
return strings.HasPrefix(strings.ToLower(this.stringifyValue(value)), strings.ToLower(this.Value))
var s = this.stringifyValue(value)
var sl = len(s)
var vl = len(this.Value)
if sl < vl {
return false
}
s = s[:vl]
return strings.HasPrefix(strings.ToLower(s), strings.ToLower(this.Value))
} else {
return strings.HasPrefix(this.stringifyValue(value), this.Value)
}
case RuleOperatorSuffix:
if this.IsCaseInsensitive {
return strings.HasSuffix(strings.ToLower(this.stringifyValue(value)), strings.ToLower(this.Value))
var s = this.stringifyValue(value)
var sl = len(s)
var vl = len(this.Value)
if sl < vl {
return false
}
s = s[sl-vl:]
return strings.HasSuffix(strings.ToLower(s), strings.ToLower(this.Value))
} else {
return strings.HasSuffix(this.stringifyValue(value), this.Value)
}