diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2020-06-04 12:06:38 -0600 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2020-06-04 12:06:38 -0600 |
commit | 7a99835dab64f7864186185761bbf5194216f8b6 (patch) | |
tree | 90845df820a57ac032cf7156e45d8859ebcf30eb /modules/caddyhttp/reverseproxy | |
parent | 7b0962ba4df7e1dd7ab3f193fba394a4b899c431 (diff) |
reverseproxy: Enable changing only the status code (close #2920)
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)) |