mirror of
https://github.com/caddyserver/caddy.git
synced 2025-03-23 05:49:27 +01:00
httpserver: Add not_starts_with condition. (#1687)
* Add not_starts_with condition. This adds the opposite of the starts_with condition, to check if a given string does not start with another string. * Correct white space problems
This commit is contained in:
parent
a5eb552215
commit
33e1560d53
2 changed files with 27 additions and 16 deletions
|
@ -45,14 +45,15 @@ func SetupIfMatcher(controller *caddy.Controller) (RequestMatcher, error) {
|
||||||
|
|
||||||
// operators
|
// operators
|
||||||
const (
|
const (
|
||||||
isOp = "is"
|
isOp = "is"
|
||||||
notOp = "not"
|
notOp = "not"
|
||||||
hasOp = "has"
|
hasOp = "has"
|
||||||
notHasOp = "not_has"
|
notHasOp = "not_has"
|
||||||
startsWithOp = "starts_with"
|
startsWithOp = "starts_with"
|
||||||
endsWithOp = "ends_with"
|
notStartsWithOp = "not_starts_with"
|
||||||
matchOp = "match"
|
endsWithOp = "ends_with"
|
||||||
notMatchOp = "not_match"
|
matchOp = "match"
|
||||||
|
notMatchOp = "not_match"
|
||||||
)
|
)
|
||||||
|
|
||||||
func operatorError(operator string) error {
|
func operatorError(operator string) error {
|
||||||
|
@ -63,14 +64,15 @@ func operatorError(operator string) error {
|
||||||
type ifCondition func(string, string) bool
|
type ifCondition func(string, string) bool
|
||||||
|
|
||||||
var ifConditions = map[string]ifCondition{
|
var ifConditions = map[string]ifCondition{
|
||||||
isOp: isFunc,
|
isOp: isFunc,
|
||||||
notOp: notFunc,
|
notOp: notFunc,
|
||||||
hasOp: hasFunc,
|
hasOp: hasFunc,
|
||||||
notHasOp: notHasFunc,
|
notHasOp: notHasFunc,
|
||||||
startsWithOp: startsWithFunc,
|
startsWithOp: startsWithFunc,
|
||||||
endsWithOp: endsWithFunc,
|
notStartsWithOp: notStartsWithFunc,
|
||||||
matchOp: matchFunc,
|
endsWithOp: endsWithFunc,
|
||||||
notMatchOp: notMatchFunc,
|
matchOp: matchFunc,
|
||||||
|
notMatchOp: notMatchFunc,
|
||||||
}
|
}
|
||||||
|
|
||||||
// isFunc is condition for Is operator.
|
// isFunc is condition for Is operator.
|
||||||
|
@ -103,6 +105,12 @@ func startsWithFunc(a, b string) bool {
|
||||||
return strings.HasPrefix(a, b)
|
return strings.HasPrefix(a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
// endsWithFunc is condition for EndsWith operator.
|
// endsWithFunc is condition for EndsWith operator.
|
||||||
// It checks if b is a suffix of a.
|
// It checks if b is a suffix of a.
|
||||||
func endsWithFunc(a, b string) bool {
|
func endsWithFunc(a, b string) bool {
|
||||||
|
|
|
@ -32,6 +32,9 @@ func TestConditions(t *testing.T) {
|
||||||
{"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},
|
||||||
|
{"bab not_starts_with bb", true},
|
||||||
|
{"bab not_starts_with ba", false},
|
||||||
|
{"bab not_starts_with bab", false},
|
||||||
{"bab ends_with bb", false},
|
{"bab ends_with bb", false},
|
||||||
{"bab ends_with bab", true},
|
{"bab ends_with bab", true},
|
||||||
{"bab ends_with ab", true},
|
{"bab ends_with ab", true},
|
||||||
|
|
Loading…
Reference in a new issue