httpcaddyfile: Fix handle grouping inside route (#5315)

Co-authored-by: Francis Lavoie <lavofr@gmail.com>
This commit is contained in:
WeidiDeng 2023-01-19 05:04:41 +08:00 committed by GitHub
parent e8ad9b32c9
commit c38a040e85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 93 additions and 21 deletions

View file

@ -731,29 +731,20 @@ func parseError(h Helper) (caddyhttp.MiddlewareHandler, error) {
// parseRoute parses the route directive. // parseRoute parses the route directive.
func parseRoute(h Helper) (caddyhttp.MiddlewareHandler, error) { func parseRoute(h Helper) (caddyhttp.MiddlewareHandler, error) {
sr := new(caddyhttp.Subroute)
allResults, err := parseSegmentAsConfig(h) allResults, err := parseSegmentAsConfig(h)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, result := range allResults { for _, result := range allResults {
switch handler := result.Value.(type) { switch result.Value.(type) {
case caddyhttp.Route: case caddyhttp.Route, caddyhttp.Subroute:
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: 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) 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)
} }
} }
return sr, nil return buildSubroute(allResults, h.groupCounter, false)
} }
func parseHandle(h Helper) (caddyhttp.MiddlewareHandler, error) { func parseHandle(h Helper) (caddyhttp.MiddlewareHandler, error) {

View file

@ -289,7 +289,7 @@ func ParseSegmentAsSubroute(h Helper) (caddyhttp.MiddlewareHandler, error) {
return nil, err return nil, err
} }
return buildSubroute(allResults, h.groupCounter) return buildSubroute(allResults, h.groupCounter, true)
} }
// parseSegmentAsConfig parses the segment such that its subdirectives // parseSegmentAsConfig parses the segment such that its subdirectives

View file

@ -618,7 +618,7 @@ func (st *ServerType) serversFromPairings(
// set up each handler directive, making sure to honor directive order // set up each handler directive, making sure to honor directive order
dirRoutes := sblock.pile["route"] dirRoutes := sblock.pile["route"]
siteSubroute, err := buildSubroute(dirRoutes, groupCounter) siteSubroute, err := buildSubroute(dirRoutes, groupCounter, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -959,14 +959,16 @@ func appendSubrouteToRouteList(routeList caddyhttp.RouteList,
// buildSubroute turns the config values, which are expected to be routes // buildSubroute turns the config values, which are expected to be routes
// into a clean and orderly subroute that has all the routes within it. // into a clean and orderly subroute that has all the routes within it.
func buildSubroute(routes []ConfigValue, groupCounter counter) (*caddyhttp.Subroute, error) { func buildSubroute(routes []ConfigValue, groupCounter counter, needsSorting bool) (*caddyhttp.Subroute, error) {
for _, val := range routes { if needsSorting {
if !directiveIsOrdered(val.directive) { for _, val := range routes {
return nil, fmt.Errorf("directive '%s' is not an ordered HTTP handler, so it cannot be used here", val.directive) if !directiveIsOrdered(val.directive) {
return nil, fmt.Errorf("directive '%s' is not an ordered HTTP handler, so it cannot be used here", val.directive)
}
} }
}
sortRoutes(routes) sortRoutes(routes)
}
subroute := new(caddyhttp.Subroute) subroute := new(caddyhttp.Subroute)

View file

@ -0,0 +1,78 @@
:8881 {
route {
handle /foo/* {
respond "Foo"
}
handle {
respond "Bar"
}
}
}
----------
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":8881"
],
"routes": [
{
"handle": [
{
"handler": "subroute",
"routes": [
{
"group": "group2",
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"body": "Foo",
"handler": "static_response"
}
]
}
]
}
],
"match": [
{
"path": [
"/foo/*"
]
}
]
},
{
"group": "group2",
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"body": "Bar",
"handler": "static_response"
}
]
}
]
}
]
}
]
}
]
}
]
}
}
}
}
}

View file

@ -74,6 +74,7 @@ route {
] ]
}, },
{ {
"group": "group0",
"handle": [ "handle": [
{ {
"handler": "rewrite", "handler": "rewrite",