proxy.go 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Package proxy is middleware that proxies HTTP requests.
package proxy

import (
	"errors"
	"net"
	"net/http"
	"net/url"
	"strings"
	"sync/atomic"
	"time"

	"github.com/mholt/caddy/caddyhttp/httpserver"
)

// Proxy represents a middleware instance that can proxy requests.
type Proxy struct {
	Next      httpserver.Handler
	Upstreams []Upstream
}

22
// Upstream manages a pool of proxy upstream hosts.
23 24 25
type Upstream interface {
	// The path this upstream host should be routed on
	From() string
26 27 28 29

	// Selects an upstream host to be routed to. It
	// should return a suitable upstream host, or nil
	// if no such hosts are available.
Kris Hamoud's avatar
Kris Hamoud committed
30
	Select(*http.Request) *UpstreamHost
31

32 33
	// Checks if subpath is not an ignored path
	AllowedPath(string) bool
34 35 36 37 38 39 40 41

	// Gets how long to try selecting upstream hosts
	// in the case of cascading failures.
	GetTryDuration() time.Duration

	// Gets how long to wait between selecting upstream
	// hosts in the case of cascading failures.
	GetTryInterval() time.Duration
42 43 44

	// Gets the number of upstream hosts.
	GetHostCount() int
45 46 47 48 49 50 51
}

// UpstreamHostDownFunc can be used to customize how Down behaves.
type UpstreamHostDownFunc func(*UpstreamHost) bool

// UpstreamHost represents a single proxy upstream
type UpstreamHost struct {
52 53
	// This field is read & written to concurrently, so all access must use
	// atomic operations.
54 55
	Conns             int64 // must be first field to be 64-bit aligned on 32-bit systems
	MaxConns          int64
56 57 58
	Name              string // hostname of this upstream host
	UpstreamHeaders   http.Header
	DownstreamHeaders http.Header
59
	FailTimeout       time.Duration
60 61
	CheckDown         UpstreamHostDownFunc
	WithoutPathPrefix string
62 63
	ReverseProxy      *ReverseProxy
	Fails             int32
64 65 66 67
	// This is an int32 so that we can use atomic operations to do concurrent
	// reads & writes to this value.  The default value of 0 indicates that it
	// is healthy and any non-zero value indicates unhealthy.
	Unhealthy int32
68 69 70 71 72 73 74 75
}

// Down checks whether the upstream host is down or not.
// Down will try to use uh.CheckDown first, and will fall
// back to some default criteria if necessary.
func (uh *UpstreamHost) Down() bool {
	if uh.CheckDown == nil {
		// Default settings
76
		return atomic.LoadInt32(&uh.Unhealthy) != 0 || atomic.LoadInt32(&uh.Fails) > 0
77 78 79 80 81 82
	}
	return uh.CheckDown(uh)
}

// Full checks whether the upstream host has reached its maximum connections
func (uh *UpstreamHost) Full() bool {
83
	return uh.MaxConns > 0 && atomic.LoadInt64(&uh.Conns) >= uh.MaxConns
84 85 86 87 88 89 90 91 92
}

// Available checks whether the upstream host is available for proxying to
func (uh *UpstreamHost) Available() bool {
	return !uh.Down() && !uh.Full()
}

// ServeHTTP satisfies the httpserver.Handler interface.
func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
93
	// start by selecting most specific matching upstream config
94 95 96 97
	upstream := p.match(r)
	if upstream == nil {
		return p.Next.ServeHTTP(w, r)
	}
98

99
	// this replacer is used to fill in header field values
100
	replacer := httpserver.NewReplacer(r, nil, "")
101

102
	// outreq is the request that makes a roundtrip to the backend
103
	outreq := createUpstreamRequest(r)
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	// If we have more than one upstream host defined and if retrying is enabled
	// by setting try_duration to a non-zero value, caddy will try to
	// retry the request at a different host if the first one failed.
	//
	// This requires us to possibly rewind and replay the request body though,
	// which in turn requires us to buffer the request body first.
	//
	// An unbuffered request is usually preferrable, because it reduces latency
	// as well as memory usage. Furthermore it enables different kinds of
	// HTTP streaming applications like gRPC for instance.
	requiresBuffering := upstream.GetHostCount() > 1 && upstream.GetTryDuration() != 0

	if requiresBuffering {
		body, err := newBufferedBody(outreq.Body)
		if err != nil {
			return http.StatusBadRequest, errors.New("failed to read downstream request body")
		}
		if body != nil {
			outreq.Body = body
		}
125 126
	}

