10 // gitHandler is an http.Handler that invokes git-http-backend (or
11 // whatever backend is configured) via CGI, with appropriate
12 // environment variables in place for git-http-backend or
14 type gitHandler struct {
18 func newGitHandler() http.Handler {
21 Path: theConfig.GitCommand,
24 "GIT_PROJECT_ROOT=" + theConfig.Root,
25 "GIT_HTTP_EXPORT_ALL=",
26 "SERVER_ADDR=" + theConfig.Addr,
30 // Needed if GitCommand is gitolite-shell:
32 "GL_BYPASS_ACCESS_CHECKS",
34 Args: []string{"http-backend"},
39 func (h *gitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
40 remoteHost, remotePort, err := net.SplitHostPort(r.RemoteAddr)
42 log.Printf("Internal error: SplitHostPort(r.RemoteAddr==%q): %s", r.RemoteAddr, err)
43 w.WriteHeader(http.StatusInternalServerError)
47 // Copy the wrapped cgi.Handler, so these request-specific
48 // variables don't leak into the next request.
49 handlerCopy := h.Handler
50 handlerCopy.Env = append(handlerCopy.Env,
51 // In Go1.5 we can skip this, net/http/cgi will do it for us:
52 "REMOTE_HOST="+remoteHost,
53 "REMOTE_ADDR="+remoteHost,
54 "REMOTE_PORT="+remotePort,
55 // Ideally this would be a real username:
56 "REMOTE_USER="+r.RemoteAddr,
58 handlerCopy.ServeHTTP(w, r)