Merge branch '15946-collectionfs-deadlock'
[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         "io"
9         "net/http"
10         "net/url"
11
12         "git.arvados.org/arvados.git/sdk/go/httpserver"
13 )
14
15 type proxy struct {
16         Name string // to use in Via header
17 }
18
19 type HTTPError struct {
20         Message string
21         Code    int
22 }
23
24 func (h HTTPError) Error() string {
25         return h.Message
26 }
27
28 var dropHeaders = map[string]bool{
29         // Headers that shouldn't be forwarded when proxying. See
30         // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers
31         "Connection":          true,
32         "Keep-Alive":          true,
33         "Proxy-Authenticate":  true,
34         "Proxy-Authorization": true,
35         // (comment/space here makes gofmt1.10 agree with gofmt1.11)
36         "TE":      true,
37         "Trailer": true,
38         "Upgrade": true,
39
40         // Headers that would interfere with Go's automatic
41         // compression/decompression if we forwarded them.
42         "Accept-Encoding":   true,
43         "Content-Encoding":  true,
44         "Transfer-Encoding": true,
45 }
46
47 type ResponseFilter func(*http.Response, error) (*http.Response, error)
48
49 // Forward a request to upstream service, and return response or error.
50 func (p *proxy) Do(
51         reqIn *http.Request,
52         urlOut *url.URL,
53         client *http.Client) (*http.Response, error) {
54
55         // Copy headers from incoming request, then add/replace proxy
56         // headers like Via and X-Forwarded-For.
57         hdrOut := http.Header{}
58         for k, v := range reqIn.Header {
59                 if !dropHeaders[k] {
60                         hdrOut[k] = v
61                 }
62         }
63         xff := reqIn.RemoteAddr
64         if xffIn := reqIn.Header.Get("X-Forwarded-For"); xffIn != "" {
65                 xff = xffIn + "," + xff
66         }
67         hdrOut.Set("X-Forwarded-For", xff)
68         if hdrOut.Get("X-Forwarded-Proto") == "" {
69                 hdrOut.Set("X-Forwarded-Proto", reqIn.URL.Scheme)
70         }
71         hdrOut.Add("Via", reqIn.Proto+" arvados-controller")
72
73         reqOut := (&http.Request{
74                 Method: reqIn.Method,
75                 URL:    urlOut,
76                 Host:   reqIn.Host,
77                 Header: hdrOut,
78                 Body:   reqIn.Body,
79         }).WithContext(reqIn.Context())
80
81         resp, err := client.Do(reqOut)
82         return resp, err
83 }
84
85 // Copy a response (or error) to the downstream client
86 func (p *proxy) ForwardResponse(w http.ResponseWriter, resp *http.Response, err error) (int64, error) {
87         if err != nil {
88                 if he, ok := err.(HTTPError); ok {
89                         httpserver.Error(w, he.Message, he.Code)
90                 } else {
91                         httpserver.Error(w, err.Error(), http.StatusBadGateway)
92                 }
93                 return 0, nil
94         }
95
96         defer resp.Body.Close()
97         for k, v := range resp.Header {
98                 for _, v := range v {
99                         w.Header().Add(k, v)
100                 }
101         }
102         w.WriteHeader(resp.StatusCode)
103         return io.Copy(w, resp.Body)
104 }