Commit a1bc94e4 authored by Matthew Holt's avatar Matthew Holt

Working on a fix for proxy related to setting Host header (cf. #874)

Also see
https://forum.caddyserver.com/t/caddy-0-9-beta-version-available-updated-beta-2/146/29?u=matt
which has another account of strange proxy behavior; I think this
resolves that.
parent 80dd95a4
...@@ -101,16 +101,26 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { ...@@ -101,16 +101,26 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
rr.Replacer.Set("upstream", host.Name) rr.Replacer.Set("upstream", host.Name)
} }
// for now, assume the backend's hostname is just a hostname; we'll proxy := host.ReverseProxy
// handle extra information like scheme later
outreq.Host = host.Name // a backend's name may contain more than just the host,
// so we parse it as a URL to try to isolate the host.
if nameURL, err := url.Parse(host.Name); err == nil {
outreq.Host = nameURL.Host
if proxy == nil {
proxy = NewSingleHostReverseProxy(nameURL, host.WithoutPathPrefix)
}
} else {
outreq.Host = host.Name
}
if proxy == nil {
return http.StatusInternalServerError, errors.New("proxy for host '" + host.Name + "' is nil")
}
// set headers for request going upstream // set headers for request going upstream
if host.UpstreamHeaders != nil { if host.UpstreamHeaders != nil {
if replacer == nil { if replacer == nil {
rHost := r.Host
replacer = httpserver.NewReplacer(r, nil, "") replacer = httpserver.NewReplacer(r, nil, "")
outreq.Host = rHost
} }
if v, ok := host.UpstreamHeaders["Host"]; ok { if v, ok := host.UpstreamHeaders["Host"]; ok {
outreq.Host = replacer.Replace(v[len(v)-1]) outreq.Host = replacer.Replace(v[len(v)-1])
...@@ -127,25 +137,11 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { ...@@ -127,25 +137,11 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
var downHeaderUpdateFn respUpdateFn var downHeaderUpdateFn respUpdateFn
if host.DownstreamHeaders != nil { if host.DownstreamHeaders != nil {
if replacer == nil { if replacer == nil {
rHost := r.Host
replacer = httpserver.NewReplacer(r, nil, "") replacer = httpserver.NewReplacer(r, nil, "")
outreq.Host = rHost
} }
downHeaderUpdateFn = createRespHeaderUpdateFn(host.DownstreamHeaders, replacer) downHeaderUpdateFn = createRespHeaderUpdateFn(host.DownstreamHeaders, replacer)
} }
// a backend's name may contain more than just the host,
// so we parse it as a URL so we can isolate the host.
proxy := host.ReverseProxy
if nameURL, err := url.Parse(outreq.Host); err == nil {
outreq.Host = nameURL.Host
if proxy == nil {
proxy = NewSingleHostReverseProxy(nameURL, host.WithoutPathPrefix)
}
} else if proxy == nil {
return http.StatusInternalServerError, err
}
// tell the proxy to serve the request // tell the proxy to serve the request
atomic.AddInt64(&host.Conns, 1) atomic.AddInt64(&host.Conns, 1)
backendErr := proxy.ServeHTTP(w, outreq, downHeaderUpdateFn) backendErr := proxy.ServeHTTP(w, outreq, downHeaderUpdateFn)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment