优化代码

This commit is contained in:
GoEdgeLab
2023-08-08 15:12:28 +08:00
parent a7a170f9c6
commit 98eac40783
21 changed files with 61 additions and 78 deletions

View File

@@ -214,12 +214,12 @@ func (this *HTTPRequestCond) match(formatter func(source string) string) bool {
return types.Int64(paramValue)%100 == types.Int64(this.Value)
case RequestCondOperatorEqString:
if this.IsCaseInsensitive {
return strings.ToUpper(paramValue) == strings.ToUpper(this.Value)
return strings.EqualFold(paramValue, this.Value)
}
return paramValue == this.Value
case RequestCondOperatorNeqString:
if this.IsCaseInsensitive {
return strings.ToUpper(paramValue) != strings.ToUpper(this.Value)
return !strings.EqualFold(paramValue, this.Value)
}
return paramValue != this.Value
case RequestCondOperatorHasPrefix:
@@ -243,11 +243,11 @@ func (this *HTTPRequestCond) match(formatter func(source string) string) bool {
}
return !strings.Contains(paramValue, this.Value)
case RequestCondOperatorEqIP:
ip := net.ParseIP(paramValue)
var ip = net.ParseIP(paramValue)
if ip == nil {
return false
}
return this.isIP && bytes.Compare(this.ipValue, ip) == 0
return this.isIP && ip.Equal(this.ipValue)
case RequestCondOperatorGtIP:
ip := net.ParseIP(paramValue)
if ip == nil {

View File

@@ -9,5 +9,5 @@ var (
RegexpAllDigitNumber = regexp.MustCompile(`^[+-]?\d+$`) // 整数,支持正负数
RegexpAllFloatNumber = regexp.MustCompile(`^[+-]?\d+(\.\d+)?$`) // 浮点数支持正负数不支持e
RegexpExternalURL = regexp.MustCompile("(?i)^(http|https|ftp)://") // URL
RegexpNamedVariable = regexp.MustCompile("\\${[\\w.-]+}") // 命名变量
RegexpNamedVariable = regexp.MustCompile(`\${[\w.-]+}`) // 命名变量
)

View File

@@ -1,17 +1,21 @@
package shared
package shared_test
import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/assert"
"testing"
)
func TestRegexp(t *testing.T) {
a := assert.NewAssertion(t)
var a = assert.NewAssertion(t)
a.IsTrue(RegexpFloatNumber.MatchString("123"))
a.IsTrue(RegexpFloatNumber.MatchString("123.456"))
a.IsFalse(RegexpFloatNumber.MatchString(".456"))
a.IsFalse(RegexpFloatNumber.MatchString("abc"))
a.IsFalse(RegexpFloatNumber.MatchString("123."))
a.IsFalse(RegexpFloatNumber.MatchString("123.456e7"))
a.IsTrue(shared.RegexpFloatNumber.MatchString("123"))
a.IsTrue(shared.RegexpFloatNumber.MatchString("123.456"))
a.IsFalse(shared.RegexpFloatNumber.MatchString(".456"))
a.IsFalse(shared.RegexpFloatNumber.MatchString("abc"))
a.IsFalse(shared.RegexpFloatNumber.MatchString("123."))
a.IsFalse(shared.RegexpFloatNumber.MatchString("123.456e7"))
a.IsTrue(shared.RegexpNamedVariable.MatchString("${abc.efg}"))
a.IsTrue(shared.RegexpNamedVariable.MatchString("${abc}"))
a.IsFalse(shared.RegexpNamedVariable.MatchString("{abc.efg}"))
}