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,
37 "Transfer-Encoding": true, // *-Encoding headers interfer with Go's automatic compression/decompression
38 "Content-Encoding": true,
39 "Accept-Encoding": true,
43 type ResponseFilter func(*http.Response, error) (*http.Response, error)
45 // Forward a request to upstream service, and return response or error.
49 client *http.Client) (*http.Response, error) {
51 // Copy headers from incoming request, then add/replace proxy
52 // headers like Via and X-Forwarded-For.
53 hdrOut := http.Header{}
54 for k, v := range reqIn.Header {
59 xff := reqIn.RemoteAddr
60 if xffIn := reqIn.Header.Get("X-Forwarded-For"); xffIn != "" {
61 xff = xffIn + "," + xff
63 hdrOut.Set("X-Forwarded-For", xff)
64 if hdrOut.Get("X-Forwarded-Proto") == "" {
65 hdrOut.Set("X-Forwarded-Proto", reqIn.URL.Scheme)
67 hdrOut.Add("Via", reqIn.Proto+" arvados-controller")
69 reqOut := (&http.Request{
75 }).WithContext(reqIn.Context())
77 resp, err := client.Do(reqOut)
81 // Copy a response (or error) to the downstream client
82 func (p *proxy) ForwardResponse(w http.ResponseWriter, resp *http.Response, err error) (int64, error) {
84 if he, ok := err.(HTTPError); ok {
85 httpserver.Error(w, he.Message, he.Code)
87 httpserver.Error(w, err.Error(), http.StatusBadGateway)
92 defer resp.Body.Close()
93 for k, v := range resp.Header {
98 w.WriteHeader(resp.StatusCode)
99 return io.Copy(w, resp.Body)