Commit 0b10eb3a authored by Jacob Vosmaer's avatar Jacob Vosmaer

Put gitHandler config in a struct

This lets us drop a few global variables.
parent 46e1ae4e
...@@ -28,6 +28,12 @@ import ( ...@@ -28,6 +28,12 @@ import (
"syscall" "syscall"
) )
type gitHandler struct {
httpClient *http.Client
repoRoot string
authBackend string
}
type gitService struct { type gitService struct {
method string method string
suffix string suffix string
...@@ -40,10 +46,8 @@ type gitEnv struct { ...@@ -40,10 +46,8 @@ type gitEnv struct {
} }
var Version string // Set at build time in the Makefile var Version string // Set at build time in the Makefile
var httpClient = &http.Client{}
// Command-line options // Command-line options
var repoRoot string
var printVersion = flag.Bool("version", false, "Print version and exit") var printVersion = flag.Bool("version", false, "Print version and exit")
var listenAddr = flag.String("listenAddr", "localhost:8181", "Listen address for HTTP server") var listenAddr = flag.String("listenAddr", "localhost:8181", "Listen address for HTTP server")
var listenNetwork = flag.String("listenNetwork", "tcp", "Listen 'network' (protocol)") var listenNetwork = flag.String("listenNetwork", "tcp", "Listen 'network' (protocol)")
...@@ -69,7 +73,7 @@ func main() { ...@@ -69,7 +73,7 @@ func main() {
fmt.Printf("gitlab-git-http-server %s\n", Version) fmt.Printf("gitlab-git-http-server %s\n", Version)
os.Exit(0) os.Exit(0)
} }
repoRoot = flag.Arg(0) repoRoot := flag.Arg(0)
if repoRoot == "" { if repoRoot == "" {
flag.Usage() flag.Usage()
os.Exit(1) os.Exit(1)
...@@ -91,11 +95,15 @@ func main() { ...@@ -91,11 +95,15 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
http.HandleFunc("/", gitHandler) http.Handle("/", newGitHandler(repoRoot, *authBackend))
log.Fatal(http.Serve(listener, nil)) log.Fatal(http.Serve(listener, nil))
} }
func gitHandler(w http.ResponseWriter, r *http.Request) { func newGitHandler(repoRoot, authBackend string) *gitHandler {
return &gitHandler{&http.Client{}, repoRoot, authBackend}
}
func (h *gitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var env gitEnv var env gitEnv
var g gitService var g gitService
...@@ -118,7 +126,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) { ...@@ -118,7 +126,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) {
// Ask the auth backend if the request is allowed, and what the // Ask the auth backend if the request is allowed, and what the
// user ID (GL_ID) is. // user ID (GL_ID) is.
authResponse, err := doAuthRequest(r) authResponse, err := h.doAuthRequest(r)
if err != nil { if err != nil {
fail500(w, err) fail500(w, err)
return return
...@@ -157,7 +165,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) { ...@@ -157,7 +165,7 @@ func gitHandler(w http.ResponseWriter, r *http.Request) {
// .- and ..-free URL.". In other words, we may assume that // .- and ..-free URL.". In other words, we may assume that
// r.URL.Path does not contain '/../', so there is no possibility // r.URL.Path does not contain '/../', so there is no possibility
// of path traversal here. // of path traversal here.
repoPath := path.Join(repoRoot, strings.TrimSuffix(r.URL.Path, g.suffix)) repoPath := path.Join(h.repoRoot, strings.TrimSuffix(r.URL.Path, g.suffix))
if !looksLikeRepo(repoPath) { if !looksLikeRepo(repoPath) {
http.Error(w, "Not Found", 404) http.Error(w, "Not Found", 404)
return return
...@@ -176,8 +184,8 @@ func looksLikeRepo(p string) bool { ...@@ -176,8 +184,8 @@ func looksLikeRepo(p string) bool {
return true return true
} }
func doAuthRequest(r *http.Request) (result *http.Response, err error) { func (h *gitHandler) doAuthRequest(r *http.Request) (result *http.Response, err error) {
url := fmt.Sprintf("%s%s", *authBackend, r.URL.RequestURI()) url := h.authBackend + r.URL.RequestURI()
authReq, err := http.NewRequest(r.Method, url, nil) authReq, err := http.NewRequest(r.Method, url, nil)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -187,7 +195,7 @@ func doAuthRequest(r *http.Request) (result *http.Response, err error) { ...@@ -187,7 +195,7 @@ func doAuthRequest(r *http.Request) (result *http.Response, err error) {
for k, v := range r.Header { for k, v := range r.Header {
authReq.Header[k] = v authReq.Header[k] = v
} }
return httpClient.Do(authReq) return h.httpClient.Do(authReq)
} }
func handleGetInfoRefs(env gitEnv, _ string, path string, w http.ResponseWriter, r *http.Request) { func handleGetInfoRefs(env gitEnv, _ string, path string, w http.ResponseWriter, r *http.Request) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment