Merge branch '14197-federated-containers' refs #14197
[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, // *-Encoding headers interfer with Go's automatic compression/decompression
32         "Content-Encoding":  true,
33         "Accept-Encoding":   true,
34         "Upgrade":           true,
35 }
36
37 type ResponseFilter func(*http.Response, error) (*http.Response, error)
38
39 func (p *proxy) Do(w http.ResponseWriter,
40         reqIn *http.Request,
41         urlOut *url.URL,
42         client *http.Client,
43         filter ResponseFilter) {
44
45         // Copy headers from incoming request, then add/replace proxy
46         // headers like Via and X-Forwarded-For.
47         hdrOut := http.Header{}
48         for k, v := range reqIn.Header {
49                 if !dropHeaders[k] {
50                         hdrOut[k] = v
51                 }
52         }
53         xff := reqIn.RemoteAddr
54         if xffIn := reqIn.Header.Get("X-Forwarded-For"); xffIn != "" {
55                 xff = xffIn + "," + xff
56         }
57         hdrOut.Set("X-Forwarded-For", xff)
58         if hdrOut.Get("X-Forwarded-Proto") == "" {
59                 hdrOut.Set("X-Forwarded-Proto", reqIn.URL.Scheme)
60         }
61         hdrOut.Add("Via", reqIn.Proto+" arvados-controller")
62
63         ctx := reqIn.Context()
64         if p.RequestTimeout > 0 {
65                 var cancel context.CancelFunc
66                 ctx, cancel = context.WithDeadline(ctx, time.Now().Add(time.Duration(p.RequestTimeout)))
67                 defer cancel()
68         }
69
70         reqOut := (&http.Request{
71                 Method: reqIn.Method,
72                 URL:    urlOut,
73                 Host:   reqIn.Host,
74                 Header: hdrOut,
75                 Body:   reqIn.Body,
76         }).WithContext(ctx)
77
78         resp, err := client.Do(reqOut)
79         if filter == nil && err != nil {
80                 httpserver.Error(w, err.Error(), http.StatusBadGateway)
81                 return
82         }
83
84         // make sure original response body gets closed
85         var originalBody io.ReadCloser
86         if resp != nil {
87                 originalBody = resp.Body
88                 if originalBody != nil {
89                         defer originalBody.Close()
90                 }
91         }
92
93         if filter != nil {
94                 resp, err = filter(resp, err)
95
96                 if err != nil {
97                         httpserver.Error(w, err.Error(), http.StatusBadGateway)
98                         return
99                 }
100                 if resp == nil {
101                         // filter() returned a nil response, this means suppress
102                         // writing a response, for the case where there might
103                         // be multiple response writers.
104                         return
105                 }
106
107                 // the filter gave us a new response body, make sure that gets closed too.
108                 if resp.Body != originalBody {
109                         defer resp.Body.Close()
110                 }
111         }
112
113         for k, v := range resp.Header {
114                 for _, v := range v {
115                         w.Header().Add(k, v)
116                 }
117         }
118         w.WriteHeader(resp.StatusCode)
119         n, err := io.Copy(w, resp.Body)
120         if err != nil {
121                 httpserver.Logger(reqIn).WithError(err).WithField("bytesCopied", n).Error("error copying response body")
122         }
123 }