Skip to content

Commit

Permalink
httpcaddyfile: Ensure handle_path is sorted equally to handle (#3676)
Browse files Browse the repository at this point in the history
* httpcaddyfile: Ensure handle_path is sorted as equal to handle

* httpcaddyfile: Make mutual exclusivity grouping deterministic (I hope)

* httpcaddyfile: Add comment linking to the issue being fixed

* httpcaddyfile: Typo fix, comment clarity

Co-authored-by: Matt Holt <[email protected]>

* Update caddyconfig/httpcaddyfile/httptype.go

Co-authored-by: Matt Holt <[email protected]>
  • Loading branch information
francislavoie and mholt authored Sep 17, 2020
1 parent d55d50b commit e3324aa
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
22 changes: 21 additions & 1 deletion caddyconfig/httpcaddyfile/httptype.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
if err != nil {
return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
}

// As a special case, we want "handle_path" to be sorted
// at the same level as "handle", so we force them to use
// the same directive name after their parsing is complete.
// See https://github.com/caddyserver/caddy/issues/3675#issuecomment-678042377
if dir == "handle_path" {
dir = "handle"
}

for _, result := range results {
result.directive = dir
sb.pile[result.Class] = append(sb.pile[result.Class], result)
Expand Down Expand Up @@ -854,7 +863,18 @@ func buildSubroute(routes []ConfigValue, groupCounter counter) (*caddyhttp.Subro
// root directives would overwrite previously-matched ones; they should not cascade
"root": {},
}
for meDir, info := range mutuallyExclusiveDirs {

// we need to deterministically loop over each of these directives
// in order to keep the group numbers consistent
keys := make([]string, 0, len(mutuallyExclusiveDirs))
for k := range mutuallyExclusiveDirs {
keys = append(keys, k)
}
sort.Strings(keys)

for _, meDir := range keys {
info := mutuallyExclusiveDirs[meDir]

// see how many instances of the directive there are
for _, r := range routes {
if r.directive == meDir {
Expand Down
105 changes: 105 additions & 0 deletions caddytest/integration/caddyfile_adapt/handle_path_sorting.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
:80 {
handle /api/* {
respond "api"
}

handle_path /static/* {
respond "static"
}

handle {
respond "handle"
}
}
----------
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":80"
],
"routes": [
{
"group": "group3",
"match": [
{
"path": [
"/static/*"
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "rewrite",
"strip_path_prefix": "/static"
}
]
},
{
"handle": [
{
"body": "static",
"handler": "static_response"
}
]
}
]
}
]
},
{
"group": "group3",
"match": [
{
"path": [
"/api/*"
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"body": "api",
"handler": "static_response"
}
]
}
]
}
]
},
{
"group": "group3",
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"body": "handle",
"handler": "static_response"
}
]
}
]
}
]
}
]
}
}
}
}
}

0 comments on commit e3324aa

Please sign in to comment.