mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-19 01:05:37 +03:00
Change outreq.Host instead of r.Host (possibly related to #874)
Also a few little formatting changes and comments.
This commit is contained in:
parent
5a45719227
commit
80dd95a495
3 changed files with 24 additions and 11 deletions
|
@ -77,19 +77,21 @@ var tryDuration = 60 * time.Second
|
||||||
|
|
||||||
// ServeHTTP satisfies the httpserver.Handler interface.
|
// ServeHTTP satisfies the httpserver.Handler interface.
|
||||||
func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
// Start by selecting most specific matching upstream config
|
// start by selecting most specific matching upstream config
|
||||||
upstream := p.match(r)
|
upstream := p.match(r)
|
||||||
if upstream == nil {
|
if upstream == nil {
|
||||||
return p.Next.ServeHTTP(w, r)
|
return p.Next.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this replacer is used to fill in header field values
|
||||||
var replacer httpserver.Replacer
|
var replacer httpserver.Replacer
|
||||||
start := time.Now()
|
|
||||||
|
|
||||||
|
// outreq is the request that makes a roundtrip to the backend
|
||||||
outreq := createUpstreamRequest(r)
|
outreq := createUpstreamRequest(r)
|
||||||
|
|
||||||
// Since Select() should give us "up" hosts, keep retrying
|
// since Select() should give us "up" hosts, keep retrying
|
||||||
// hosts until timeout (or until we get a nil host).
|
// hosts until timeout (or until we get a nil host).
|
||||||
|
start := time.Now()
|
||||||
for time.Now().Sub(start) < tryDuration {
|
for time.Now().Sub(start) < tryDuration {
|
||||||
host := upstream.Select()
|
host := upstream.Select()
|
||||||
if host == nil {
|
if host == nil {
|
||||||
|
@ -99,7 +101,11 @@ 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
|
||||||
|
// handle extra information like scheme later
|
||||||
outreq.Host = host.Name
|
outreq.Host = host.Name
|
||||||
|
|
||||||
|
// set headers for request going upstream
|
||||||
if host.UpstreamHeaders != nil {
|
if host.UpstreamHeaders != nil {
|
||||||
if replacer == nil {
|
if replacer == nil {
|
||||||
rHost := r.Host
|
rHost := r.Host
|
||||||
|
@ -109,13 +115,15 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
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])
|
||||||
}
|
}
|
||||||
// Modify headers for request that will be sent to the upstream host
|
// modify headers for request that will be sent to the upstream host
|
||||||
upHeaders := createHeadersByRules(host.UpstreamHeaders, r.Header, replacer)
|
upHeaders := createHeadersByRules(host.UpstreamHeaders, r.Header, replacer)
|
||||||
for k, v := range upHeaders {
|
for k, v := range upHeaders {
|
||||||
outreq.Header[k] = v
|
outreq.Header[k] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prepare a function that will update response
|
||||||
|
// headers coming back downstream
|
||||||
var downHeaderUpdateFn respUpdateFn
|
var downHeaderUpdateFn respUpdateFn
|
||||||
if host.DownstreamHeaders != nil {
|
if host.DownstreamHeaders != nil {
|
||||||
if replacer == nil {
|
if replacer == nil {
|
||||||
|
@ -123,23 +131,27 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
replacer = httpserver.NewReplacer(r, nil, "")
|
replacer = httpserver.NewReplacer(r, nil, "")
|
||||||
outreq.Host = rHost
|
outreq.Host = rHost
|
||||||
}
|
}
|
||||||
//Creates a function that is used to update headers the response received by the reverse proxy
|
|
||||||
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
|
proxy := host.ReverseProxy
|
||||||
if baseURL, err := url.Parse(host.Name); err == nil {
|
if nameURL, err := url.Parse(outreq.Host); err == nil {
|
||||||
r.Host = baseURL.Host
|
outreq.Host = nameURL.Host
|
||||||
if proxy == nil {
|
if proxy == nil {
|
||||||
proxy = NewSingleHostReverseProxy(baseURL, host.WithoutPathPrefix)
|
proxy = NewSingleHostReverseProxy(nameURL, host.WithoutPathPrefix)
|
||||||
}
|
}
|
||||||
} else if proxy == nil {
|
} else if proxy == nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
atomic.AddInt64(&host.Conns, -1)
|
atomic.AddInt64(&host.Conns, -1)
|
||||||
|
|
||||||
|
// if no errors, we're done here; otherwise failover
|
||||||
if backendErr == nil {
|
if backendErr == nil {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -158,6 +158,7 @@ func (u *staticUpstream) NewHost(host string) (*UpstreamHost, error) {
|
||||||
if u.insecureSkipVerify {
|
if u.insecureSkipVerify {
|
||||||
uh.ReverseProxy.Transport = InsecureTransport
|
uh.ReverseProxy.Transport = InsecureTransport
|
||||||
}
|
}
|
||||||
|
|
||||||
return uh, nil
|
return uh, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ func HTTPChallengeHandler(w http.ResponseWriter, r *http.Request, altPort string
|
||||||
|
|
||||||
proxy := httputil.NewSingleHostReverseProxy(upstream)
|
proxy := httputil.NewSingleHostReverseProxy(upstream)
|
||||||
proxy.Transport = &http.Transport{
|
proxy.Transport = &http.Transport{
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // solver uses self-signed certs
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
}
|
}
|
||||||
proxy.ServeHTTP(w, r)
|
proxy.ServeHTTP(w, r)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue