MCPcopy
hub / github.com/fabiolb/fabio / ServeHTTP

Method ServeHTTP

proxy/http_proxy.go:83–260  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

81}
82
83func (p *HTTPProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
84 if p.Lookup == nil {
85 panic("no lookup function")
86 }
87
88 if p.Config.RequestID != "" {
89 id := p.UUID
90 if id == nil {
91 id = uuid.NewUUID
92 }
93 r.Header.Set(p.Config.RequestID, id())
94 }
95
96 t := p.Lookup(r)
97
98 if t == nil {
99 status := p.Config.NoRouteStatus
100 if status < 100 || status > 999 {
101 status = http.StatusNotFound
102 }
103 w.WriteHeader(status)
104 html := noroute.GetHTML()
105 if html != "" {
106 io.WriteString(w, html)
107 }
108 return
109 }
110
111 if t.AccessDeniedHTTP(r) {
112 http.Error(w, "access denied", http.StatusForbidden)
113 return
114 }
115
116 if !t.Authorized(r, w, p.AuthSchemes) {
117 http.Error(w, "authorization failed", http.StatusUnauthorized)
118 return
119 }
120
121 // build the request url since r.URL will get modified
122 // by the reverse proxy and contains only the RequestURI anyway
123 requestURL := &url.URL{
124 Scheme: scheme(r),
125 Host: r.Host,
126 Path: r.URL.Path,
127 RawQuery: r.URL.RawQuery,
128 }
129
130 if t.RedirectCode != 0 && t.RedirectURL != nil {
131 http.Redirect(w, r, t.RedirectURL.String(), t.RedirectCode)
132 if p.Stats.RedirectCounter != nil {
133 p.Stats.RedirectCounter.With("code", strconv.Itoa(t.RedirectCode)).Add(1)
134 }
135 return
136 }
137
138 // build the real target url that is passed to the proxy
139 targetURL := &url.URL{
140 Scheme: t.URL.Scheme,

Callers 4

testProxyLogOutputFunction · 0.95
BenchmarkProxyLoggerFunction · 0.95
TestProxyWSUpstreamFunction · 0.45
NewGzipHandlerFunction · 0.45

Calls 15

GetHTMLFunction · 0.92
NewGzipHandlerFunction · 0.92
schemeFunction · 0.85
addHeadersFunction · 0.85
addResponseHeadersFunction · 0.85
newWSHandlerFunction · 0.85
LookupMethod · 0.80
AccessDeniedHTTPMethod · 0.80
ErrorMethod · 0.80
newHTTPProxyFunction · 0.70
AuthorizedMethod · 0.65
DialMethod · 0.65

Tested by 3

testProxyLogOutputFunction · 0.76
BenchmarkProxyLoggerFunction · 0.76
TestProxyWSUpstreamFunction · 0.36