From 3e940513183e0603b37715012df26a0df45061bd Mon Sep 17 00:00:00 2001 From: armadi1809 Date: Sun, 10 Dec 2023 20:57:21 -0600 Subject: [PATCH] Included host matchers for error routes --- caddyconfig/httpcaddyfile/builtins.go | 4 +- caddyconfig/httpcaddyfile/httptype.go | 9 +- .../error_multi_site_blocks.txt | 245 ++++++++++++++++++ .../caddyfile_adapt/error_range_codes.txt | 9 +- .../error_range_simple_codes.txt | 31 +-- .../caddyfile_adapt/error_simple_codes.txt | 9 +- .../caddyfile_adapt/error_sort.txt | 28 +- 7 files changed, 289 insertions(+), 46 deletions(-) create mode 100644 caddytest/integration/caddyfile_adapt/error_multi_site_blocks.txt diff --git a/caddyconfig/httpcaddyfile/builtins.go b/caddyconfig/httpcaddyfile/builtins.go index 2f44862dcf92..334c913a4cf0 100644 --- a/caddyconfig/httpcaddyfile/builtins.go +++ b/caddyconfig/httpcaddyfile/builtins.go @@ -851,13 +851,13 @@ func parseHandleErrors(h Helper) ([]ConfigValue, error) { expression = "" codes := []string{} for _, val := range args { - if strings.HasSuffix(val, "xx") { + if len(val) == 3 && strings.HasSuffix(val, "xx") { val = val[:1] if expression != "" { expression += " || " } expression += fmt.Sprintf("{http.error.status_code} >= %s00 && {http.error.status_code} <= %s99", val, val) - } else { + } else if len(val) == 3 { codes = append(codes, val) } } diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go index fe7df194c0be..066df3014f74 100644 --- a/caddyconfig/httpcaddyfile/httptype.go +++ b/caddyconfig/httpcaddyfile/httptype.go @@ -781,15 +781,12 @@ func (st *ServerType) serversFromPairings( } return true }) + errorsSubroute := &caddyhttp.Subroute{} for _, val := range errorSubrouteVals { sr := val.Value.(*caddyhttp.Subroute) - routeMatcherSet := sr.Routes[0].MatcherSetsRaw - sr.Routes[0].MatcherSetsRaw = []caddy.ModuleMap{} - if routeMatcherSet == nil { - routeMatcherSet = matcherSetsEnc - } - srv.Errors.Routes = appendSubrouteToRouteList(srv.Errors.Routes, sr, routeMatcherSet, p, warnings) + errorsSubroute.Routes = append(errorsSubroute.Routes, sr.Routes...) } + srv.Errors.Routes = appendSubrouteToRouteList(srv.Errors.Routes, errorsSubroute, matcherSetsEnc, p, warnings) } // add log associations diff --git a/caddytest/integration/caddyfile_adapt/error_multi_site_blocks.txt b/caddytest/integration/caddyfile_adapt/error_multi_site_blocks.txt new file mode 100644 index 000000000000..0e84a13c2d36 --- /dev/null +++ b/caddytest/integration/caddyfile_adapt/error_multi_site_blocks.txt @@ -0,0 +1,245 @@ +foo.localhost { + root * /srv + error /private* "Unauthorized" 410 + error /fivehundred* "Internal Server Error" 500 + + handle_errors 5xx { + respond "Error In range [500 .. 599]" + } + handle_errors 410 { + respond "404 or 410 error" + } +} + +bar.localhost { + root * /srv + error /private* "Unauthorized" 410 + error /fivehundred* "Internal Server Error" 500 + + handle_errors 5xx { + respond "Error In range [500 .. 599] from second site" + } + handle_errors 410 { + respond "404 or 410 error from second site" + } +} +---------- +{ + "apps": { + "http": { + "servers": { + "srv0": { + "listen": [ + ":443" + ], + "routes": [ + { + "match": [ + { + "host": [ + "foo.localhost" + ] + } + ], + "handle": [ + { + "handler": "subroute", + "routes": [ + { + "handle": [ + { + "handler": "vars", + "root": "/srv" + } + ] + }, + { + "handle": [ + { + "error": "Internal Server Error", + "handler": "error", + "status_code": 500 + } + ], + "match": [ + { + "path": [ + "/fivehundred*" + ] + } + ] + }, + { + "handle": [ + { + "error": "Unauthorized", + "handler": "error", + "status_code": 410 + } + ], + "match": [ + { + "path": [ + "/private*" + ] + } + ] + } + ] + } + ], + "terminal": true + }, + { + "match": [ + { + "host": [ + "bar.localhost" + ] + } + ], + "handle": [ + { + "handler": "subroute", + "routes": [ + { + "handle": [ + { + "handler": "vars", + "root": "/srv" + } + ] + }, + { + "handle": [ + { + "error": "Internal Server Error", + "handler": "error", + "status_code": 500 + } + ], + "match": [ + { + "path": [ + "/fivehundred*" + ] + } + ] + }, + { + "handle": [ + { + "error": "Unauthorized", + "handler": "error", + "status_code": 410 + } + ], + "match": [ + { + "path": [ + "/private*" + ] + } + ] + } + ] + } + ], + "terminal": true + } + ], + "errors": { + "routes": [ + { + "match": [ + { + "host": [ + "foo.localhost" + ] + } + ], + "handle": [ + { + "handler": "subroute", + "routes": [ + { + "handle": [ + { + "body": "404 or 410 error", + "handler": "static_response" + } + ], + "match": [ + { + "expression": "{http.error.status_code} in [410]" + } + ] + }, + { + "handle": [ + { + "body": "Error In range [500 .. 599]", + "handler": "static_response" + } + ], + "match": [ + { + "expression": "{http.error.status_code} \u003e= 500 \u0026\u0026 {http.error.status_code} \u003c= 599" + } + ] + } + ] + } + ], + "terminal": true + }, + { + "match": [ + { + "host": [ + "bar.localhost" + ] + } + ], + "handle": [ + { + "handler": "subroute", + "routes": [ + { + "handle": [ + { + "body": "404 or 410 error from second site", + "handler": "static_response" + } + ], + "match": [ + { + "expression": "{http.error.status_code} in [410]" + } + ] + }, + { + "handle": [ + { + "body": "Error In range [500 .. 599] from second site", + "handler": "static_response" + } + ], + "match": [ + { + "expression": "{http.error.status_code} \u003e= 500 \u0026\u0026 {http.error.status_code} \u003c= 599" + } + ] + } + ] + } + ], + "terminal": true + } + ] + } + } + } + } + } +} diff --git a/caddytest/integration/caddyfile_adapt/error_range_codes.txt b/caddytest/integration/caddyfile_adapt/error_range_codes.txt index 447824d09426..46b70c8e3847 100644 --- a/caddytest/integration/caddyfile_adapt/error_range_codes.txt +++ b/caddytest/integration/caddyfile_adapt/error_range_codes.txt @@ -84,7 +84,9 @@ localhost:3010 { { "match": [ { - "expression": "{http.error.status_code} \u003e= 400 \u0026\u0026 {http.error.status_code} \u003c= 499" + "host": [ + "localhost" + ] } ], "handle": [ @@ -97,6 +99,11 @@ localhost:3010 { "body": "Error in the [400 .. 499] range", "handler": "static_response" } + ], + "match": [ + { + "expression": "{http.error.status_code} \u003e= 400 \u0026\u0026 {http.error.status_code} \u003c= 499" + } ] } ] diff --git a/caddytest/integration/caddyfile_adapt/error_range_simple_codes.txt b/caddytest/integration/caddyfile_adapt/error_range_simple_codes.txt index 4649671a9615..70158830c265 100644 --- a/caddytest/integration/caddyfile_adapt/error_range_simple_codes.txt +++ b/caddytest/integration/caddyfile_adapt/error_range_simple_codes.txt @@ -104,7 +104,9 @@ localhost:2099 { { "match": [ { - "expression": "{http.error.status_code} \u003e= 400 \u0026\u0026 {http.error.status_code} \u003c= 499" + "host": [ + "localhost" + ] } ], "handle": [ @@ -117,29 +119,24 @@ localhost:2099 { "body": "Error in the [400 .. 499] range", "handler": "static_response" } + ], + "match": [ + { + "expression": "{http.error.status_code} \u003e= 400 \u0026\u0026 {http.error.status_code} \u003c= 499" + } ] - } - ] - } - ], - "terminal": true - }, - { - "match": [ - { - "expression": "{http.error.status_code} \u003e= 300 \u0026\u0026 {http.error.status_code} \u003c= 399 || {http.error.status_code} in [500]" - } - ], - "handle": [ - { - "handler": "subroute", - "routes": [ + }, { "handle": [ { "body": "Error code is equal to 500 or in the [300..399] range", "handler": "static_response" } + ], + "match": [ + { + "expression": "{http.error.status_code} \u003e= 300 \u0026\u0026 {http.error.status_code} \u003c= 399 || {http.error.status_code} in [500]" + } ] } ] diff --git a/caddytest/integration/caddyfile_adapt/error_simple_codes.txt b/caddytest/integration/caddyfile_adapt/error_simple_codes.txt index dccc35a602e4..5ac5863e32d3 100644 --- a/caddytest/integration/caddyfile_adapt/error_simple_codes.txt +++ b/caddytest/integration/caddyfile_adapt/error_simple_codes.txt @@ -84,7 +84,9 @@ localhost:3010 { { "match": [ { - "expression": "{http.error.status_code} in [404, 410]" + "host": [ + "localhost" + ] } ], "handle": [ @@ -97,6 +99,11 @@ localhost:3010 { "body": "404 or 410 error", "handler": "static_response" } + ], + "match": [ + { + "expression": "{http.error.status_code} in [404, 410]" + } ] } ] diff --git a/caddytest/integration/caddyfile_adapt/error_sort.txt b/caddytest/integration/caddyfile_adapt/error_sort.txt index 578c8582802c..63701cccba90 100644 --- a/caddytest/integration/caddyfile_adapt/error_sort.txt +++ b/caddytest/integration/caddyfile_adapt/error_sort.txt @@ -104,7 +104,9 @@ localhost:2099 { { "match": [ { - "expression": "{http.error.status_code} \u003e= 400 \u0026\u0026 {http.error.status_code} \u003c= 499" + "host": [ + "localhost" + ] } ], "handle": [ @@ -117,25 +119,13 @@ localhost:2099 { "body": "Error in the [400 .. 499] range", "handler": "static_response" } + ], + "match": [ + { + "expression": "{http.error.status_code} \u003e= 400 \u0026\u0026 {http.error.status_code} \u003c= 499" + } ] - } - ] - } - ], - "terminal": true - }, - { - "match": [ - { - "host": [ - "localhost" - ] - } - ], - "handle": [ - { - "handler": "subroute", - "routes": [ + }, { "handle": [ {