summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/headers
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2019-05-28 18:53:08 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2019-05-28 18:53:08 -0600
commitbf54615efcaa98ab2e5c83e19a0e7b57897ca2c8 (patch)
tree7ab4296eb6ffd67622516e393fc50566f585ab2a /modules/caddyhttp/headers
parentda6a8cfc86620300734405d69c527856c0e71d66 (diff)
ResponseMatcher for conditional logic of response headers
Diffstat (limited to 'modules/caddyhttp/headers')
-rw-r--r--modules/caddyhttp/headers/headers.go11
1 files changed, 8 insertions, 3 deletions
diff --git a/modules/caddyhttp/headers/headers.go b/modules/caddyhttp/headers/headers.go
index 4cab5b5..b07a588 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)
}