X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/95e5ccacf6c1193b313fa90a6d39baafa2ba67d8..a7cdd1faaf1de132fa556944bc86831ebdfe8886:/lib/controller/proxy.go diff --git a/lib/controller/proxy.go b/lib/controller/proxy.go index 712071bef9..c0b94c2b5f 100644 --- a/lib/controller/proxy.go +++ b/lib/controller/proxy.go @@ -5,18 +5,24 @@ package controller import ( - "context" "io" "net/http" "net/url" - "time" "git.curoverse.com/arvados.git/sdk/go/httpserver" ) type proxy struct { - Name string // to use in Via header - RequestTimeout time.Duration + Name string // to use in Via header +} + +type HTTPError struct { + Message string + Code int +} + +func (h HTTPError) Error() string { + return h.Message } // headers that shouldn't be forwarded when proxying. See @@ -26,13 +32,23 @@ var dropHeaders = map[string]bool{ "Keep-Alive": true, "Proxy-Authenticate": true, "Proxy-Authorization": true, + // this line makes gofmt 1.10 and 1.11 agree "TE": true, "Trailer": true, - "Transfer-Encoding": true, + "Transfer-Encoding": true, // *-Encoding headers interfer with Go's automatic compression/decompression + "Content-Encoding": true, + "Accept-Encoding": true, "Upgrade": true, } -func (p *proxy) Do(w http.ResponseWriter, reqIn *http.Request, urlOut *url.URL, client *http.Client) { +type ResponseFilter func(*http.Response, error) (*http.Response, error) + +// Forward a request to upstream service, and return response or error. +func (p *proxy) Do( + reqIn *http.Request, + urlOut *url.URL, + client *http.Client) (*http.Response, error) { + // Copy headers from incoming request, then add/replace proxy // headers like Via and X-Forwarded-For. hdrOut := http.Header{} @@ -51,33 +67,35 @@ func (p *proxy) Do(w http.ResponseWriter, reqIn *http.Request, urlOut *url.URL, } hdrOut.Add("Via", reqIn.Proto+" arvados-controller") - ctx := reqIn.Context() - if p.RequestTimeout > 0 { - var cancel context.CancelFunc - ctx, cancel = context.WithDeadline(ctx, time.Now().Add(time.Duration(p.RequestTimeout))) - defer cancel() - } - reqOut := (&http.Request{ Method: reqIn.Method, URL: urlOut, Host: reqIn.Host, Header: hdrOut, Body: reqIn.Body, - }).WithContext(ctx) + }).WithContext(reqIn.Context()) + resp, err := client.Do(reqOut) + return resp, err +} + +// Copy a response (or error) to the downstream client +func (p *proxy) ForwardResponse(w http.ResponseWriter, resp *http.Response, err error) (int64, error) { if err != nil { - httpserver.Error(w, err.Error(), http.StatusBadGateway) - return + if he, ok := err.(HTTPError); ok { + httpserver.Error(w, he.Message, he.Code) + } else { + httpserver.Error(w, err.Error(), http.StatusBadGateway) + } + return 0, nil } + + defer resp.Body.Close() for k, v := range resp.Header { for _, v := range v { w.Header().Add(k, v) } } w.WriteHeader(resp.StatusCode) - n, err := io.Copy(w, resp.Body) - if err != nil { - httpserver.Logger(reqIn).WithError(err).WithField("bytesCopied", n).Error("error copying response body") - } + return io.Copy(w, resp.Body) }