templates: Access to proxy response (closes #4420)

Also sorted the func map just for kicks.
This commit is contained in:
Matthew Holt
2022-07-07 13:17:23 -06:00
parent f259ed52bb
commit 577f8ba811
2 changed files with 45 additions and 8 deletions
@@ -1296,6 +1296,18 @@ var bufPool = sync.Pool{
},
}
// Response returns the reverse proxy's HTTP response attached to the
// request. It can only be used inside handle_response routes.
// EXPERIMENTAL: This API is subject to change or removal.
func Response(req *http.Request) (*http.Response, error) {
hrc, ok := req.Context().Value(proxyHandleResponseContextCtxKey).(*handleResponseContext)
if !ok {
return nil, caddyhttp.Error(http.StatusInternalServerError,
fmt.Errorf("cannot access backend response outside of reverse_proxy's handle_response route"))
}
return hrc.response, nil
}
// handleResponseContext carries some contextual information about the
// the current proxy handling.
type handleResponseContext struct {