Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 03:30 UTC
httpcaddyfile: keep handle_errors blocks in Caddyfile order (#8195)
Jay Hemnani committed
Oct 12, 2026 at 12:41 UTC
054c92b9fa0d5766ae223c00f40f9c91b6735e48
4 files changed
+44
-16
caddyconfig/httpcaddyfile/httptype.go
+1
-4
@@ -951,10 +951,7 @@ func (st *ServerType) serversFromPairings(
951
}
952
sort.SliceStable(errorSubrouteVals, func(i, j int) bool {
953
sri, srj := errorSubrouteVals[i].Value.(*caddyhttp.Subroute), errorSubrouteVals[j].Value.(*caddyhttp.Subroute)
954
- if len(sri.Routes[0].MatcherSetsRaw) == 0 && len(srj.Routes[0].MatcherSetsRaw) != 0 {
955
- return false
956
- }
957
- return true
954
+ return len(sri.Routes[0].MatcherSetsRaw) != 0 && len(srj.Routes[0].MatcherSetsRaw) == 0
955
})
956
errorsSubroute := &caddyhttp.Subroute{}
957
for _, val := range errorSubrouteVals {
caddytest/integration/caddyfile_adapt/error_multi_site_blocks.caddyfiletest
+8
-8
@@ -170,7 +170,7 @@ bar.localhost {
170
{
171
"handle": [
172
{
173
- "body": "404 or 410 error",
173
+ "body": "Error In range [500 .. 599]",
174
"handler": "static_response"
175
}
176
]
@@ -180,7 +180,7 @@ bar.localhost {
180
],
181
"match": [
182
{
183
- "expression": "{http.error.status_code} in [410]"
183
+ "expression": "{http.error.status_code} \u003e= 500 \u0026\u0026 {http.error.status_code} \u003c= 599"
184
}
185
]
186
},
@@ -192,7 +192,7 @@ bar.localhost {
192
{
193
"handle": [
194
{
195
- "body": "Error In range [500 .. 599]",
195
+ "body": "404 or 410 error",
196
"handler": "static_response"
197
}
198
]
@@ -202,7 +202,7 @@ bar.localhost {
202
],
203
"match": [
204
{
205
- "expression": "{http.error.status_code} \u003e= 500 \u0026\u0026 {http.error.status_code} \u003c= 599"
205
+ "expression": "{http.error.status_code} in [410]"
206
}
207
]
208
}
@@ -231,7 +231,7 @@ bar.localhost {
231
{
232
"handle": [
233
{
234
- "body": "404 or 410 error from second site",
234
+ "body": "Error In range [500 .. 599] from second site",
235
"handler": "static_response"
236
}
237
]
@@ -241,7 +241,7 @@ bar.localhost {
241
],
242
"match": [
243
{
244
- "expression": "{http.error.status_code} in [410]"
244
+ "expression": "{http.error.status_code} \u003e= 500 \u0026\u0026 {http.error.status_code} \u003c= 599"
245
}
246
]
247
},
@@ -253,7 +253,7 @@ bar.localhost {
253
{
254
"handle": [
255
{
256
- "body": "Error In range [500 .. 599] from second site",
256
+ "body": "404 or 410 error from second site",
257
"handler": "static_response"
258
}
259
]
@@ -263,7 +263,7 @@ bar.localhost {
263
],
264
"match": [
265
{
266
- "expression": "{http.error.status_code} \u003e= 500 \u0026\u0026 {http.error.status_code} \u003c= 599"
266
+ "expression": "{http.error.status_code} in [410]"
267
}
268
]
269
}
caddytest/integration/caddyfile_adapt/error_range_simple_codes.caddyfiletest
+4
-4
@@ -121,7 +121,7 @@ localhost:2099 {
121
{
122
"handle": [
123
{
124
- "body": "Error in the [400 .. 499] range",
124
+ "body": "Error code is equal to 500 or in the [300..399] range",
125
"handler": "static_response"
126
}
127
]
@@ -131,7 +131,7 @@ localhost:2099 {
131
],
132
"match": [
133
{
134
- "expression": "{http.error.status_code} \u003e= 400 \u0026\u0026 {http.error.status_code} \u003c= 499"
134
+ "expression": "{http.error.status_code} \u003e= 300 \u0026\u0026 {http.error.status_code} \u003c= 399 || {http.error.status_code} in [500]"
135
}
136
]
137
},
@@ -143,7 +143,7 @@ localhost:2099 {
143
{
144
"handle": [
145
{
146
- "body": "Error code is equal to 500 or in the [300..399] range",
146
+ "body": "Error in the [400 .. 499] range",
147
"handler": "static_response"
148
}
149
]
@@ -153,7 +153,7 @@ localhost:2099 {
153
],
154
"match": [
155
{
156
- "expression": "{http.error.status_code} \u003e= 300 \u0026\u0026 {http.error.status_code} \u003c= 399 || {http.error.status_code} in [500]"
156
+ "expression": "{http.error.status_code} \u003e= 400 \u0026\u0026 {http.error.status_code} \u003c= 499"
157
}
158
]
159
}
caddytest/integration/caddyfile_test.go
+31
@@ -782,6 +782,37 @@ func TestHandleErrorRangeAndCodes(t *testing.T) {
782
tester.AssertGetResponse("http://localhost:9080/private", 410, "Error in the [400 .. 499] range")
783
}
784
785
+func TestHandleErrorOverlappingCodesKeepOrder(t *testing.T) {
786
+ tester := caddytest.NewTester(t)
787
+ tester.InitServer(`{
788
+ admin localhost:2999
789
+ http_port 9080
790
+ }
791
+ localhost:9080 {
792
+ root * /srv
793
+ error /private* "Unauthorized" 410
794
+ error /hidden* "Not found" 404
795
+ error /internalerr* "Internal Server Error" 500
796
+
797
+ handle_errors 404 {
798
+ respond "Not found page"
799
+ }
800
+ handle_errors 4xx {
801
+ respond "Error in the [400 .. 499] range"
802
+ }
803
+ handle_errors {
804
+ respond "First fallback"
805
+ }
806
+ handle_errors {
807
+ respond "Second fallback"
808
+ }
809
+ }`, "caddyfile")
810
+ // act and assert
811
+ tester.AssertGetResponse("http://localhost:9080/hidden", 404, "Not found page")
812
+ tester.AssertGetResponse("http://localhost:9080/private", 410, "Error in the [400 .. 499] range")
813
+ tester.AssertGetResponse("http://localhost:9080/internalerr", 500, "First fallback")
814
+}
815
+
816
func TestHandleErrorSubHandlers(t *testing.T) {
817
tester := caddytest.NewTester(t)
818
tester.InitServer(`{