mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-14 21:40:28 +08:00
缓存条件类型增加“URL通配符”
This commit is contained in:
@@ -5,12 +5,10 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/lists"
|
"github.com/iwind/TeaGo/lists"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"github.com/iwind/TeaGo/utils/string"
|
"github.com/iwind/TeaGo/utils/string"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -59,6 +57,20 @@ func (this *HTTPRequestCond) Init() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
this.regValue = reg
|
this.regValue = reg
|
||||||
|
} else if lists.ContainsString([]string{
|
||||||
|
RequestCondOperatorWildcardMatch,
|
||||||
|
RequestCondOperatorWildcardNotMatch,
|
||||||
|
}, this.Operator) {
|
||||||
|
var pieces = strings.Split(this.Value, "*")
|
||||||
|
for index, piece := range pieces {
|
||||||
|
pieces[index] = regexp.QuoteMeta(piece)
|
||||||
|
}
|
||||||
|
var pattern = strings.Join(pieces, "(.*)")
|
||||||
|
reg, err := regexp.Compile("(?i)" /** 大小写不敏感 **/ + "^" + pattern + "$")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
this.regValue = reg
|
||||||
} else if lists.ContainsString([]string{
|
} else if lists.ContainsString([]string{
|
||||||
RequestCondOperatorEqFloat,
|
RequestCondOperatorEqFloat,
|
||||||
RequestCondOperatorGtFloat,
|
RequestCondOperatorGtFloat,
|
||||||
@@ -162,6 +174,16 @@ func (this *HTTPRequestCond) match(formatter func(source string) string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return !this.regValue.MatchString(paramValue)
|
return !this.regValue.MatchString(paramValue)
|
||||||
|
case RequestCondOperatorWildcardMatch:
|
||||||
|
if this.regValue == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return this.regValue.MatchString(paramValue)
|
||||||
|
case RequestCondOperatorWildcardNotMatch:
|
||||||
|
if this.regValue == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return !this.regValue.MatchString(paramValue)
|
||||||
case RequestCondOperatorEqInt:
|
case RequestCondOperatorEqInt:
|
||||||
return this.isInt && paramValue == this.Value
|
return this.isInt && paramValue == this.Value
|
||||||
case RequestCondOperatorEqFloat:
|
case RequestCondOperatorEqFloat:
|
||||||
@@ -373,32 +395,32 @@ func (this *HTTPRequestCond) match(formatter func(source string) string) bool {
|
|||||||
return this.ipToInt64(net.ParseIP(paramValue))%10 == types.Int64(this.Value)
|
return this.ipToInt64(net.ParseIP(paramValue))%10 == types.Int64(this.Value)
|
||||||
case RequestCondOperatorIPMod100:
|
case RequestCondOperatorIPMod100:
|
||||||
return this.ipToInt64(net.ParseIP(paramValue))%100 == types.Int64(this.Value)
|
return this.ipToInt64(net.ParseIP(paramValue))%100 == types.Int64(this.Value)
|
||||||
case RequestCondOperatorFileExist:
|
/**case RequestCondOperatorFileExist:
|
||||||
index := strings.Index(paramValue, "?")
|
index := strings.Index(paramValue, "?")
|
||||||
if index > -1 {
|
if index > -1 {
|
||||||
paramValue = paramValue[:index]
|
paramValue = paramValue[:index]
|
||||||
}
|
}
|
||||||
if len(paramValue) == 0 {
|
if len(paramValue) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !filepath.IsAbs(paramValue) {
|
if !filepath.IsAbs(paramValue) {
|
||||||
paramValue = Tea.Root + Tea.DS + paramValue
|
paramValue = Tea.Root + Tea.DS + paramValue
|
||||||
}
|
}
|
||||||
stat, err := os.Stat(paramValue)
|
stat, err := os.Stat(paramValue)
|
||||||
return err == nil && !stat.IsDir()
|
return err == nil && !stat.IsDir()
|
||||||
case RequestCondOperatorFileNotExist:
|
case RequestCondOperatorFileNotExist:
|
||||||
index := strings.Index(paramValue, "?")
|
index := strings.Index(paramValue, "?")
|
||||||
if index > -1 {
|
if index > -1 {
|
||||||
paramValue = paramValue[:index]
|
paramValue = paramValue[:index]
|
||||||
}
|
}
|
||||||
if len(paramValue) == 0 {
|
if len(paramValue) == 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if !filepath.IsAbs(paramValue) {
|
if !filepath.IsAbs(paramValue) {
|
||||||
paramValue = Tea.Root + Tea.DS + paramValue
|
paramValue = Tea.Root + Tea.DS + paramValue
|
||||||
}
|
}
|
||||||
stat, err := os.Stat(paramValue)
|
stat, err := os.Stat(paramValue)
|
||||||
return err != nil || stat.IsDir()
|
return err != nil || stat.IsDir()**/
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package shared
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
|
||||||
"github.com/iwind/TeaGo/assert"
|
"github.com/iwind/TeaGo/assert"
|
||||||
"net"
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -85,6 +84,78 @@ func TestRequestCond_Compare1(t *testing.T) {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var cond = HTTPRequestCond{
|
||||||
|
Param: "/hello",
|
||||||
|
Operator: RequestCondOperatorWildcardMatch,
|
||||||
|
Value: "/*",
|
||||||
|
}
|
||||||
|
a.IsNil(cond.Init())
|
||||||
|
a.IsTrue(cond.Match(func(format string) string {
|
||||||
|
return format
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var cond = HTTPRequestCond{
|
||||||
|
Param: "/hello/world",
|
||||||
|
Operator: RequestCondOperatorWildcardMatch,
|
||||||
|
Value: "/*/world",
|
||||||
|
}
|
||||||
|
a.IsNil(cond.Init())
|
||||||
|
a.IsTrue(cond.Match(func(format string) string {
|
||||||
|
return format
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var cond = HTTPRequestCond{
|
||||||
|
Param: "/hello/world",
|
||||||
|
Operator: RequestCondOperatorWildcardMatch,
|
||||||
|
Value: "/H*/world",
|
||||||
|
}
|
||||||
|
a.IsNil(cond.Init())
|
||||||
|
a.IsTrue(cond.Match(func(format string) string {
|
||||||
|
return format
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var cond = HTTPRequestCond{
|
||||||
|
Param: "/hello",
|
||||||
|
Operator: RequestCondOperatorWildcardMatch,
|
||||||
|
Value: "/hello/*",
|
||||||
|
}
|
||||||
|
a.IsNil(cond.Init())
|
||||||
|
a.IsFalse(cond.Match(func(format string) string {
|
||||||
|
return format
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var cond = HTTPRequestCond{
|
||||||
|
Param: "/hello/world",
|
||||||
|
Operator: RequestCondOperatorWildcardNotMatch,
|
||||||
|
Value: "/hello/*",
|
||||||
|
}
|
||||||
|
a.IsNil(cond.Init())
|
||||||
|
a.IsFalse(cond.Match(func(format string) string {
|
||||||
|
return format
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var cond = HTTPRequestCond{
|
||||||
|
Param: "/hello",
|
||||||
|
Operator: RequestCondOperatorWildcardNotMatch,
|
||||||
|
Value: "/hello/*",
|
||||||
|
}
|
||||||
|
a.IsNil(cond.Init())
|
||||||
|
a.IsTrue(cond.Match(func(format string) string {
|
||||||
|
return format
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
cond := HTTPRequestCond{
|
cond := HTTPRequestCond{
|
||||||
Param: "123.123",
|
Param: "123.123",
|
||||||
@@ -687,7 +758,7 @@ func TestRequestCond_File(t *testing.T) {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
/**{
|
||||||
cond := HTTPRequestCond{
|
cond := HTTPRequestCond{
|
||||||
Param: "a.png",
|
Param: "a.png",
|
||||||
Operator: RequestCondOperatorFileExist,
|
Operator: RequestCondOperatorFileExist,
|
||||||
@@ -762,7 +833,7 @@ func TestRequestCond_File(t *testing.T) {
|
|||||||
a.IsFalse(cond.Match(func(source string) string {
|
a.IsFalse(cond.Match(func(source string) string {
|
||||||
return source
|
return source
|
||||||
}))
|
}))
|
||||||
}
|
}**/
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRequestCond_MimeType(t *testing.T) {
|
func TestRequestCond_MimeType(t *testing.T) {
|
||||||
|
|||||||
@@ -2,15 +2,23 @@ package shared
|
|||||||
|
|
||||||
import "github.com/iwind/TeaGo/maps"
|
import "github.com/iwind/TeaGo/maps"
|
||||||
|
|
||||||
// 运算符定义
|
// RequestCondOperator 运算符定义
|
||||||
type RequestCondOperator = string
|
type RequestCondOperator = string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
||||||
// 正则
|
// 正则
|
||||||
|
|
||||||
RequestCondOperatorRegexp RequestCondOperator = "regexp"
|
RequestCondOperatorRegexp RequestCondOperator = "regexp"
|
||||||
RequestCondOperatorNotRegexp RequestCondOperator = "not regexp"
|
RequestCondOperatorNotRegexp RequestCondOperator = "not regexp"
|
||||||
|
|
||||||
|
// 通配符
|
||||||
|
|
||||||
|
RequestCondOperatorWildcardMatch RequestCondOperator = "wildcard match"
|
||||||
|
RequestCondOperatorWildcardNotMatch RequestCondOperator = "wildcard not match"
|
||||||
|
|
||||||
// 数字相关
|
// 数字相关
|
||||||
|
|
||||||
RequestCondOperatorEqInt RequestCondOperator = "eq int" // 整数等于
|
RequestCondOperatorEqInt RequestCondOperator = "eq int" // 整数等于
|
||||||
RequestCondOperatorEqFloat RequestCondOperator = "eq float" // 浮点数等于
|
RequestCondOperatorEqFloat RequestCondOperator = "eq float" // 浮点数等于
|
||||||
RequestCondOperatorGtFloat RequestCondOperator = "gt"
|
RequestCondOperatorGtFloat RequestCondOperator = "gt"
|
||||||
@@ -19,11 +27,13 @@ const (
|
|||||||
RequestCondOperatorLteFloat RequestCondOperator = "lte"
|
RequestCondOperatorLteFloat RequestCondOperator = "lte"
|
||||||
|
|
||||||
// 取模
|
// 取模
|
||||||
|
|
||||||
RequestCondOperatorMod10 RequestCondOperator = "mod 10"
|
RequestCondOperatorMod10 RequestCondOperator = "mod 10"
|
||||||
RequestCondOperatorMod100 RequestCondOperator = "mod 100"
|
RequestCondOperatorMod100 RequestCondOperator = "mod 100"
|
||||||
RequestCondOperatorMod RequestCondOperator = "mod"
|
RequestCondOperatorMod RequestCondOperator = "mod"
|
||||||
|
|
||||||
// 字符串相关
|
// 字符串相关
|
||||||
|
|
||||||
RequestCondOperatorEqString RequestCondOperator = "eq"
|
RequestCondOperatorEqString RequestCondOperator = "eq"
|
||||||
RequestCondOperatorNeqString RequestCondOperator = "not"
|
RequestCondOperatorNeqString RequestCondOperator = "not"
|
||||||
RequestCondOperatorHasPrefix RequestCondOperator = "prefix"
|
RequestCondOperatorHasPrefix RequestCondOperator = "prefix"
|
||||||
@@ -37,6 +47,7 @@ const (
|
|||||||
RequestCondOperatorVersionRange RequestCondOperator = "version range"
|
RequestCondOperatorVersionRange RequestCondOperator = "version range"
|
||||||
|
|
||||||
// IP相关
|
// IP相关
|
||||||
|
|
||||||
RequestCondOperatorEqIP RequestCondOperator = "eq ip"
|
RequestCondOperatorEqIP RequestCondOperator = "eq ip"
|
||||||
RequestCondOperatorGtIP RequestCondOperator = "gt ip"
|
RequestCondOperatorGtIP RequestCondOperator = "gt ip"
|
||||||
RequestCondOperatorGteIP RequestCondOperator = "gte ip"
|
RequestCondOperatorGteIP RequestCondOperator = "gte ip"
|
||||||
@@ -48,11 +59,13 @@ const (
|
|||||||
RequestCondOperatorIPMod RequestCondOperator = "ip mod"
|
RequestCondOperatorIPMod RequestCondOperator = "ip mod"
|
||||||
|
|
||||||
// 文件相关
|
// 文件相关
|
||||||
RequestCondOperatorFileExist RequestCondOperator = "file exist"
|
// 为了安全暂时不提供
|
||||||
RequestCondOperatorFileNotExist RequestCondOperator = "file not exist"
|
|
||||||
|
//RequestCondOperatorFileExist RequestCondOperator = "file exist"
|
||||||
|
//RequestCondOperatorFileNotExist RequestCondOperator = "file not exist"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 所有的运算符
|
// AllRequestOperators 所有的运算符
|
||||||
func AllRequestOperators() []maps.Map {
|
func AllRequestOperators() []maps.Map {
|
||||||
return []maps.Map{
|
return []maps.Map{
|
||||||
{
|
{
|
||||||
@@ -65,6 +78,16 @@ func AllRequestOperators() []maps.Map {
|
|||||||
"op": RequestCondOperatorNotRegexp,
|
"op": RequestCondOperatorNotRegexp,
|
||||||
"description": "判断是否正则表达式不匹配",
|
"description": "判断是否正则表达式不匹配",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "通配符匹配",
|
||||||
|
"op": RequestCondOperatorWildcardMatch,
|
||||||
|
"description": "判断是否和指定的通配符匹配",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "通配符不匹配",
|
||||||
|
"op": RequestCondOperatorWildcardNotMatch,
|
||||||
|
"description": "判断是否和指定的通配符不匹配",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "字符串等于",
|
"name": "字符串等于",
|
||||||
"op": RequestCondOperatorEqString,
|
"op": RequestCondOperatorEqString,
|
||||||
@@ -211,7 +234,7 @@ func AllRequestOperators() []maps.Map {
|
|||||||
"description": "对IP参数值取模,对比值格式为:除数,余数,比如10,1",
|
"description": "对IP参数值取模,对比值格式为:除数,余数,比如10,1",
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
/**{
|
||||||
"name": "文件存在",
|
"name": "文件存在",
|
||||||
"op": RequestCondOperatorFileExist,
|
"op": RequestCondOperatorFileExist,
|
||||||
"description": "判断参数值解析后的文件是否存在",
|
"description": "判断参数值解析后的文件是否存在",
|
||||||
@@ -221,6 +244,6 @@ func AllRequestOperators() []maps.Map {
|
|||||||
"name": "文件不存在",
|
"name": "文件不存在",
|
||||||
"op": RequestCondOperatorFileNotExist,
|
"op": RequestCondOperatorFileNotExist,
|
||||||
"description": "判断参数值解析后的文件是否不存在",
|
"description": "判断参数值解析后的文件是否不存在",
|
||||||
},
|
},**/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user