forked from fabiolb/fabio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
59 lines (48 loc) · 1.12 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
package proxy
import (
"net/http"
"time"
"github.com/eBay/fabio/config"
gometrics "github.com/rcrowley/go-metrics"
)
// Proxy is a dynamic reverse proxy.
type Proxy struct {
tr http.RoundTripper
cfg config.Proxy
requests gometrics.Timer
}
func New(tr http.RoundTripper, cfg config.Proxy) *Proxy {
return &Proxy{
tr: tr,
cfg: cfg,
requests: gometrics.GetOrRegisterTimer("requests", gometrics.DefaultRegistry),
}
}
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if ShuttingDown() {
http.Error(w, "shutting down", http.StatusServiceUnavailable)
return
}
t := target(r)
if t == nil {
w.WriteHeader(http.StatusNotFound)
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)
default:
h = newHTTPProxy(t.URL, p.tr)
}
start := time.Now()
h.ServeHTTP(w, r)
p.requests.UpdateSince(start)
t.Timer.UpdateSince(start)
}