reverseproxy: Fix Caddyfile support for replace_status (#4754)

This commit is contained in:
Francis Lavoie 2022-05-02 13:44:28 -04:00 committed by GitHub
parent af7321511c
commit 4a223f5203
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 19 deletions

View File

@ -1,8 +1,13 @@
:8884 :8884
reverse_proxy 127.0.0.1:65535 { reverse_proxy 127.0.0.1:65535 {
@changeStatus status 500 @500 status 500
replace_status @changeStatus 400 replace_status @500 400
@all status 2xx 3xx 4xx 5xx
replace_status @all {http.error.status_code}
replace_status {http.error.status_code}
@accel header X-Accel-Redirect * @accel header X-Accel-Redirect *
handle_response @accel { handle_response @accel {
@ -78,6 +83,17 @@ reverse_proxy 127.0.0.1:65535 {
}, },
"status_code": 400 "status_code": 400
}, },
{
"match": {
"status_code": [
2,
3,
4,
5
]
},
"status_code": "{http.error.status_code}"
},
{ {
"match": { "match": {
"headers": { "headers": {
@ -228,6 +244,9 @@ reverse_proxy 127.0.0.1:65535 {
} }
] ]
}, },
{
"status_code": "{http.error.status_code}"
},
{ {
"routes": [ "routes": [
{ {

View File

@ -627,22 +627,24 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
case "replace_status": case "replace_status":
args := d.RemainingArgs() args := d.RemainingArgs()
if len(args) != 2 { if len(args) != 1 && len(args) != 2 {
return d.Errf("must have two arguments: a response matcher and a status code") return d.Errf("must have one or two arguments: an optional response matcher, and a status code")
} }
if !strings.HasPrefix(args[0], matcherPrefix) { responseHandler := caddyhttp.ResponseHandler{}
return d.Errf("must use a named response matcher, starting with '@'")
}
foundMatcher, ok := h.responseMatchers[args[0]] if len(args) == 2 {
if !ok { if !strings.HasPrefix(args[0], matcherPrefix) {
return d.Errf("no named response matcher defined with name '%s'", args[0][1:]) return d.Errf("must use a named response matcher, starting with '@'")
} }
foundMatcher, ok := h.responseMatchers[args[0]]
_, err := strconv.Atoi(args[1]) if !ok {
if err != nil { return d.Errf("no named response matcher defined with name '%s'", args[0][1:])
return d.Errf("bad integer value '%s': %v", args[1], err) }
responseHandler.Match = &foundMatcher
responseHandler.StatusCode = caddyhttp.WeakString(args[1])
} else if len(args) == 1 {
responseHandler.StatusCode = caddyhttp.WeakString(args[0])
} }
// make sure there's no block, cause it doesn't make sense // make sure there's no block, cause it doesn't make sense
@ -652,10 +654,7 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
h.HandleResponse = append( h.HandleResponse = append(
h.HandleResponse, h.HandleResponse,
caddyhttp.ResponseHandler{ responseHandler,
Match: &foundMatcher,
StatusCode: caddyhttp.WeakString(args[1]),
},
) )
default: default: