Skip to content

Commit 81ee34e

Browse files
httpcaddyfile: Fix sorting edgecase for nested handle_path (#4477)
1 parent 78b5356 commit 81ee34e

File tree

3 files changed

+150
-7
lines changed

3 files changed

+150
-7
lines changed

caddyconfig/httpcaddyfile/directives.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ func parseSegmentAsConfig(h Helper) ([]ConfigValue, error) {
340340
if err != nil {
341341
return nil, h.Errf("parsing caddyfile tokens for '%s': %v", dir, err)
342342
}
343+
344+
dir = normalizeDirectiveName(dir)
345+
343346
for _, result := range results {
344347
result.directive = dir
345348
allResults = append(allResults, result)

caddyconfig/httpcaddyfile/httptype.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,7 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
193193
return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
194194
}
195195

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

204198
for _, result := range results {
205199
result.directive = dir
@@ -1061,6 +1055,19 @@ func buildSubroute(routes []ConfigValue, groupCounter counter) (*caddyhttp.Subro
10611055
return subroute, nil
10621056
}
10631057

1058+
// normalizeDirectiveName ensures directives that should be sorted
1059+
// at the same level are named the same before sorting happens.
1060+
func normalizeDirectiveName(directive string) string {
1061+
// As a special case, we want "handle_path" to be sorted
1062+
// at the same level as "handle", so we force them to use
1063+
// the same directive name after their parsing is complete.
1064+
// See https://github.com/caddyserver/caddy/issues/3675#issuecomment-678042377
1065+
if directive == "handle_path" {
1066+
directive = "handle"
1067+
}
1068+
return directive
1069+
}
1070+
10641071
// consolidateRoutes combines routes with the same properties
10651072
// (same matchers, same Terminal and Group settings) for a
10661073
// cleaner overall output.
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
*.example.com {
2+
@foo host foo.example.com
3+
handle @foo {
4+
handle_path /strip* {
5+
respond "this should be first"
6+
}
7+
handle {
8+
respond "this should be second"
9+
}
10+
}
11+
handle {
12+
respond "this should be last"
13+
}
14+
}
15+
----------
16+
{
17+
"apps": {
18+
"http": {
19+
"servers": {
20+
"srv0": {
21+
"listen": [
22+
":443"
23+
],
24+
"routes": [
25+
{
26+
"match": [
27+
{
28+
"host": [
29+
"*.example.com"
30+
]
31+
}
32+
],
33+
"handle": [
34+
{
35+
"handler": "subroute",
36+
"routes": [
37+
{
38+
"group": "group5",
39+
"handle": [
40+
{
41+
"handler": "subroute",
42+
"routes": [
43+
{
44+
"group": "group2",
45+
"handle": [
46+
{
47+
"handler": "subroute",
48+
"routes": [
49+
{
50+
"handle": [
51+
{
52+
"handler": "rewrite",
53+
"strip_path_prefix": "/strip"
54+
}
55+
]
56+
},
57+
{
58+
"handle": [
59+
{
60+
"body": "this should be first",
61+
"handler": "static_response"
62+
}
63+
]
64+
}
65+
]
66+
}
67+
],
68+
"match": [
69+
{
70+
"path": [
71+
"/strip*"
72+
]
73+
}
74+
]
75+
},
76+
{
77+
"group": "group2",
78+
"handle": [
79+
{
80+
"handler": "subroute",
81+
"routes": [
82+
{
83+
"handle": [
84+
{
85+
"body": "this should be second",
86+
"handler": "static_response"
87+
}
88+
]
89+
}
90+
]
91+
}
92+
]
93+
}
94+
]
95+
}
96+
],
97+
"match": [
98+
{
99+
"host": [
100+
"foo.example.com"
101+
]
102+
}
103+
]
104+
},
105+
{
106+
"group": "group5",
107+
"handle": [
108+
{
109+
"handler": "subroute",
110+
"routes": [
111+
{
112+
"handle": [
113+
{
114+
"body": "this should be last",
115+
"handler": "static_response"
116+
}
117+
]
118+
}
119+
]
120+
}
121+
]
122+
}
123+
]
124+
}
125+
],
126+
"terminal": true
127+
}
128+
]
129+
}
130+
}
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)