diff --git a/caddyhttp/httpserver/condition.go b/caddyhttp/httpserver/condition.go index 8e33a88eb..233a23ee6 100644 --- a/caddyhttp/httpserver/condition.go +++ b/caddyhttp/httpserver/condition.go @@ -6,12 +6,13 @@ import ( "regexp" "strings" - "github.com/mholt/caddy/caddyfile" + "github.com/mholt/caddy" ) // SetupIfMatcher parses `if` or `if_op` in the current dispenser block. // It returns a RequestMatcher and an error if any. -func SetupIfMatcher(c caddyfile.Dispenser) (RequestMatcher, error) { +func SetupIfMatcher(controller *caddy.Controller) (RequestMatcher, error) { + var c = controller.Dispenser // copy the dispenser var matcher IfMatcher for c.NextBlock() { switch c.Val() { @@ -193,7 +194,13 @@ func (m IfMatcher) Or(r *http.Request) bool { return false } -// IfMatcherKeyword returns if k is a keyword for 'if' config block. -func IfMatcherKeyword(k string) bool { - return k == "if" || k == "if_op" +// IfMatcherKeyword checks if the next value in the dispenser is a keyword for 'if' config block. +// If true, remaining arguments in the dispinser are cleard to keep the dispenser valid for use. +func IfMatcherKeyword(c *caddy.Controller) bool { + if c.Val() == "if" || c.Val() == "if_op" { + // clear remainig args + c.RemainingArgs() + return true + } + return false } diff --git a/caddyhttp/httpserver/condition_test.go b/caddyhttp/httpserver/condition_test.go index 6fd42f092..e11cc11ad 100644 --- a/caddyhttp/httpserver/condition_test.go +++ b/caddyhttp/httpserver/condition_test.go @@ -243,7 +243,7 @@ func TestSetupIfMatcher(t *testing.T) { for i, test := range tests { c := caddy.NewTestController("http", test.input) c.Next() - matcher, err := SetupIfMatcher(c.Dispenser) + matcher, err := SetupIfMatcher(c) if err == nil && test.shouldErr { t.Errorf("Test %d didn't error, but it should have", i) } else if err != nil && !test.shouldErr { @@ -277,8 +277,11 @@ func TestIfMatcherKeyword(t *testing.T) { {"if_type", false}, {"if_cond", false}, } + for i, test := range tests { - valid := IfMatcherKeyword(test.keyword) + c := caddy.NewTestController("http", test.keyword) + c.Next() + valid := IfMatcherKeyword(c) if valid != test.expected { t.Errorf("Test %d: expected %v found %v", i, test.expected, valid) } diff --git a/caddyhttp/redirect/setup.go b/caddyhttp/redirect/setup.go index a05a7b0cb..0880ddcb1 100644 --- a/caddyhttp/redirect/setup.go +++ b/caddyhttp/redirect/setup.go @@ -63,7 +63,7 @@ func redirParse(c *caddy.Controller) ([]Rule, error) { } for c.Next() { - matcher, err := httpserver.SetupIfMatcher(c.Dispenser) + matcher, err := httpserver.SetupIfMatcher(c) if err != nil { return nil, err } @@ -71,8 +71,7 @@ func redirParse(c *caddy.Controller) ([]Rule, error) { var hadOptionalBlock bool for c.NextBlock() { - if httpserver.IfMatcherKeyword(c.Val()) { - c.RemainingArgs() + if httpserver.IfMatcherKeyword(c) { continue } diff --git a/caddyhttp/rewrite/setup.go b/caddyhttp/rewrite/setup.go index 4e369baa0..395f6ca39 100644 --- a/caddyhttp/rewrite/setup.go +++ b/caddyhttp/rewrite/setup.go @@ -57,12 +57,15 @@ func rewriteParse(c *caddy.Controller) ([]httpserver.HandlerConfig, error) { fallthrough case 0: // Integrate request matcher for 'if' conditions. - matcher, err = httpserver.SetupIfMatcher(c.Dispenser) + matcher, err = httpserver.SetupIfMatcher(c) if err != nil { return nil, err } - block: + for c.NextBlock() { + if httpserver.IfMatcherKeyword(c) { + continue + } switch c.Val() { case "r", "regexp": if !c.NextArg() { @@ -90,10 +93,6 @@ func rewriteParse(c *caddy.Controller) ([]httpserver.HandlerConfig, error) { return nil, c.Err("status must be 2xx or 4xx") } default: - if httpserver.IfMatcherKeyword(c.Val()) { - c.RemainingArgs() - continue block - } return nil, c.ArgErr() } }