14087: Fetch federated collection by PDH
[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, error) (*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
76         resp, err := client.Do(reqOut)
77         if filter == nil && err != nil {
78                 httpserver.Error(w, err.Error(), http.StatusBadGateway)
79                 return
80         }
81
82         // make sure original response body gets closed
83         originalBody := resp.Body
84         if originalBody != nil {
85                 defer originalBody.Close()
86         }
87
88         if filter != nil {
89                 resp, err = filter(resp, err)
90
91                 if err != nil {
92                         httpserver.Error(w, err.Error(), http.StatusBadGateway)
93                         return
94                 }
95                 if resp == nil {
96                         // filter() returned a nil response, this means suppress
97                         // writing a response, for the case where there might
98                         // be multiple response writers.
99                         return
100                 }
101
102                 // the filter gave us a new response body, make sure that gets closed too.
103                 if resp.Body != originalBody {
104                         defer resp.Body.Close()
105                 }
106         }
107
108         for k, v := range resp.Header {
109                 for _, v := range v {
110                         w.Header().Add(k, v)
111                 }
112         }
113         w.WriteHeader(resp.StatusCode)
114         n, err := io.Copy(w, resp.Body)
115         if err != nil {
116                 httpserver.Logger(reqIn).WithError(err).WithField("bytesCopied", n).Error("error copying response body")
117         }
118 }