127 128 129
	// The keepRetrying function will return true if we should
	// loop and try to select another host, or false if we
	// should break and stop retrying.
130
	start := time.Now()
131 132
	keepRetrying := func() bool {
		// if we've tried long enough, break
133
		if time.Since(start) >= upstream.GetTryDuration() {
134 135 136 137 138 139 140
			return false
		}
		// otherwise, wait and try the next available host
		time.Sleep(upstream.GetTryInterval())
		return true
	}

141
	var backendErr error
142
	for {
143 144
		// since Select() should give us "up" hosts, keep retrying
		// hosts until timeout (or until we get a nil host).
Kris Hamoud's avatar
Kris Hamoud committed
145
		host := upstream.Select(r)
146
		if host == nil {
147 148 149
			if backendErr == nil {
				backendErr = errors.New("no hosts available upstream")
			}
150 151 152 153
			if !keepRetrying() {
				break
			}
			continue
154 155 156 157 158
		}
		if rr, ok := w.(*httpserver.ResponseRecorder); ok && rr.Replacer != nil {
			rr.Replacer.Set("upstream", host.Name)
		}

159 160 161 162 163 164 165
		proxy := host.ReverseProxy

		// 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 {
166
				proxy = NewSingleHostReverseProxy(nameURL, host.WithoutPathPrefix, http.DefaultMaxIdleConnsPerHost)
167
			}
168 169 170 171 172 173

			// use upstream credentials by default
			if outreq.Header.Get("Authorization") == "" && nameURL.User != nil {
				pwd, _ := nameURL.User.Password()
				outreq.SetBasicAuth(nameURL.User.Username(), pwd)
			}
174 175 176 177 178 179
		} else {
			outreq.Host = host.Name
		}
		if proxy == nil {
			return http.StatusInternalServerError, errors.New("proxy for host '" + host.Name + "' is nil")
		}
180 181

		// set headers for request going upstream
182
		if host.UpstreamHeaders != nil {
183
			// modify headers for request that will be sent to the upstream host
184 185 186
			mutateHeadersByRules(outreq.Header, host.UpstreamHeaders, replacer)
			if hostHeaders, ok := outreq.Header["Host"]; ok && len(hostHeaders) > 0 {
				outreq.Host = hostHeaders[len(hostHeaders)-1]
187
			}
188
		}
189

190 191
		// prepare a function that will update response
		// headers coming back downstream
192 193 194 195
		var downHeaderUpdateFn respUpdateFn
		if host.DownstreamHeaders != nil {
			downHeaderUpdateFn = createRespHeaderUpdateFn(host.DownstreamHeaders, replacer)
		}
196

197 198 199 200 201 202
		// Before we retry the request we have to make sure
		// that the body is rewound to it's beginning.
		if bb, ok := outreq.Body.(*bufferedBody); ok {
			if err := bb.rewind(); err != nil {
				return http.StatusInternalServerError, errors.New("unable to rewind downstream request body")
			}
203 204
		}

205
		// tell the proxy to serve the request
206 207 208 209 210 211 212 213 214 215
		//
		// NOTE:
		//   The call to proxy.ServeHTTP can theoretically panic.
		//   To prevent host.Conns from getting out-of-sync we thus have to
		//   make sure that it's _always_ correctly decremented afterwards.
		func() {
			atomic.AddInt64(&host.Conns, 1)
			defer atomic.AddInt64(&host.Conns, -1)
			backendErr = proxy.ServeHTTP(w, outreq, downHeaderUpdateFn)
		}()
216

217
		// if no errors, we're done here
218 219 220
		if backendErr == nil {
			return 0, nil
		}
221

222 223 224 225
		if _, ok := backendErr.(httpserver.MaxBytesExceeded); ok {
			return http.StatusRequestEntityTooLarge, backendErr
		}

226 227
		// failover; remember this failure for some time if
		// request failure counting is enabled
228
		timeout := host.FailTimeout
229 230 231 232 233 234 235 236 237
		if timeout > 0 {
			atomic.AddInt32(&host.Fails, 1)
			go func(host *UpstreamHost, timeout time.Duration) {
				time.Sleep(timeout)
				atomic.AddInt32(&host.Fails, -1)
			}(host, timeout)
		}

		// if we've tried long enough, break
