@marcel91200 / caddy Mirror · read-only / commits / e3324aa6
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, 05:15 UTC

httpcaddyfile: Ensure handle_path is sorted equally to handle (#3676)

* 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 <mholt@users.noreply.github.com> * Update caddyconfig/httpcaddyfile/httptype.go Co-authored-by: Matt Holt <mholt@users.noreply.github.com>

Francis Lavoie committed Sep 16, 2020 at 22:01 UTC e3324aa6de6c6f8f9fc14757025bb15de4801dde
2 files changed +126 -1
caddyconfig/httpcaddyfile/httptype.go
+21 -1
@@ -172,6 +172,15 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
172 if err != nil {
173 return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
174 }
175 +
176 + // As a special case, we want "handle_path" to be sorted
177 + // at the same level as "handle", so we force them to use
178 + // the same directive name after their parsing is complete.
179 + // See https://github.com/caddyserver/caddy/issues/3675#issuecomment-678042377
180 + if dir == "handle_path" {
181 + dir = "handle"
182 + }
183 +
184 for _, result := range results {
185 result.directive = dir
186 sb.pile[result.Class] = append(sb.pile[result.Class], result)
@@ -854,7 +863,18 @@ func buildSubroute(routes []ConfigValue, groupCounter counter) (*caddyhttp.Subro
863 // root directives would overwrite previously-matched ones; they should not cascade
864 "root": {},
865 }
857 - for meDir, info := range mutuallyExclusiveDirs {
866 +
867 + // we need to deterministically loop over each of these directives
868 + // in order to keep the group numbers consistent
869 + keys := make([]string, 0, len(mutuallyExclusiveDirs))
870 + for k := range mutuallyExclusiveDirs {
871 + keys = append(keys, k)
872 + }
873 + sort.Strings(keys)
874 +
875 + for _, meDir := range keys {
876 + info := mutuallyExclusiveDirs[meDir]
877 +
878 // see how many instances of the directive there are
879 for _, r := range routes {
880 if r.directive == meDir {
caddytest/integration/caddyfile_adapt/handle_path_sorting.txt new
+105
@@ -0,0 +1,105 @@
1 +:80 {
2 + handle /api/* {
3 + respond "api"
4 + }
5 +
6 + handle_path /static/* {
7 + respond "static"
8 + }
9 +
10 + handle {
11 + respond "handle"
12 + }
13 +}
14 +----------
15 +{
16 + "apps": {
17 + "http": {
18 + "servers": {
19 + "srv0": {
20 + "listen": [
21 + ":80"
22 + ],
23 + "routes": [
24 + {
25 + "group": "group3",
26 + "match": [
27 + {
28 + "path": [
29 + "/static/*"
30 + ]
31 + }
32 + ],
33 + "handle": [
34 + {
35 + "handler": "subroute",
36 + "routes": [
37 + {
38 + "handle": [
39 + {
40 + "handler": "rewrite",
41 + "strip_path_prefix": "/static"
42 + }
43 + ]
44 + },
45 + {
46 + "handle": [
47 + {
48 + "body": "static",
49 + "handler": "static_response"
50 + }
51 + ]
52 + }
53 + ]
54 + }
55 + ]
56 + },
57 + {
58 + "group": "group3",
59 + "match": [
60 + {
61 + "path": [
62 + "/api/*"
63 + ]
64 + }
65 + ],
66 + "handle": [
67 + {
68 + "handler": "subroute",
69 + "routes": [
70 + {
71 + "handle": [
72 + {
73 + "body": "api",
74 + "handler": "static_response"
75 + }
76 + ]
77 + }
78 + ]
79 + }
80 + ]
81 + },
82 + {
83 + "group": "group3",
84 + "handle": [
85 + {
86 + "handler": "subroute",
87 + "routes": [
88 + {
89 + "handle": [
90 + {
91 + "body": "handle",
92 + "handler": "static_response"
93 + }
94 + ]
95 + }
96 + ]
97 + }
98 + ]
99 + }
100 + ]
101 + }
102 + }
103 + }
104 + }
105 +}
\ No newline at end of file