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"
15 "git.arvados.org/arvados.git/sdk/go/ctxlog"
18 // gitHandler is an http.Handler that invokes git-http-backend (or
19 // whatever backend is configured) via CGI, with appropriate
20 // environment variables in place for git-http-backend or
22 type gitHandler struct {
26 func newGitHandler(ctx context.Context, cluster *arvados.Cluster) http.Handler {
27 const glBypass = "GL_BYPASS_ACCESS_CHECKS"
28 const glHome = "GITOLITE_HTTP_HOME"
30 path := os.Getenv("PATH")
31 if cluster.Git.GitoliteHome != "" {
33 glHome+"="+cluster.Git.GitoliteHome,
35 path = path + ":" + cluster.Git.GitoliteHome + "/bin"
36 } else if home, bypass := os.Getenv(glHome), os.Getenv(glBypass); home != "" || bypass != "" {
37 env = append(env, glHome+"="+home, glBypass+"="+bypass)
38 ctxlog.FromContext(ctx).Printf("DEPRECATED: Passing through %s and %s environment variables. Use GitoliteHome configuration instead.", glHome, glBypass)
41 var listen arvados.URL
42 for listen = range cluster.Services.GitHTTP.InternalURLs {
46 "GIT_PROJECT_ROOT="+cluster.Git.Repositories,
47 "GIT_HTTP_EXPORT_ALL=",
48 "SERVER_ADDR="+listen.Host,
52 Path: cluster.Git.GitCommand,
53 Dir: cluster.Git.Repositories,
55 Args: []string{"http-backend"},
60 func (h *gitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
61 remoteHost, remotePort, err := net.SplitHostPort(r.RemoteAddr)
63 ctxlog.FromContext(r.Context()).Errorf("Internal error: SplitHostPort(r.RemoteAddr==%q): %s", r.RemoteAddr, err)
64 w.WriteHeader(http.StatusInternalServerError)
68 // Copy the wrapped cgi.Handler, so these request-specific
69 // variables don't leak into the next request.
70 handlerCopy := h.Handler
71 handlerCopy.Env = append(handlerCopy.Env,
72 // In Go1.5 we can skip this, net/http/cgi will do it for us:
73 "REMOTE_HOST="+remoteHost,
74 "REMOTE_ADDR="+remoteHost,
75 "REMOTE_PORT="+remotePort,
76 // Ideally this would be a real username:
77 "REMOTE_USER="+r.RemoteAddr,
79 handlerCopy.ServeHTTP(w, r)