Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 06:45 UTC
httpcaddyfile: Fix `handle` grouping inside `route` (#5315)
Co-authored-by: Francis Lavoie <lavofr@gmail.com>
WeidiDeng committed
Jan 19, 2023 at 05:04 UTC
c38a040e85a7d277299acaab39bdad171bc45675
5 files changed
+93
-21
caddyconfig/httpcaddyfile/builtins.go
+3
-12
@@ -731,29 +731,20 @@ func parseError(h Helper) (caddyhttp.MiddlewareHandler, error) {
731
732
// parseRoute parses the route directive.
733
func parseRoute(h Helper) (caddyhttp.MiddlewareHandler, error) {
734
- sr := new(caddyhttp.Subroute)
735
-
734
allResults, err := parseSegmentAsConfig(h)
735
if err != nil {
736
return nil, err
737
}
738
739
for _, result := range allResults {
742
- switch handler := result.Value.(type) {
743
- case caddyhttp.Route:
744
- sr.Routes = append(sr.Routes, handler)
745
- case caddyhttp.Subroute:
746
- // directives which return a literal subroute instead of a route
747
- // means they intend to keep those handlers together without
748
- // them being reordered; we're doing that anyway since we're in
749
- // the route directive, so just append its handlers
750
- sr.Routes = append(sr.Routes, handler.Routes...)
740
+ switch result.Value.(type) {
741
+ case caddyhttp.Route, caddyhttp.Subroute:
742
default:
743
return nil, h.Errf("%s directive returned something other than an HTTP route or subroute: %#v (only handler directives can be used in routes)", result.directive, result.Value)
744
}
745
}
746
756
- return sr, nil
747
+ return buildSubroute(allResults, h.groupCounter, false)
748
}
749
750
func parseHandle(h Helper) (caddyhttp.MiddlewareHandler, error) {
caddyconfig/httpcaddyfile/directives.go
+1
-1
@@ -289,7 +289,7 @@ func ParseSegmentAsSubroute(h Helper) (caddyhttp.MiddlewareHandler, error) {
289
return nil, err
290
}
291
292
- return buildSubroute(allResults, h.groupCounter)
292
+ return buildSubroute(allResults, h.groupCounter, true)
293
}
294
295
// parseSegmentAsConfig parses the segment such that its subdirectives
caddyconfig/httpcaddyfile/httptype.go
+9
-7
@@ -618,7 +618,7 @@ func (st *ServerType) serversFromPairings(
618
619
// set up each handler directive, making sure to honor directive order
620
dirRoutes := sblock.pile["route"]
621
- siteSubroute, err := buildSubroute(dirRoutes, groupCounter)
621
+ siteSubroute, err := buildSubroute(dirRoutes, groupCounter, true)
622
if err != nil {
623
return nil, err
624
}
@@ -959,14 +959,16 @@ func appendSubrouteToRouteList(routeList caddyhttp.RouteList,
959
960
// buildSubroute turns the config values, which are expected to be routes
961
// into a clean and orderly subroute that has all the routes within it.
962
-func buildSubroute(routes []ConfigValue, groupCounter counter) (*caddyhttp.Subroute, error) {
963
- for _, val := range routes {
964
- if !directiveIsOrdered(val.directive) {
965
- return nil, fmt.Errorf("directive '%s' is not an ordered HTTP handler, so it cannot be used here", val.directive)
962
+func buildSubroute(routes []ConfigValue, groupCounter counter, needsSorting bool) (*caddyhttp.Subroute, error) {
963
+ if needsSorting {
964
+ for _, val := range routes {
965
+ if !directiveIsOrdered(val.directive) {
966
+ return nil, fmt.Errorf("directive '%s' is not an ordered HTTP handler, so it cannot be used here", val.directive)
967
+ }
968
}
967
- }
969
969
- sortRoutes(routes)
970
+ sortRoutes(routes)
971
+ }
972
973
subroute := new(caddyhttp.Subroute)
974
caddytest/integration/caddyfile_adapt/handle_nested_in_route.txt
new
+78
@@ -0,0 +1,78 @@
1
+:8881 {
2
+ route {
3
+ handle /foo/* {
4
+ respond "Foo"
5
+ }
6
+ handle {
7
+ respond "Bar"
8
+ }
9
+ }
10
+}
11
+----------
12
+{
13
+ "apps": {
14
+ "http": {
15
+ "servers": {
16
+ "srv0": {
17
+ "listen": [
18
+ ":8881"
19
+ ],
20
+ "routes": [
21
+ {
22
+ "handle": [
23
+ {
24
+ "handler": "subroute",
25
+ "routes": [
26
+ {
27
+ "group": "group2",
28
+ "handle": [
29
+ {
30
+ "handler": "subroute",
31
+ "routes": [
32
+ {
33
+ "handle": [
34
+ {
35
+ "body": "Foo",
36
+ "handler": "static_response"
37
+ }
38
+ ]
39
+ }
40
+ ]
41
+ }
42
+ ],
43
+ "match": [
44
+ {
45
+ "path": [
46
+ "/foo/*"
47
+ ]
48
+ }
49
+ ]
50
+ },
51
+ {
52
+ "group": "group2",
53
+ "handle": [
54
+ {
55
+ "handler": "subroute",
56
+ "routes": [
57
+ {
58
+ "handle": [
59
+ {
60
+ "body": "Bar",
61
+ "handler": "static_response"
62
+ }
63
+ ]
64
+ }
65
+ ]
66
+ }
67
+ ]
68
+ }
69
+ ]
70
+ }
71
+ ]
72
+ }
73
+ ]
74
+ }
75
+ }
76
+ }
77
+ }
78
+}
\ No newline at end of file
caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.txt
+2
-1
@@ -74,6 +74,7 @@ route {
74
]
75
},
76
{
77
+ "group": "group0",
78
"handle": [
79
{
80
"handler": "rewrite",
@@ -129,4 +130,4 @@ route {
130
}
131
}
132
}
132
-}
\ No newline at end of file
133
+}