mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-02-12 09:45:38 +08:00
优化代码
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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.-]+}`) // 命名变量
|
||||
)
|
||||
|
||||
@@ -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}"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user