summaryrefslogtreecommitdiff
path: root/caddyconfig
diff options
context:
space:
mode:
authorFrancis Lavoie <lavofr@gmail.com>2020-08-05 15:42:29 -0400
committerGitHub <noreply@github.com>2020-08-05 13:42:29 -0600
commit584eba94a4e06af9ce7efc91fb914f84d7a6a48c (patch)
treed01bd6c67e2bf9e8d895982a4b6f246b146b45bf /caddyconfig
parent904f149e5b2f0ae9eff09dacb4f1ec41c4a76298 (diff)
httpcaddyfile: Allow named matchers in `route` blocks (#3632)
Diffstat (limited to 'caddyconfig')
-rw-r--r--caddyconfig/httpcaddyfile/builtins.go45
-rw-r--r--caddyconfig/httpcaddyfile/directives.go14
2 files changed, 29 insertions, 30 deletions
diff --git a/caddyconfig/httpcaddyfile/builtins.go b/caddyconfig/httpcaddyfile/builtins.go
index 5dbb406..520796d 100644
--- a/caddyconfig/httpcaddyfile/builtins.go
+++ b/caddyconfig/httpcaddyfile/builtins.go
@@ -480,36 +480,23 @@ func parseRespond(h Helper) (caddyhttp.MiddlewareHandler, error) {
func parseRoute(h Helper) (caddyhttp.MiddlewareHandler, error) {
sr := new(caddyhttp.Subroute)
- for h.Next() {
- for nesting := h.Nesting(); h.NextBlock(nesting); {
- dir := h.Val()
-
- dirFunc, ok := registeredDirectives[dir]
- if !ok {
- return nil, h.Errf("unrecognized directive: %s", dir)
- }
-
- subHelper := h
- subHelper.Dispenser = h.NewFromNextSegment()
+ allResults, err := parseSegmentAsConfig(h)
+ if err != nil {
+ return nil, err
+ }
- results, err := dirFunc(subHelper)
- if err != nil {
- return nil, h.Errf("parsing caddyfile tokens for '%s': %v", dir, err)
- }
- for _, result := range results {
- switch handler := result.Value.(type) {
- case caddyhttp.Route:
- sr.Routes = append(sr.Routes, handler)
- case caddyhttp.Subroute:
- // directives which return a literal subroute instead of a route
- // means they intend to keep those handlers together without
- // them being reordered; we're doing that anyway since we're in
- // the route directive, so just append its handlers
- sr.Routes = append(sr.Routes, handler.Routes...)
- default:
- return nil, h.Errf("%s directive returned something other than an HTTP route or subroute: %#v (only handler directives can be used in routes)", dir, result.Value)
- }
- }
+ for _, result := range allResults {
+ switch handler := result.Value.(type) {
+ case caddyhttp.Route:
+ sr.Routes = append(sr.Routes, handler)
+ case caddyhttp.Subroute:
+ // directives which return a literal subroute instead of a route
+ // means they intend to keep those handlers together without
+ // them being reordered; we're doing that anyway since we're in
+ // the route directive, so just append its handlers
+ sr.Routes = append(sr.Routes, handler.Routes...)
+ default:
+ return nil, h.Errf("%s directive returned something other than an HTTP route or subroute: %#v (only handler directives can be used in routes)", result.directive, result.Value)
}
}
diff --git a/caddyconfig/httpcaddyfile/directives.go b/caddyconfig/httpcaddyfile/directives.go
index c31bdd3..e93fdd0 100644
--- a/caddyconfig/httpcaddyfile/directives.go
+++ b/caddyconfig/httpcaddyfile/directives.go
@@ -269,6 +269,18 @@ func (h Helper) NewBindAddresses(addrs []string) []ConfigValue {
// are themselves treated as directives, from which a subroute is built
// and returned.
func ParseSegmentAsSubroute(h Helper) (caddyhttp.MiddlewareHandler, error) {
+ allResults, err := parseSegmentAsConfig(h)
+ if err != nil {
+ return nil, err
+ }
+
+ return buildSubroute(allResults, h.groupCounter)
+}
+
+// parseSegmentAsConfig parses the segment such that its subdirectives
+// are themselves treated as directives, including named matcher definitions,
+// and the raw Config structs are returned.
+func parseSegmentAsConfig(h Helper) ([]ConfigValue, error) {
var allResults []ConfigValue
for h.Next() {
@@ -319,7 +331,7 @@ func ParseSegmentAsSubroute(h Helper) (caddyhttp.MiddlewareHandler, error) {
}
}
- return buildSubroute(allResults, h.groupCounter)
+ return allResults, nil
}
// ConfigValue represents a value to be added to the final