1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 "git.curoverse.com/arvados.git/sdk/go/httpserver"
16 Name string // to use in Via header
19 type HTTPError struct {
24 func (h HTTPError) Error() string {
28 // headers that shouldn't be forwarded when proxying. See
29 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers
30 var dropHeaders = map[string]bool{
33 "Proxy-Authenticate": true,
34 "Proxy-Authorization": true,
35 // this line makes gofmt 1.10 and 1.11 agree
38 "Transfer-Encoding": true, // *-Encoding headers interfer with Go's automatic compression/decompression
39 "Content-Encoding": true,
40 "Accept-Encoding": true,
44 type ResponseFilter func(*http.Response, error) (*http.Response, error)
46 // Forward a request to upstream service, and return response or error.
50 client *http.Client) (*http.Response, error) {
52 // Copy headers from incoming request, then add/replace proxy
53 // headers like Via and X-Forwarded-For.
54 hdrOut := http.Header{}
55 for k, v := range reqIn.Header {
60 xff := reqIn.RemoteAddr
61 if xffIn := reqIn.Header.Get("X-Forwarded-For"); xffIn != "" {
62 xff = xffIn + "," + xff
64 hdrOut.Set("X-Forwarded-For", xff)
65 if hdrOut.Get("X-Forwarded-Proto") == "" {
66 hdrOut.Set("X-Forwarded-Proto", reqIn.URL.Scheme)
68 hdrOut.Add("Via", reqIn.Proto+" arvados-controller")
70 reqOut := (&http.Request{
76 }).WithContext(reqIn.Context())
78 resp, err := client.Do(reqOut)
82 // Copy a response (or error) to the downstream client
83 func (p *proxy) ForwardResponse(w http.ResponseWriter, resp *http.Response, err error) (int64, error) {
85 if he, ok := err.(HTTPError); ok {
86 httpserver.Error(w, he.Message, he.Code)
88 httpserver.Error(w, err.Error(), http.StatusBadGateway)
93 defer resp.Body.Close()
94 for k, v := range resp.Header {
99 w.WriteHeader(resp.StatusCode)
100 return io.Copy(w, resp.Body)