mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-23 18:55:49 +03:00
Add not_has and not_match conditions.
This commit is contained in:
parent
4636ca1051
commit
55f69fd742
2 changed files with 35 additions and 0 deletions
|
@ -14,9 +14,11 @@ const (
|
||||||
Is = "is"
|
Is = "is"
|
||||||
Not = "not"
|
Not = "not"
|
||||||
Has = "has"
|
Has = "has"
|
||||||
|
NotHas = "not_has"
|
||||||
StartsWith = "starts_with"
|
StartsWith = "starts_with"
|
||||||
EndsWith = "ends_with"
|
EndsWith = "ends_with"
|
||||||
Match = "match"
|
Match = "match"
|
||||||
|
NotMatch = "not_match"
|
||||||
)
|
)
|
||||||
|
|
||||||
func operatorError(operator string) error {
|
func operatorError(operator string) error {
|
||||||
|
@ -34,9 +36,11 @@ var conditions = map[string]condition{
|
||||||
Is: isFunc,
|
Is: isFunc,
|
||||||
Not: notFunc,
|
Not: notFunc,
|
||||||
Has: hasFunc,
|
Has: hasFunc,
|
||||||
|
NotHas: notHasFunc,
|
||||||
StartsWith: startsWithFunc,
|
StartsWith: startsWithFunc,
|
||||||
EndsWith: endsWithFunc,
|
EndsWith: endsWithFunc,
|
||||||
Match: matchFunc,
|
Match: matchFunc,
|
||||||
|
NotMatch: notMatchFunc,
|
||||||
}
|
}
|
||||||
|
|
||||||
// isFunc is condition for Is operator.
|
// isFunc is condition for Is operator.
|
||||||
|
@ -57,6 +61,12 @@ func hasFunc(a, b string) bool {
|
||||||
return strings.Contains(a, b)
|
return strings.Contains(a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// notHasFunc is condition for NotHas operator.
|
||||||
|
// It checks if b is not a substring of a.
|
||||||
|
func notHasFunc(a, b string) bool {
|
||||||
|
return !strings.Contains(a, b)
|
||||||
|
}
|
||||||
|
|
||||||
// startsWithFunc is condition for StartsWith operator.
|
// startsWithFunc is condition for StartsWith operator.
|
||||||
// It checks if b is a prefix of a.
|
// It checks if b is a prefix of a.
|
||||||
func startsWithFunc(a, b string) bool {
|
func startsWithFunc(a, b string) bool {
|
||||||
|
@ -71,11 +81,20 @@ func endsWithFunc(a, b string) bool {
|
||||||
|
|
||||||
// matchFunc is condition for Match operator.
|
// matchFunc is condition for Match operator.
|
||||||
// It does regexp matching of a against pattern in b
|
// It does regexp matching of a against pattern in b
|
||||||
|
// and returns if they match.
|
||||||
func matchFunc(a, b string) bool {
|
func matchFunc(a, b string) bool {
|
||||||
matched, _ := regexp.MatchString(b, a)
|
matched, _ := regexp.MatchString(b, a)
|
||||||
return matched
|
return matched
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// notMatchFunc is condition for NotMatch operator.
|
||||||
|
// It does regexp matching of a against pattern in b
|
||||||
|
// and returns if they do not match.
|
||||||
|
func notMatchFunc(a, b string) bool {
|
||||||
|
matched, _ := regexp.MatchString(b, a)
|
||||||
|
return !matched
|
||||||
|
}
|
||||||
|
|
||||||
// If is statement for a rewrite condition.
|
// If is statement for a rewrite condition.
|
||||||
type If struct {
|
type If struct {
|
||||||
A string
|
A string
|
||||||
|
|
|
@ -20,6 +20,11 @@ func TestConditions(t *testing.T) {
|
||||||
{"ba has b", true},
|
{"ba has b", true},
|
||||||
{"bab has b", true},
|
{"bab has b", true},
|
||||||
{"bab has bb", false},
|
{"bab has bb", false},
|
||||||
|
{"a not_has a", false},
|
||||||
|
{"a not_has b", true},
|
||||||
|
{"ba not_has b", false},
|
||||||
|
{"bab not_has b", false},
|
||||||
|
{"bab not_has bb", true},
|
||||||
{"bab starts_with bb", false},
|
{"bab starts_with bb", false},
|
||||||
{"bab starts_with ba", true},
|
{"bab starts_with ba", true},
|
||||||
{"bab starts_with bab", true},
|
{"bab starts_with bab", true},
|
||||||
|
@ -37,6 +42,17 @@ func TestConditions(t *testing.T) {
|
||||||
{"b0a match b[a-z]", false},
|
{"b0a match b[a-z]", false},
|
||||||
{"b0a match b[a-z]+", false},
|
{"b0a match b[a-z]+", false},
|
||||||
{"b0a match b[a-z0-9]+", true},
|
{"b0a match b[a-z0-9]+", true},
|
||||||
|
{"a not_match *", true},
|
||||||
|
{"a not_match a", false},
|
||||||
|
{"a not_match .*", false},
|
||||||
|
{"a not_match a.*", false},
|
||||||
|
{"a not_match b.*", true},
|
||||||
|
{"ba not_match b.*", false},
|
||||||
|
{"ba not_match b[a-z]", false},
|
||||||
|
{"b0 not_match b[a-z]", true},
|
||||||
|
{"b0a not_match b[a-z]", true},
|
||||||
|
{"b0a not_match b[a-z]+", true},
|
||||||
|
{"b0a not_match b[a-z0-9]+", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
|
|
Loading…
Reference in a new issue