From 005a11cf4b0af5bfbbb72f8572681b327675541c Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Wed, 11 Sep 2019 18:48:37 -0600 Subject: headers: New 'request_header' directive; handle Host header specially Before this change, only response headers could be manipulated with the Caddyfile's 'header' directive. Also handle the request Host header specially, since the Go standard library treats it separately from the other header fields... --- modules/caddyhttp/headers/headers.go | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'modules/caddyhttp/headers/headers.go') diff --git a/modules/caddyhttp/headers/headers.go b/modules/caddyhttp/headers/headers.go index e740004..ba9d89d 100644 --- a/modules/caddyhttp/headers/headers.go +++ b/modules/caddyhttp/headers/headers.go @@ -58,7 +58,18 @@ type RespHeaderOps struct { func (h Headers) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { repl := r.Context().Value(caddy.ReplacerCtxKey).(caddy.Replacer) + apply(h.Request, r.Header, repl) + + // request header's Host is handled specially by the + // Go standard library, so if that header was changed, + // change it in the Host field since the Header won't + // be used + if intendedHost := r.Header.Get("Host"); intendedHost != "" { + r.Host = intendedHost + r.Header.Del("Host") + } + if h.Response != nil { if h.Response.Deferred || h.Response.Require != nil { w = &responseWriterWrapper{ -- cgit v1.2.3