Skip to content

Commit 38df5ae

Browse files
armadi1809francislavoie
authored andcommitted
Included host matchers for error routes
1 parent 953bbe4 commit 38df5ae

File tree

6 files changed

+287
-44
lines changed

6 files changed

+287
-44
lines changed

caddyconfig/httpcaddyfile/httptype.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -776,15 +776,12 @@ func (st *ServerType) serversFromPairings(
776776
}
777777
return true
778778
})
779+
errorsSubroute := &caddyhttp.Subroute{}
779780
for _, val := range errorSubrouteVals {
780781
sr := val.Value.(*caddyhttp.Subroute)
781-
routeMatcherSet := sr.Routes[0].MatcherSetsRaw
782-
sr.Routes[0].MatcherSetsRaw = []caddy.ModuleMap{}
783-
if routeMatcherSet == nil {
784-
routeMatcherSet = matcherSetsEnc
785-
}
786-
srv.Errors.Routes = appendSubrouteToRouteList(srv.Errors.Routes, sr, routeMatcherSet, p, warnings)
782+
errorsSubroute.Routes = append(errorsSubroute.Routes, sr.Routes...)
787783
}
784+
srv.Errors.Routes = appendSubrouteToRouteList(srv.Errors.Routes, errorsSubroute, matcherSetsEnc, p, warnings)
788785
}
789786

790787
// add log associations
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
foo.localhost {
2+
root * /srv
3+
error /private* "Unauthorized" 410
4+
error /fivehundred* "Internal Server Error" 500
5+
6+
handle_errors 5xx {
7+
respond "Error In range [500 .. 599]"
8+
}
9+
handle_errors 410 {
10+
respond "404 or 410 error"
11+
}
12+
}
13+
14+
bar.localhost {
15+
root * /srv
16+
error /private* "Unauthorized" 410
17+
error /fivehundred* "Internal Server Error" 500
18+
19+
handle_errors 5xx {
20+
respond "Error In range [500 .. 599] from second site"
21+
}
22+
handle_errors 410 {
23+
respond "404 or 410 error from second site"
24+
}
25+
}
26+
----------
27+
{
28+
"apps": {
29+
"http": {
30+
"servers": {
31+
"srv0": {
32+
"listen": [
33+
":443"
34+
],
35+
"routes": [
36+
{
37+
"match": [
38+
{
39+
"host": [
40+
"foo.localhost"
41+
]
42+
}
43+
],
44+
"handle": [
45+
{
46+
"handler": "subroute",
47+
"routes": [
48+
{
49+
"handle": [
50+
{
51+
"handler": "vars",
52+
"root": "/srv"
53+
}
54+
]
55+
},
56+
{
57+
"handle": [
58+
{
59+
"error": "Internal Server Error",
60+
"handler": "error",
61+
"status_code": 500
62+
}
63+
],
64+
"match": [
65+
{
66+
"path": [
67+
"/fivehundred*"
68+
]
69+
}
70+
]
71+
},
72+
{
73+
"handle": [
74+
{
75+
"error": "Unauthorized",
76+
"handler": "error",
77+
"status_code": 410
78+
}
79+
],
80+
"match": [
81+
{
82+
"path": [
83+
"/private*"
84+
]
85+
}
86+
]
87+
}
88+
]
89+
}
90+
],
91+
"terminal": true
92+
},
93+
{
94+
"match": [
95+
{
96+
"host": [
97+
"bar.localhost"
98+
]
99+
}
100+
],
101+
"handle": [
102+
{
103+
"handler": "subroute",
104+
"routes": [
105+
{
106+
"handle": [
107+
{
108+
"handler": "vars",
109+
"root": "/srv"
110+
}
111+
]
112+
},
113+
{
114+
"handle": [
115+
{
116+
"error": "Internal Server Error",
117+
"handler": "error",
118+
"status_code": 500
119+
}
120+
],
121+
"match": [
122+
{
123+
"path": [
124+
"/fivehundred*"
125+
]
126+
}
127+
]
128+
},
129+
{
130+
"handle": [
131+
{
132+
"error": "Unauthorized",
133+
"handler": "error",
134+
"status_code": 410
135+
}
136+
],
137+
"match": [
138+
{
139+
"path": [
140+
"/private*"
141+
]
142+
}
143+
]
144+
}
145+
]
146+
}
147+
],
148+
"terminal": true
149+
}
150+
],
151+
"errors": {
152+
"routes": [
153+
{
154+
"match": [
155+
{
156+
"host": [
157+
"foo.localhost"
158+
]
159+
}
160+
],
161+
"handle": [
162+
{
163+
"handler": "subroute",
164+
"routes": [
165+
{
166+
"handle": [
167+
{
168+
"body": "404 or 410 error",
169+
"handler": "static_response"
170+
}
171+
],
172+
"match": [
173+
{
174+
"expression": "{http.error.status_code} in [410]"
175+
}
176+
]
177+
},
178+
{
179+
"handle": [
180+
{
181+
"body": "Error In range [500 .. 599]",
182+
"handler": "static_response"
183+
}
184+
],
185+
"match": [
186+
{
187+
"expression": "{http.error.status_code} \u003e= 500 \u0026\u0026 {http.error.status_code} \u003c= 599"
188+
}
189+
]
190+
}
191+
]
192+
}
193+
],
194+
"terminal": true
195+
},
196+
{
197+
"match": [
198+
{
199+
"host": [
200+
"bar.localhost"
201+
]
202+
}
203+
],
204+
"handle": [
205+
{
206+
"handler": "subroute",
207+
"routes": [
208+
{
209+
"handle": [
210+
{
211+
"body": "404 or 410 error from second site",
212+
"handler": "static_response"
213+
}
214+
],
215+
"match": [
216+
{
217+
"expression": "{http.error.status_code} in [410]"
218+
}
219+
]
220+
},
221+
{
222+
"handle": [
223+
{
224+
"body": "Error In range [500 .. 599] from second site",
225+
"handler": "static_response"
226+
}
227+
],
228+
"match": [
229+
{
230+
"expression": "{http.error.status_code} \u003e= 500 \u0026\u0026 {http.error.status_code} \u003c= 599"
231+
}
232+
]
233+
}
234+
]
235+
}
236+
],
237+
"terminal": true
238+
}
239+
]
240+
}
241+
}
242+
}
243+
}
244+
}
245+
}

