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...
This commit is contained in:
Matthew Holt
2019-09-11 18:48:37 -06:00
parent 194df652eb
commit 005a11cf4b
3 changed files with 69 additions and 16 deletions

View File

@ -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{