@marcel91200 / caddy Mirror · read-only / commits / 81ee34e9
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 00:15 UTC

httpcaddyfile: Fix sorting edgecase for nested `handle_path` (#4477)

Francis Lavoie committed Dec 13, 2021 at 13:42 UTC 81ee34e9623c3ac630f46c81a26e7823d0b2bf7b
3 files changed +150 -7
caddyconfig/httpcaddyfile/directives.go
+3
@@ -340,6 +340,9 @@ func parseSegmentAsConfig(h Helper) ([]ConfigValue, error) {
340 if err != nil {
341 return nil, h.Errf("parsing caddyfile tokens for '%s': %v", dir, err)
342 }
343 +
344 + dir = normalizeDirectiveName(dir)
345 +
346 for _, result := range results {
347 result.directive = dir
348 allResults = append(allResults, result)
caddyconfig/httpcaddyfile/httptype.go
+14 -7
@@ -193,13 +193,7 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
193 return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
194 }
195
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)
197
198 for _, result := range results {
199 result.directive = dir
@@ -1061,6 +1055,19 @@ func buildSubroute(routes []ConfigValue, groupCounter counter) (*caddyhttp.Subro
1055 return subroute, nil
1056 }
1057
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 +
1071 // consolidateRoutes combines routes with the same properties
1072 // (same matchers, same Terminal and Group settings) for a
1073 // cleaner overall output.
caddytest/integration/caddyfile_adapt/sort_directives_within_handle.txt new
+133
@@ -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 +}
\ No newline at end of file