Commit 0fa72053 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Map all HTTP errors to 502

parent b0452381
...@@ -113,7 +113,8 @@ func main() { ...@@ -113,7 +113,8 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
var authTransport http.RoundTripper // Create Proxy Transport
authTransport := http.DefaultTransport
if *authSocket != "" { if *authSocket != "" {
dialer := &net.Dialer{ dialer := &net.Dialer{
// The values below are taken from http.DefaultTransport // The values below are taken from http.DefaultTransport
...@@ -126,6 +127,7 @@ func main() { ...@@ -126,6 +127,7 @@ func main() {
}, },
} }
} }
proxyTransport := &proxyRoundTripper{transport: authTransport}
// The profiler will only be activated by HTTP requests. HTTP // The profiler will only be activated by HTTP requests. HTTP
// requests can only reach the profiler if we start a listener. So by // requests can only reach the profiler if we start a listener. So by
...@@ -137,7 +139,7 @@ func main() { ...@@ -137,7 +139,7 @@ func main() {
}() }()
} }
upstream := newUpstream(*authBackend, authTransport) upstream := newUpstream(*authBackend, proxyTransport)
upstream.SetRelativeUrlRoot(*relativeUrlRoot) upstream.SetRelativeUrlRoot(*relativeUrlRoot)
upstream.SetProxyTimeout(*proxyTimeout) upstream.SetProxyTimeout(*proxyTimeout)
......
package main package main
import ( import (
"bytes"
"io/ioutil"
"net/http" "net/http"
) )
type proxyRoundTripper struct {
transport http.RoundTripper
}
func (p *proxyRoundTripper) RoundTrip(r *http.Request) (res *http.Response, err error) {
res, err = p.transport.RoundTrip(r)
// Map error to 502 response
if err != nil {
res = &http.Response{
StatusCode: 502,
Status: err.Error(),
Request: r,
ProtoMajor: r.ProtoMajor,
ProtoMinor: r.ProtoMinor,
Proto: r.Proto,
Header: make(http.Header),
Trailer: make(http.Header),
Body: ioutil.NopCloser(&bytes.Buffer{}),
}
err = nil
}
return
}
func headerClone(h http.Header) http.Header { func headerClone(h http.Header) http.Header {
h2 := make(http.Header, len(h)) h2 := make(http.Header, len(h))
for k, vv := range h { for k, vv := range h {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment