请求条件增加不区分大小写选项

This commit is contained in:
刘祥超
2021-12-12 16:11:11 +08:00
parent 15c952e6eb
commit f080088e19

View File

@@ -26,9 +26,10 @@ type HTTPRequestCond struct {
// ${arg.name}, ${requestPath} // ${arg.name}, ${requestPath}
Param string `yaml:"param" json:"param"` Param string `yaml:"param" json:"param"`
Operator RequestCondOperator `yaml:"operator" json:"operator"` // 运算符 Operator RequestCondOperator `yaml:"operator" json:"operator"` // 运算符
Value string `yaml:"value" json:"value"` // 对比值 Value string `yaml:"value" json:"value"` // 对比值
IsReverse bool `yaml:"isReverse" json:"isReverse"` // 是否反向匹配 IsReverse bool `yaml:"isReverse" json:"isReverse"` // 是否反向匹配
IsCaseInsensitive bool `yaml:"isCaseInsensitive" json:"isCaseInsensitive"` // 大小写是否敏感
isInt bool isInt bool
isFloat bool isFloat bool
@@ -49,7 +50,11 @@ func (this *HTTPRequestCond) Init() error {
RequestCondOperatorRegexp, RequestCondOperatorRegexp,
RequestCondOperatorNotRegexp, RequestCondOperatorNotRegexp,
}, this.Operator) { }, this.Operator) {
reg, err := regexp.Compile(this.Value) var value = this.Value
if this.IsCaseInsensitive && !strings.HasPrefix(this.Value, "(?i)") {
value = "(?i)" + value
}
reg, err := regexp.Compile(value)
if err != nil { if err != nil {
return err return err
} }
@@ -186,16 +191,34 @@ func (this *HTTPRequestCond) match(formatter func(source string) string) bool {
case RequestCondOperatorMod100: case RequestCondOperatorMod100:
return types.Int64(paramValue)%100 == types.Int64(this.Value) return types.Int64(paramValue)%100 == types.Int64(this.Value)
case RequestCondOperatorEqString: case RequestCondOperatorEqString:
if this.IsCaseInsensitive {
return strings.ToUpper(paramValue) == strings.ToUpper(this.Value)
}
return paramValue == this.Value return paramValue == this.Value
case RequestCondOperatorNeqString: case RequestCondOperatorNeqString:
if this.IsCaseInsensitive {
return strings.ToUpper(paramValue) != strings.ToUpper(this.Value)
}
return paramValue != this.Value return paramValue != this.Value
case RequestCondOperatorHasPrefix: case RequestCondOperatorHasPrefix:
if this.IsCaseInsensitive {
return strings.HasPrefix(strings.ToUpper(paramValue), strings.ToUpper(this.Value))
}
return strings.HasPrefix(paramValue, this.Value) return strings.HasPrefix(paramValue, this.Value)
case RequestCondOperatorHasSuffix: case RequestCondOperatorHasSuffix:
if this.IsCaseInsensitive {
return strings.HasSuffix(strings.ToUpper(paramValue), strings.ToUpper(this.Value))
}
return strings.HasSuffix(paramValue, this.Value) return strings.HasSuffix(paramValue, this.Value)
case RequestCondOperatorContainsString: case RequestCondOperatorContainsString:
if this.IsCaseInsensitive {
return strings.Contains(strings.ToUpper(paramValue), strings.ToUpper(this.Value))
}
return strings.Contains(paramValue, this.Value) return strings.Contains(paramValue, this.Value)
case RequestCondOperatorNotContainsString: case RequestCondOperatorNotContainsString:
if this.IsCaseInsensitive {
return !strings.Contains(strings.ToUpper(paramValue), strings.ToUpper(this.Value))
}
return !strings.Contains(paramValue, this.Value) return !strings.Contains(paramValue, this.Value)
case RequestCondOperatorEqIP: case RequestCondOperatorEqIP:
ip := net.ParseIP(paramValue) ip := net.ParseIP(paramValue)
@@ -272,9 +295,29 @@ func (this *HTTPRequestCond) match(formatter func(source string) string) bool {
return false return false
} }
case RequestCondOperatorIn: case RequestCondOperatorIn:
return lists.ContainsString(this.arrayValue, paramValue) if this.IsCaseInsensitive {
paramValue = strings.ToUpper(paramValue)
for _, v := range this.arrayValue {
if strings.ToUpper(v) == paramValue {
return true
}
}
return false
} else {
return lists.ContainsString(this.arrayValue, paramValue)
}
case RequestCondOperatorNotIn: case RequestCondOperatorNotIn:
return !lists.ContainsString(this.arrayValue, paramValue) if this.IsCaseInsensitive {
paramValue = strings.ToUpper(paramValue)
for _, v := range this.arrayValue {
if strings.ToUpper(v) == paramValue {
return false
}
}
return true
} else {
return !lists.ContainsString(this.arrayValue, paramValue)
}
case RequestCondOperatorFileExt: case RequestCondOperatorFileExt:
ext := filepath.Ext(paramValue) ext := filepath.Ext(paramValue)
if len(ext) > 0 { if len(ext) > 0 {