From 33e1560d53d2baa84f855563a1ed3a74da5bd5d2 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 24 May 2017 10:32:53 -0500 Subject: [PATCH] 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 --- caddyhttp/httpserver/condition.go | 40 +++++++++++++++----------- caddyhttp/httpserver/condition_test.go | 3 ++ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/caddyhttp/httpserver/condition.go b/caddyhttp/httpserver/condition.go index 39ea5a7c4..a45dffe0c 100644 --- a/caddyhttp/httpserver/condition.go +++ b/caddyhttp/httpserver/condition.go @@ -45,14 +45,15 @@ func SetupIfMatcher(controller *caddy.Controller) (RequestMatcher, error) { // operators const ( - isOp = "is" - notOp = "not" - hasOp = "has" - notHasOp = "not_has" - startsWithOp = "starts_with" - endsWithOp = "ends_with" - matchOp = "match" - notMatchOp = "not_match" + isOp = "is" + notOp = "not" + hasOp = "has" + notHasOp = "not_has" + startsWithOp = "starts_with" + notStartsWithOp = "not_starts_with" + endsWithOp = "ends_with" + matchOp = "match" + notMatchOp = "not_match" ) func operatorError(operator string) error { @@ -63,14 +64,15 @@ func operatorError(operator string) error { type ifCondition func(string, string) bool var ifConditions = map[string]ifCondition{ - isOp: isFunc, - notOp: notFunc, - hasOp: hasFunc, - notHasOp: notHasFunc, - startsWithOp: startsWithFunc, - endsWithOp: endsWithFunc, - matchOp: matchFunc, - notMatchOp: notMatchFunc, + isOp: isFunc, + notOp: notFunc, + hasOp: hasFunc, + notHasOp: notHasFunc, + startsWithOp: startsWithFunc, + notStartsWithOp: notStartsWithFunc, + endsWithOp: endsWithFunc, + matchOp: matchFunc, + notMatchOp: notMatchFunc, } // isFunc is condition for Is operator. @@ -103,6 +105,12 @@ func startsWithFunc(a, b string) bool { 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. // It checks if b is a suffix of a. func endsWithFunc(a, b string) bool { diff --git a/caddyhttp/httpserver/condition_test.go b/caddyhttp/httpserver/condition_test.go index 76ba0475f..4ccac0e07 100644 --- a/caddyhttp/httpserver/condition_test.go +++ b/caddyhttp/httpserver/condition_test.go @@ -32,6 +32,9 @@ func TestConditions(t *testing.T) { {"bab starts_with bb", false}, {"bab starts_with ba", 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 bab", true}, {"bab ends_with ab", true},