mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-03 23:09:57 -05:00
httpcaddyfile: Group try_files routes together (#2891)
This ensures that only the first matching route is used.
This commit is contained in:
parent
a66f461201
commit
2466ed1484
3 changed files with 44 additions and 9 deletions
|
@ -16,6 +16,7 @@ package httpcaddyfile
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/caddyserver/caddy/v2"
|
"github.com/caddyserver/caddy/v2"
|
||||||
|
@ -93,10 +94,11 @@ func RegisterHandlerDirective(dir string, setupFunc UnmarshalHandlerFunc) {
|
||||||
// Caddyfile tokens.
|
// Caddyfile tokens.
|
||||||
type Helper struct {
|
type Helper struct {
|
||||||
*caddyfile.Dispenser
|
*caddyfile.Dispenser
|
||||||
options map[string]interface{}
|
options map[string]interface{}
|
||||||
warnings *[]caddyconfig.Warning
|
warnings *[]caddyconfig.Warning
|
||||||
matcherDefs map[string]caddy.ModuleMap
|
matcherDefs map[string]caddy.ModuleMap
|
||||||
parentBlock caddyfile.ServerBlock
|
parentBlock caddyfile.ServerBlock
|
||||||
|
groupCounter *int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Option gets the option keyed by name.
|
// Option gets the option keyed by name.
|
||||||
|
@ -166,6 +168,32 @@ func (h Helper) NewRoute(matcherSet caddy.ModuleMap,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h Helper) GroupRoutes(vals []ConfigValue) {
|
||||||
|
// ensure there's at least two routes; group of one is pointless
|
||||||
|
var count int
|
||||||
|
for _, v := range vals {
|
||||||
|
if _, ok := v.Value.(caddyhttp.Route); ok {
|
||||||
|
count++
|
||||||
|
if count > 1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if count < 2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// now that we know the group will have some effect, do it
|
||||||
|
groupNum := *h.groupCounter
|
||||||
|
for i := 0; i < len(vals); i++ {
|
||||||
|
if route, ok := vals[i].Value.(caddyhttp.Route); ok {
|
||||||
|
route.Group = fmt.Sprintf("group%d", groupNum)
|
||||||
|
vals[i].Value = route
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*h.groupCounter++
|
||||||
|
}
|
||||||
|
|
||||||
// NewBindAddresses returns config values relevant to adding
|
// NewBindAddresses returns config values relevant to adding
|
||||||
// listener bind addresses to the config.
|
// listener bind addresses to the config.
|
||||||
func (h Helper) NewBindAddresses(addrs []string) []ConfigValue {
|
func (h Helper) NewBindAddresses(addrs []string) []ConfigValue {
|
||||||
|
|
|
@ -41,6 +41,7 @@ type ServerType struct {
|
||||||
func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
|
func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
|
||||||
options map[string]interface{}) (*caddy.Config, []caddyconfig.Warning, error) {
|
options map[string]interface{}) (*caddy.Config, []caddyconfig.Warning, error) {
|
||||||
var warnings []caddyconfig.Warning
|
var warnings []caddyconfig.Warning
|
||||||
|
groupCounter := new(int)
|
||||||
|
|
||||||
var serverBlocks []serverBlock
|
var serverBlocks []serverBlock
|
||||||
for _, sblock := range originalServerBlocks {
|
for _, sblock := range originalServerBlocks {
|
||||||
|
@ -140,11 +141,12 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
|
||||||
}
|
}
|
||||||
|
|
||||||
results, err := dirFunc(Helper{
|
results, err := dirFunc(Helper{
|
||||||
Dispenser: caddyfile.NewDispenser(segment),
|
Dispenser: caddyfile.NewDispenser(segment),
|
||||||
options: options,
|
options: options,
|
||||||
warnings: &warnings,
|
warnings: &warnings,
|
||||||
matcherDefs: matcherDefs,
|
matcherDefs: matcherDefs,
|
||||||
parentBlock: sb.block,
|
parentBlock: sb.block,
|
||||||
|
groupCounter: groupCounter,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
|
return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
|
||||||
|
|
|
@ -163,5 +163,10 @@ func parseTryFiles(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
|
||||||
result = append(result, makeRoute(try, "")...)
|
result = append(result, makeRoute(try, "")...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ensure that multiple routes (possible if rewrite targets
|
||||||
|
// have query strings, for example) are grouped together
|
||||||
|
// so only the first matching rewrite is performed (#2891)
|
||||||
|
h.GroupRoutes(result)
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue