diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2019-05-20 23:48:43 -0600 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2019-05-20 23:48:43 -0600 |
commit | 65195a726d9ceff4bbf870b7baa7eff20cf35381 (patch) | |
tree | 6b6f19517e4874831197b535395cdc891d11dfbd /modules/caddyhttp/headers | |
parent | b84cb058484e7900d60324da1289d319c77f5447 (diff) |
Implement rewrite middleware; fix middleware stack bugs
Diffstat (limited to 'modules/caddyhttp/headers')
-rw-r--r-- | modules/caddyhttp/headers/headers.go | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/modules/caddyhttp/headers/headers.go b/modules/caddyhttp/headers/headers.go index 37efc57..b56bbf9 100644 --- a/modules/caddyhttp/headers/headers.go +++ b/modules/caddyhttp/headers/headers.go @@ -37,29 +37,36 @@ type RespHeaderOps struct { } func (h Headers) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { - apply(h.Request, r.Header) + repl := r.Context().Value(caddy2.ReplacerCtxKey).(caddy2.Replacer) + apply(h.Request, r.Header, repl) if h.Response.Deferred { w = &responseWriterWrapper{ ResponseWriterWrapper: &caddyhttp.ResponseWriterWrapper{ResponseWriter: w}, + replacer: repl, headerOps: h.Response.HeaderOps, } } else { - apply(h.Response.HeaderOps, w.Header()) + apply(h.Response.HeaderOps, w.Header(), repl) } return next.ServeHTTP(w, r) } -func apply(ops HeaderOps, hdr http.Header) { +func apply(ops HeaderOps, hdr http.Header, repl caddy2.Replacer) { for fieldName, vals := range ops.Add { + fieldName = repl.ReplaceAll(fieldName, "") for _, v := range vals { - hdr.Add(fieldName, v) + hdr.Add(fieldName, repl.ReplaceAll(v, "")) } } for fieldName, vals := range ops.Set { + fieldName = repl.ReplaceAll(fieldName, "") + for i := range vals { + vals[i] = repl.ReplaceAll(vals[i], "") + } hdr.Set(fieldName, strings.Join(vals, ",")) } for _, fieldName := range ops.Delete { - hdr.Del(fieldName) + hdr.Del(repl.ReplaceAll(fieldName, "")) } } @@ -67,6 +74,7 @@ func apply(ops HeaderOps, hdr http.Header) { // operations until WriteHeader is called. type responseWriterWrapper struct { *caddyhttp.ResponseWriterWrapper + replacer caddy2.Replacer headerOps HeaderOps wroteHeader bool } @@ -83,7 +91,7 @@ func (rww *responseWriterWrapper) WriteHeader(status int) { return } rww.wroteHeader = true - apply(rww.headerOps, rww.ResponseWriterWrapper.Header()) + apply(rww.headerOps, rww.ResponseWriterWrapper.Header(), rww.replacer) rww.ResponseWriterWrapper.WriteHeader(status) } |