diff options
Diffstat (limited to 'modules/caddyhttp/reverseproxy')
-rw-r--r-- | modules/caddyhttp/reverseproxy/reverseproxy.go | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/modules/caddyhttp/reverseproxy/reverseproxy.go b/modules/caddyhttp/reverseproxy/reverseproxy.go index 06802a0..7971348 100644 --- a/modules/caddyhttp/reverseproxy/reverseproxy.go +++ b/modules/caddyhttp/reverseproxy/reverseproxy.go @@ -24,6 +24,7 @@ import ( "net" "net/http" "regexp" + "strconv" "strings" "sync" "time" @@ -531,15 +532,32 @@ func (h *Handler) reverseProxy(rw http.ResponseWriter, req *http.Request, di Dia } } + // see if any response handler is configured for this response from the backend for i, rh := range h.HandleResponse { - if len(rh.Routes) == 0 { + if rh.Match != nil && !rh.Match.Match(res.StatusCode, res.Header) { continue } - if rh.Match != nil && !rh.Match.Match(res.StatusCode, res.Header) { + + repl := req.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer) + + // if configured to only change the status code, do that then continue regular proxy response + if statusCodeStr := rh.StatusCode.String(); statusCodeStr != "" { + statusCode, err := strconv.Atoi(repl.ReplaceAll(statusCodeStr, "")) + if err != nil { + return caddyhttp.Error(http.StatusInternalServerError, err) + } + if statusCode != 0 { + res.StatusCode = statusCode + } + break + } + + // otherwise, if there are any routes configured, execute those as the + // actual response instead of what we got from the proxy backend + if len(rh.Routes) == 0 { continue } res.Body.Close() - repl := req.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer) repl.Set("http.reverse_proxy.status_code", res.StatusCode) repl.Set("http.reverse_proxy.status_text", res.Status) h.logger.Debug("handling response", zap.Int("handler", i)) |