| 15 | const StatusClientClosedRequest = 499 |
| 16 | |
| 17 | func newHTTPProxy(target *url.URL, tr http.RoundTripper, flush time.Duration) http.Handler { |
| 18 | return &httputil.ReverseProxy{ |
| 19 | // this is a simplified director function based on the |
| 20 | // httputil.NewSingleHostReverseProxy() which does not |
| 21 | // mangle the request and target URL since the target |
| 22 | // URL is already in the correct format. |
| 23 | Director: func(req *http.Request) { |
| 24 | req.URL.Scheme = target.Scheme |
| 25 | req.URL.Host = target.Host |
| 26 | req.URL.Path = target.Path |
| 27 | req.URL.RawQuery = target.RawQuery |
| 28 | if _, ok := req.Header["User-Agent"]; !ok { |
| 29 | // explicitly disable User-Agent so it's not set to default value |
| 30 | req.Header.Set("User-Agent", "") |
| 31 | } |
| 32 | }, |
| 33 | FlushInterval: flush, |
| 34 | Transport: tr, |
| 35 | ErrorHandler: httpProxyErrorHandler, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func httpProxyErrorHandler(w http.ResponseWriter, r *http.Request, err error) { |
| 40 | // According to https://golang.org/src/net/http/httputil/reverseproxy.go#L74, Go will return a 502 (Bad Gateway) StatusCode by default if no ErrorHandler is provided |