caddytest/integration/caddyfile_adapt/error_range_codes.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ localhost:3010 {
8484
{
8585
"match": [
8686
{
87-
"expression": "{http.error.status_code} \u003e= 400 \u0026\u0026 {http.error.status_code} \u003c= 499"
87+
"host": [
88+
"localhost"
89+
]
8890
}
8991
],
9092
"handle": [
@@ -97,6 +99,11 @@ localhost:3010 {
9799
"body": "Error in the [400 .. 499] range",
98100
"handler": "static_response"
99101
}
102+
],
103+
"match": [
104+
{
105+
"expression": "{http.error.status_code} \u003e= 400 \u0026\u0026 {http.error.status_code} \u003c= 499"
106+
}
100107
]
101108
}
102109
]

caddytest/integration/caddyfile_adapt/error_range_simple_codes.txt

+14-17
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ localhost:2099 {
104104
{
105105
"match": [
106106
{
107-
"expression": "{http.error.status_code} \u003e= 400 \u0026\u0026 {http.error.status_code} \u003c= 499"
107+
"host": [
108+
"localhost"
109+
]
108110
}
109111
],
110112
"handle": [
@@ -117,29 +119,24 @@ localhost:2099 {
117119
"body": "Error in the [400 .. 499] range",
118120
"handler": "static_response"
119121
}
122+
],
123+
"match": [
124+
{
125+
"expression": "{http.error.status_code} \u003e= 400 \u0026\u0026 {http.error.status_code} \u003c= 499"
126+
}
120127
]
121-
}
122-
]
123-
}
124-
],
125-
"terminal": true
126-
},
127-
{
128-
"match": [
129-
{
130-
"expression": "{http.error.status_code} \u003e= 300 \u0026\u0026 {http.error.status_code} \u003c= 399 || {http.error.status_code} in [500]"
131-
}
132-
],
133-
"handle": [
134-
{
135-
"handler": "subroute",
136-
"routes": [
128+
},
137129
{
138130
"handle": [
139131
{
140132
"body": "Error code is equal to 500 or in the [300..399] range",
141133
"handler": "static_response"
142134
}
135+
],
136+
"match": [
137+
{
138+
"expression": "{http.error.status_code} \u003e= 300 \u0026\u0026 {http.error.status_code} \u003c= 399 || {http.error.status_code} in [500]"
139+
}
143140
]
144141
}
145142
]

caddytest/integration/caddyfile_adapt/error_simple_codes.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ localhost:3010 {
8484
{
8585
"match": [
8686
{
87-
"expression": "{http.error.status_code} in [404, 410]"
87+
"host": [
88+
"localhost"
89+
]
8890
}
8991
],
9092
"handle": [
@@ -97,6 +99,11 @@ localhost:3010 {
9799
"body": "404 or 410 error",
98100
"handler": "static_response"
99101
}
102+
],
103+
"match": [
104+
{
105+
"expression": "{http.error.status_code} in [404, 410]"
106+
}
100107
]
101108
}
102109
]

0 commit comments

Comments
 (0)