diff --git a/caddyhttp/httpserver/condition.go b/caddyhttp/httpserver/condition.go index a45dffe0..986125c1 100644 --- a/caddyhttp/httpserver/condition.go +++ b/caddyhttp/httpserver/condition.go @@ -52,6 +52,7 @@ const ( startsWithOp = "starts_with" notStartsWithOp = "not_starts_with" endsWithOp = "ends_with" + notEndsWithOp = "not_ends_with" matchOp = "match" notMatchOp = "not_match" ) @@ -71,6 +72,7 @@ var ifConditions = map[string]ifCondition{ startsWithOp: startsWithFunc, notStartsWithOp: notStartsWithFunc, endsWithOp: endsWithFunc, + notEndsWithOp: notEndsWithFunc, matchOp: matchFunc, notMatchOp: notMatchFunc, } @@ -84,7 +86,7 @@ func isFunc(a, b string) bool { // notFunc is condition for Not operator. // It checks for inequality. func notFunc(a, b string) bool { - return a != b + return !isFunc(a, b) } // hasFunc is condition for Has operator. @@ -96,7 +98,7 @@ func hasFunc(a, b string) bool { // 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) + return !hasFunc(a, b) } // startsWithFunc is condition for StartsWith operator. @@ -108,7 +110,7 @@ func startsWithFunc(a, b string) bool { // notStartsWithFunc is condition for NotStartsWith operator. // It checks if b is not a prefix of a. func notStartsWithFunc(a, b string) bool { - return !strings.HasPrefix(a, b) + return !startsWithFunc(a, b) } // endsWithFunc is condition for EndsWith operator. @@ -117,6 +119,12 @@ func endsWithFunc(a, b string) bool { return strings.HasSuffix(a, b) } +// notEndsWithFunc is condition for NotEndsWith operator. +// It checks if b is not a suffix of a. +func notEndsWithFunc(a, b string) bool { + return !endsWithFunc(a, b) +} + // matchFunc is condition for Match operator. // It does regexp matching of a against pattern in b // and returns if they match. @@ -129,8 +137,7 @@ func matchFunc(a, b string) bool { // 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 + return !matchFunc(a, b) } // ifCond is statement for a IfMatcher condition. diff --git a/caddyhttp/httpserver/condition_test.go b/caddyhttp/httpserver/condition_test.go index 4ccac0e0..0b50450f 100644 --- a/caddyhttp/httpserver/condition_test.go +++ b/caddyhttp/httpserver/condition_test.go @@ -38,6 +38,9 @@ func TestConditions(t *testing.T) { {"bab ends_with bb", false}, {"bab ends_with bab", true}, {"bab ends_with ab", true}, + {"bab not_ends_with bb", true}, + {"bab not_ends_with ab", false}, + {"bab not_ends_with bab", false}, {"a match *", false}, {"a match a", true}, {"a match .*", true}, @@ -224,7 +227,7 @@ func TestSetupIfMatcher(t *testing.T) { }, {`test { if goal has go - if cook not_has go + if cook not_has go }`, false, IfMatcher{ ifs: []ifCond{ {a: "goal", op: "has", b: "go"}, @@ -233,7 +236,7 @@ func TestSetupIfMatcher(t *testing.T) { }}, {`test { if goal has go - if cook not_has go + if cook not_has go if_op and }`, false, IfMatcher{ ifs: []ifCond{ @@ -243,7 +246,7 @@ func TestSetupIfMatcher(t *testing.T) { }}, {`test { if goal has go - if cook not_has go + if cook not_has go if_op not }`, true, IfMatcher{}, },