优化URL匹配方法

This commit is contained in:
GoEdgeLab
2023-03-07 17:21:38 +08:00
parent 9a779bbc5f
commit 0145758e70
2 changed files with 7 additions and 1 deletions

View File

@@ -34,7 +34,7 @@ func (this *URLPattern) Init() error {
for index, piece := range pieces { for index, piece := range pieces {
pieces[index] = regexp.QuoteMeta(piece) pieces[index] = regexp.QuoteMeta(piece)
} }
reg, err := regexp.Compile("(?i)" /** 大小写不敏感 **/ + strings.Join(pieces, "(.*)")) reg, err := regexp.Compile("(?i)" /** 大小写不敏感 **/ + "^" + strings.Join(pieces, "(.*)") + "$")
if err != nil { if err != nil {
return err return err
} }

View File

@@ -43,6 +43,12 @@ func TestURLPattern_Match(t *testing.T) {
url: "https://example.com", url: "https://example.com",
result: true, result: true,
}, },
{
patternType: "wildcard",
pattern: "*COM",
url: "https://example.com/hello",
result: false,
},
{ {
patternType: "wildcard", patternType: "wildcard",
pattern: "http://*", pattern: "http://*",