13993: Support for federated collection requests by uuid
[arvados.git] / lib / controller / proxy.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package controller
6
7 import (
8         "context"
9         "io"
10         "net/http"
11         "net/url"
12         "time"
13
14         "git.curoverse.com/arvados.git/sdk/go/httpserver"
15 )
16
17 type proxy struct {
18         Name           string // to use in Via header
19         RequestTimeout time.Duration
20 }
21
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{
25         "Connection":          true,
26         "Keep-Alive":          true,
27         "Proxy-Authenticate":  true,
28         "Proxy-Authorization": true,
29         "TE":                true,
30         "Trailer":           true,
31         "Transfer-Encoding": true,
32         "Upgrade":           true,
33 }
34
35 type ResponseFilter func(*http.Response) (*http.Response, error)
36
37 func (p *proxy) Do(w http.ResponseWriter,
38         reqIn *http.Request,
39         urlOut *url.URL,
40         client *http.Client,
41         filter ResponseFilter) {
42
43         // Copy headers from incoming request, then add/replace proxy
44         // headers like Via and X-Forwarded-For.
45         hdrOut := http.Header{}
46         for k, v := range reqIn.Header {
47                 if !dropHeaders[k] {
48                         hdrOut[k] = v
49                 }
50         }
51         xff := reqIn.RemoteAddr
52         if xffIn := reqIn.Header.Get("X-Forwarded-For"); xffIn != "" {
53                 xff = xffIn + "," + xff
54         }
55         hdrOut.Set("X-Forwarded-For", xff)
56         if hdrOut.Get("X-Forwarded-Proto") == "" {
57                 hdrOut.Set("X-Forwarded-Proto", reqIn.URL.Scheme)
58         }
59         hdrOut.Add("Via", reqIn.Proto+" arvados-controller")
60
61         ctx := reqIn.Context()
62         if p.RequestTimeout > 0 {
63                 var cancel context.CancelFunc
64                 ctx, cancel = context.WithDeadline(ctx, time.Now().Add(time.Duration(p.RequestTimeout)))
65                 defer cancel()
66         }
67
68         reqOut := (&http.Request{
69                 Method: reqIn.Method,
70                 URL:    urlOut,
71                 Host:   reqIn.Host,
72                 Header: hdrOut,
73                 Body:   reqIn.Body,
74         }).WithContext(ctx)
75         resp, err := client.Do(reqOut)
76         if err != nil {
77                 httpserver.Error(w, err.Error(), http.StatusBadGateway)
78                 return
79         }
80
81         // make sure original response body gets closed
82         originalBody := resp.Body
83         defer originalBody.Close()
84
85         if filter != nil {
86                 resp, err = filter(resp)
87
88                 if err != nil {
89                         httpserver.Error(w, err.Error(), http.StatusBadGateway)
90                         return
91                 }
92                 if resp == nil {
93                         // filter() returned a nil response, this means suppress
94                         // writing a response, for the case where there might
95                         // be multiple response writers.
96                         return
97                 }
98
99                 // the filter gave us a new response body, make sure that gets closed too.
100                 if resp.Body != originalBody {
101                         defer resp.Body.Close()
102                 }
103         }
104         for k, v := range resp.Header {
105                 for _, v := range v {
106                         w.Header().Add(k, v)
107                 }
108         }
109         w.WriteHeader(resp.StatusCode)
110         n, err := io.Copy(w, resp.Body)
111         if err != nil {
112                 httpserver.Logger(reqIn).WithError(err).WithField("bytesCopied", n).Error("error copying response body")
113         }
114 }