1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.curoverse.com/arvados.git/sdk/go/httpserver"
18 Name string // to use in Via header
19 RequestTimeout time.Duration
22 // headers that shouldn't be forwarded when proxying. See
23 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers
24 var dropHeaders = map[string]bool{
27 "Proxy-Authenticate": true,
28 "Proxy-Authorization": true,
31 "Transfer-Encoding": true,
35 func (p *proxy) Do(w http.ResponseWriter, reqIn *http.Request, urlOut *url.URL, client *http.Client) {
36 // Copy headers from incoming request, then add/replace proxy
37 // headers like Via and X-Forwarded-For.
38 hdrOut := http.Header{}
39 for k, v := range reqIn.Header {
44 xff := reqIn.RemoteAddr
45 if xffIn := reqIn.Header.Get("X-Forwarded-For"); xffIn != "" {
46 xff = xffIn + "," + xff
48 hdrOut.Set("X-Forwarded-For", xff)
49 if hdrOut.Get("X-Forwarded-Proto") == "" {
50 hdrOut.Set("X-Forwarded-Proto", reqIn.URL.Scheme)
52 hdrOut.Add("Via", reqIn.Proto+" arvados-controller")
54 ctx := reqIn.Context()
55 if p.RequestTimeout > 0 {
56 var cancel context.CancelFunc
57 ctx, cancel = context.WithDeadline(ctx, time.Now().Add(time.Duration(p.RequestTimeout)))
61 reqOut := (&http.Request{
68 resp, err := client.Do(reqOut)
70 httpserver.Error(w, err.Error(), http.StatusBadGateway)
73 for k, v := range resp.Header {
78 w.WriteHeader(resp.StatusCode)
79 n, err := io.Copy(w, resp.Body)
81 httpserver.Logger(reqIn).WithError(err).WithField("bytesCopied", n).Error("error copying response body")