diff options
author | Matthew Holt <[email protected]> | 2019-05-28 18:53:08 -0600 |
---|---|---|
committer | Matthew Holt <[email protected]> | 2019-05-28 18:53:08 -0600 |
commit | bf54615efcaa98ab2e5c83e19a0e7b57897ca2c8 (patch) | |
tree | 7ab4296eb6ffd67622516e393fc50566f585ab2a /modules/caddyhttp/headers | |
parent | da6a8cfc86620300734405d69c527856c0e71d66 (diff) | |
download | caddy-bf54615efcaa98ab2e5c83e19a0e7b57897ca2c8.tar.gz caddy-bf54615efcaa98ab2e5c83e19a0e7b57897ca2c8.zip |
ResponseMatcher for conditional logic of response headers
Diffstat (limited to 'modules/caddyhttp/headers')
-rw-r--r-- | modules/caddyhttp/headers/headers.go | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/modules/caddyhttp/headers/headers.go b/modules/caddyhttp/headers/headers.go index 4cab5b5cb..b07a588a6 100644 --- a/modules/caddyhttp/headers/headers.go +++ b/modules/caddyhttp/headers/headers.go @@ -33,16 +33,18 @@ type HeaderOps struct { // optionally deferred until response time. type RespHeaderOps struct { *HeaderOps - Deferred bool `json:"deferred"` + Require *caddyhttp.ResponseMatcher `json:"require,omitempty"` + Deferred bool `json:"deferred,omitempty"` } func (h Headers) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { repl := r.Context().Value(caddy2.ReplacerCtxKey).(caddy2.Replacer) apply(h.Request, r.Header, repl) - if h.Response.Deferred { + if h.Response.Deferred || h.Response.Require != nil { w = &responseWriterWrapper{ ResponseWriterWrapper: &caddyhttp.ResponseWriterWrapper{ResponseWriter: w}, replacer: repl, + require: h.Response.Require, headerOps: h.Response.HeaderOps, } } else { @@ -75,6 +77,7 @@ func apply(ops *HeaderOps, hdr http.Header, repl caddy2.Replacer) { type responseWriterWrapper struct { *caddyhttp.ResponseWriterWrapper replacer caddy2.Replacer + require *caddyhttp.ResponseMatcher headerOps *HeaderOps wroteHeader bool } @@ -91,7 +94,9 @@ func (rww *responseWriterWrapper) WriteHeader(status int) { return } rww.wroteHeader = true - apply(rww.headerOps, rww.ResponseWriterWrapper.Header(), rww.replacer) + if rww.require == nil || rww.require.Match(status, rww.ResponseWriterWrapper.Header()) { + apply(rww.headerOps, rww.ResponseWriterWrapper.Header(), rww.replacer) + } rww.ResponseWriterWrapper.WriteHeader(status) } |