From 538ddb85876001976fa0c76f10b912c0870fe6d7 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Wed, 27 May 2020 10:15:20 -0600 Subject: reverseproxy: Enable response interception (#1447, #2920) It's a raw, low-level implementation for now, but it's very flexible. More sugar-coating can be added after error handling is more developed. --- modules/caddyhttp/subroute.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'modules/caddyhttp/subroute.go') diff --git a/modules/caddyhttp/subroute.go b/modules/caddyhttp/subroute.go index 2e80d88..b1700f5 100644 --- a/modules/caddyhttp/subroute.go +++ b/modules/caddyhttp/subroute.go @@ -80,6 +80,36 @@ func (sr *Subroute) ServeHTTP(w http.ResponseWriter, r *http.Request, next Handl return err } +// ResponseHandler pairs a response matcher with a route list. +// It is useful for executing handler routes based on the +// properties of an HTTP response that has not been written +// out to the client yet. +// +// To use this type, provision it at module load time, then +// when ready to use, match the response against its matcher; +// if it matches (or doesn't have a matcher), invoke the routes +// by calling `rh.Routes.Compile(next).ServeHTTP(rw, req)` (or +// similar). +type ResponseHandler struct { + // The response matcher for this handler. If empty/nil, + // it always matches. + Match *ResponseMatcher `json:"match,omitempty"` + + // The list of HTTP routes to execute. + Routes RouteList `json:"routes,omitempty"` +} + +// Provision sets up the routse in rh. +func (rh *ResponseHandler) Provision(ctx caddy.Context) error { + if rh.Routes != nil { + err := rh.Routes.Provision(ctx) + if err != nil { + return err + } + } + return nil +} + // Interface guards var ( _ caddy.Provisioner = (*Subroute)(nil) -- cgit v1.2.3