Skip to content

Commit

Permalink
Included host matchers for error routes
Browse files Browse the repository at this point in the history
  • Loading branch information
armadi1809 committed Jan 16, 2024
1 parent 4f0b4ea commit 3e94051
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 46 deletions.
4 changes: 2 additions & 2 deletions caddyconfig/httpcaddyfile/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
9 changes: 3 additions & 6 deletions caddyconfig/httpcaddyfile/httptype.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
245 changes: 245 additions & 0 deletions caddytest/integration/caddyfile_adapt/error_multi_site_blocks.txt
Original file line number Diff line number Diff line change
@@ -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
}
]
}
}
}
}
}
}
9 changes: 8 additions & 1 deletion caddytest/integration/caddyfile_adapt/error_range_codes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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"
}
]
}
]
Expand Down
31 changes: 14 additions & 17 deletions caddytest/integration/caddyfile_adapt/error_range_simple_codes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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]"
}
]
}
]
Expand Down
9 changes: 8 additions & 1 deletion caddytest/integration/caddyfile_adapt/error_simple_codes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ localhost:3010 {
{
"match": [
{
"expression": "{http.error.status_code} in [404, 410]"
"host": [
"localhost"
]
}
],
"handle": [
Expand All @@ -97,6 +99,11 @@ localhost:3010 {
"body": "404 or 410 error",
"handler": "static_response"
}
],
"match": [
{
"expression": "{http.error.status_code} in [404, 410]"
}
]
}
]
Expand Down
Loading

0 comments on commit 3e94051

Please sign in to comment.