1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.curoverse.com/arvados.git/sdk/go/arvados"
18 "git.curoverse.com/arvados.git/sdk/go/health"
19 "git.curoverse.com/arvados.git/sdk/go/httpserver"
23 Cluster *arvados.Cluster
24 NodeProfile *arvados.NodeProfile
27 handlerStack http.Handler
28 proxyClient *arvados.Client
31 func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
32 h.setupOnce.Do(h.setup)
33 if req.Method != "GET" && req.Method != "HEAD" {
34 // http.ServeMux returns 301 with a cleaned path if
35 // the incoming request has a double slash. Some
36 // clients (including the Go standard library) change
37 // the request method to GET when following a 301
38 // redirect if the original method was not HEAD
39 // (RFC7231 6.4.2 specifically allows this in the case
40 // of POST). Thus "POST //foo" gets misdirected to
41 // "GET /foo". To avoid this, eliminate double slashes
42 // before passing the request to ServeMux.
43 for strings.Contains(req.URL.Path, "//") {
44 req.URL.Path = strings.Replace(req.URL.Path, "//", "/", -1)
47 h.handlerStack.ServeHTTP(w, req)
50 func (h *Handler) CheckHealth() error {
51 h.setupOnce.Do(h.setup)
52 _, err := findRailsAPI(h.Cluster, h.NodeProfile)
56 func (h *Handler) setup() {
57 mux := http.NewServeMux()
58 mux.Handle("/_health/", &health.Handler{
59 Token: h.Cluster.ManagementToken,
62 mux.Handle("/", http.HandlerFunc(h.proxyRailsAPI))
65 // Changing the global isn't the right way to do this, but a
66 // proper solution would conflict with an impending 13493
67 // merge anyway, so this will do for now.
68 arvados.InsecureHTTPClient.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
71 // headers that shouldn't be forwarded when proxying. See
72 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers
73 var dropHeaders = map[string]bool{
76 "Proxy-Authenticate": true,
77 "Proxy-Authorization": true,
80 "Transfer-Encoding": true,
84 func (h *Handler) proxyRailsAPI(w http.ResponseWriter, reqIn *http.Request) {
85 urlOut, err := findRailsAPI(h.Cluster, h.NodeProfile)
87 httpserver.Error(w, err.Error(), http.StatusInternalServerError)
91 Scheme: urlOut.Scheme,
94 RawPath: reqIn.URL.RawPath,
95 RawQuery: reqIn.URL.RawQuery,
98 // Copy headers from incoming request, then add/replace proxy
99 // headers like Via and X-Forwarded-For.
100 hdrOut := http.Header{}
101 for k, v := range reqIn.Header {
106 xff := reqIn.RemoteAddr
107 if xffIn := reqIn.Header.Get("X-Forwarded-For"); xffIn != "" {
108 xff = xffIn + "," + xff
110 hdrOut.Set("X-Forwarded-For", xff)
111 if hdrOut.Get("X-Forwarded-Proto") == "" {
112 hdrOut.Set("X-Forwarded-Proto", reqIn.URL.Scheme)
114 hdrOut.Add("Via", reqIn.Proto+" arvados-controller")
116 ctx := reqIn.Context()
117 if timeout := h.Cluster.HTTPRequestTimeout; timeout > 0 {
118 var cancel context.CancelFunc
119 ctx, cancel = context.WithDeadline(ctx, time.Now().Add(time.Duration(timeout)))
123 reqOut := (&http.Request{
124 Method: reqIn.Method,
130 resp, err := arvados.InsecureHTTPClient.Do(reqOut)
132 httpserver.Error(w, err.Error(), http.StatusInternalServerError)
135 for k, v := range resp.Header {
136 for _, v := range v {
140 w.WriteHeader(resp.StatusCode)
141 n, err := io.Copy(w, resp.Body)
143 httpserver.Logger(reqIn).WithError(err).WithField("bytesCopied", n).Error("error copying response body")
147 // For now, findRailsAPI always uses the rails API running on this
149 func findRailsAPI(cluster *arvados.Cluster, np *arvados.NodeProfile) (*url.URL, error) {
150 hostport := np.RailsAPI.Listen
151 if len(hostport) > 1 && hostport[0] == ':' && strings.TrimRight(hostport[1:], "0123456789") == "" {
152 // ":12345" => connect to indicated port on localhost
153 hostport = "localhost" + hostport
154 } else if _, _, err := net.SplitHostPort(hostport); err == nil {
155 // "[::1]:12345" => connect to indicated address & port
163 return url.Parse(proto + "://" + hostport)