forked from fabiolb/fabio
/
proxy.go
72 lines (59 loc) · 1.59 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package proxy
import (
"net/http"
"time"
"github.com/eBay/fabio/config"
"github.com/eBay/fabio/metrics"
"github.com/eBay/fabio/proxy/gzip"
)
// httpProxy is a dynamic reverse proxy for HTTP and HTTPS protocols.
type httpProxy struct {
tr http.RoundTripper
cfg config.Proxy
requests metrics.Timer
noroute metrics.Counter
}
func NewHTTPProxy(tr http.RoundTripper, cfg config.Proxy) http.Handler {
return &httpProxy{
tr: tr,
cfg: cfg,
requests: metrics.DefaultRegistry.GetTimer("requests"),
noroute: metrics.DefaultRegistry.GetCounter("notfound"),
}
}
func (p *httpProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if ShuttingDown() {
http.Error(w, "shutting down", http.StatusServiceUnavailable)
return
}
t := target(r)
if t == nil {
p.noroute.Inc(1)
w.WriteHeader(p.cfg.NoRouteStatus)
return
}
if err := addHeaders(r, p.cfg); err != nil {
http.Error(w, "cannot parse "+r.RemoteAddr, http.StatusInternalServerError)
return
}
var h http.Handler
switch {
case r.Header.Get("Upgrade") == "websocket":
h = newRawProxy(t.URL)
// To use the filtered proxy use
// h = newWSProxy(t.URL)
case r.Header.Get("Accept") == "text/event-stream":
// use the flush interval for SSE (server-sent events)
// must be > 0s to be effective
h = newHTTPProxy(t.URL, p.tr, p.cfg.FlushInterval)
default:
h = newHTTPProxy(t.URL, p.tr, time.Duration(0))
}
if p.cfg.GZIPContentTypes != nil {
h = gzip.NewGzipHandler(h, p.cfg.GZIPContentTypes)
}
start := time.Now()
h.ServeHTTP(w, r)
p.requests.UpdateSince(start)
t.Timer.UpdateSince(start)
}