1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.arvados.org/arvados.git/sdk/go/arvados"
17 // gitHandler is an http.Handler that invokes git-http-backend (or
18 // whatever backend is configured) via CGI, with appropriate
19 // environment variables in place for git-http-backend or
21 type gitHandler struct {
25 func newGitHandler(cluster *arvados.Cluster) http.Handler {
26 const glBypass = "GL_BYPASS_ACCESS_CHECKS"
27 const glHome = "GITOLITE_HTTP_HOME"
29 path := os.Getenv("PATH")
30 if cluster.Git.GitoliteHome != "" {
32 glHome+"="+cluster.Git.GitoliteHome,
34 path = path + ":" + cluster.Git.GitoliteHome + "/bin"
35 } else if home, bypass := os.Getenv(glHome), os.Getenv(glBypass); home != "" || bypass != "" {
36 env = append(env, glHome+"="+home, glBypass+"="+bypass)
37 log.Printf("DEPRECATED: Passing through %s and %s environment variables. Use GitoliteHome configuration instead.", glHome, glBypass)
40 var listen arvados.URL
41 for listen = range cluster.Services.GitHTTP.InternalURLs {
45 "GIT_PROJECT_ROOT="+cluster.Git.Repositories,
46 "GIT_HTTP_EXPORT_ALL=",
47 "SERVER_ADDR="+listen.Host,
51 Path: cluster.Git.GitCommand,
52 Dir: cluster.Git.Repositories,
54 Args: []string{"http-backend"},
59 func (h *gitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
60 remoteHost, remotePort, err := net.SplitHostPort(r.RemoteAddr)
62 log.Printf("Internal error: SplitHostPort(r.RemoteAddr==%q): %s", r.RemoteAddr, err)
63 w.WriteHeader(http.StatusInternalServerError)
67 // Copy the wrapped cgi.Handler, so these request-specific
68 // variables don't leak into the next request.
69 handlerCopy := h.Handler
70 handlerCopy.Env = append(handlerCopy.Env,
71 // In Go1.5 we can skip this, net/http/cgi will do it for us:
72 "REMOTE_HOST="+remoteHost,
73 "REMOTE_ADDR="+remoteHost,
74 "REMOTE_PORT="+remotePort,
75 // Ideally this would be a real username:
76 "REMOTE_USER="+r.RemoteAddr,
78 handlerCopy.ServeHTTP(w, r)