238
		if !keepRetrying() {
239
			break
240 241 242
		}
	}

243
	return http.StatusBadGateway, backendErr
244 245
}

246
// match finds the best match for a proxy config based on r.
247 248 249 250 251 252 253 254 255 256 257 258 259 260
func (p Proxy) match(r *http.Request) Upstream {
	var u Upstream
	var longestMatch int
	for _, upstream := range p.Upstreams {
		basePath := upstream.From()
		if !httpserver.Path(r.URL.Path).Matches(basePath) || !upstream.AllowedPath(r.URL.Path) {
			continue
		}
		if len(basePath) > longestMatch {
			longestMatch = len(basePath)
			u = upstream
		}
	}
	return u
261 262 263 264
}

// createUpstremRequest shallow-copies r into a new request
// that can be sent upstream.
265 266
//
// Derived from reverseproxy.go in the standard Go httputil package.
267 268 269
func createUpstreamRequest(r *http.Request) *http.Request {
	outreq := new(http.Request)
	*outreq = *r // includes shallow copies of maps, but okay
270 271 272 273 274
	// We should set body to nil explicitly if request body is empty.
	// For server requests the Request Body is always non-nil.
	if r.ContentLength == 0 {
		outreq.Body = nil
	}
275 276 277 278 279 280

	// Restore URL Path if it has been modified
	if outreq.URL.RawPath != "" {
		outreq.URL.Opaque = outreq.URL.RawPath
	}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
	// We are modifying the same underlying map from req (shallow
	// copied above) so we only copy it if necessary.
	copiedHeaders := false

	// Remove hop-by-hop headers listed in the "Connection" header.
	// See RFC 2616, section 14.10.
	if c := outreq.Header.Get("Connection"); c != "" {
		for _, f := range strings.Split(c, ",") {
			if f = strings.TrimSpace(f); f != "" {
				if !copiedHeaders {
					outreq.Header = make(http.Header)
					copyHeader(outreq.Header, r.Header)
					copiedHeaders = true
				}
				outreq.Header.Del(f)
			}
		}
	}

300
	// Remove hop-by-hop headers to the backend. Especially
301
	// important is "Connection" because we want a persistent
302
	// connection, regardless of what the client sent to us.
303 304
	for _, h := range hopHeaders {
		if outreq.Header.Get(h) != "" {
305 306 307 308 309
			if !copiedHeaders {
				outreq.Header = make(http.Header)
				copyHeader(outreq.Header, r.Header)
				copiedHeaders = true
			}
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
			outreq.Header.Del(h)
		}
	}

	if clientIP, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
		// If we aren't the first proxy, retain prior
		// X-Forwarded-For information as a comma+space
		// separated list and fold multiple headers into one.
		if prior, ok := outreq.Header["X-Forwarded-For"]; ok {
			clientIP = strings.Join(prior, ", ") + ", " + clientIP
		}
		outreq.Header.Set("X-Forwarded-For", clientIP)
	}

	return outreq
}

func createRespHeaderUpdateFn(rules http.Header, replacer httpserver.Replacer) respUpdateFn {
	return func(resp *http.Response) {
329
		mutateHeadersByRules(resp.Header, rules, replacer)
330 331 332
	}
}

333 334 335 336
func mutateHeadersByRules(headers, rules http.Header, repl httpserver.Replacer) {
	for ruleField, ruleValues := range rules {
		if strings.HasPrefix(ruleField, "+") {
			for _, ruleValue := range ruleValues {
Gyula Voros's avatar
Gyula Voros committed
337 338 339 340
				replacement := repl.Replace(ruleValue)
				if len(replacement) > 0 {
					headers.Add(strings.TrimPrefix(ruleField, "+"), replacement)
				}
341
			}
342 343 344
		} else if strings.HasPrefix(ruleField, "-") {
			headers.Del(strings.TrimPrefix(ruleField, "-"))
		} else if len(ruleValues) > 0 {
Gyula Voros's avatar
Gyula Voros committed
345 346 347 348
			replacement := repl.Replace(ruleValues[len(ruleValues)-1])
			if len(replacement) > 0 {
				headers.Set(ruleField, replacement)
			}
349 350 351
		}
	}
}