Commit 3b9e6e34 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Comments and formatting

parent e0a71fd0
...@@ -22,6 +22,8 @@ type gitHandler struct { ...@@ -22,6 +22,8 @@ type gitHandler struct {
} }
var http_client = &http.Client{} var http_client = &http.Client{}
// Command-line options
var repo_root string var repo_root string
var listen_addr = flag.String("listen_addr", "localhost:8181", "Listen address for HTTP server") var listen_addr = flag.String("listen_addr", "localhost:8181", "Listen address for HTTP server")
var auth_backend = flag.String("auth_backend", "http://localhost:8080", "Authentication/authorization backend") var auth_backend = flag.String("auth_backend", "http://localhost:8080", "Authentication/authorization backend")
...@@ -33,16 +35,20 @@ var git_handlers = [...]gitHandler{ ...@@ -33,16 +35,20 @@ var git_handlers = [...]gitHandler{
} }
func main() { func main() {
// Parse the command-line
flag.Parse() flag.Parse()
repo_root = flag.Arg(0) repo_root = flag.Arg(0)
log.Printf("repo_root: %s", repo_root) log.Printf("repo_root: %s", repo_root)
http.HandleFunc("/", git_handler) http.HandleFunc("/", git_handler)
log.Fatal(http.ListenAndServe(*listen_addr, nil)) log.Fatal(http.ListenAndServe(*listen_addr, nil))
} }
func git_handler(w http.ResponseWriter, r *http.Request) { func git_handler(w http.ResponseWriter, r *http.Request) {
var user string var user string
log.Print(r.Method, " ", r.URL) log.Print(r.Method, " ", r.URL)
for _, g := range git_handlers { for _, g := range git_handlers {
path_match := g.regexp.FindStringSubmatch(r.URL.Path) path_match := g.regexp.FindStringSubmatch(r.URL.Path)
if r.Method == g.method && path_match != nil { if r.Method == g.method && path_match != nil {
...@@ -52,6 +58,9 @@ func git_handler(w http.ResponseWriter, r *http.Request) { ...@@ -52,6 +58,9 @@ func git_handler(w http.ResponseWriter, r *http.Request) {
return return
} }
if auth_response.StatusCode != 200 { if auth_response.StatusCode != 200 {
// The Git request is not allowed by the backend. Maybe the client
// needs to send HTTP Basic credentials.
// Forward the response from the auth backend to our client
for k, v := range auth_response.Header { for k, v := range auth_response.Header {
w.Header()[k] = v w.Header()[k] = v
} }
...@@ -59,6 +68,7 @@ func git_handler(w http.ResponseWriter, r *http.Request) { ...@@ -59,6 +68,7 @@ func git_handler(w http.ResponseWriter, r *http.Request) {
io.Copy(w, auth_response.Body) io.Copy(w, auth_response.Body)
return return
} }
// The auth backend told us who the user is according to them
if _, err := fmt.Fscan(auth_response.Body, &user); err != nil { if _, err := fmt.Fscan(auth_response.Body, &user); err != nil {
fail_500(w, err) fail_500(w, err)
return return
...@@ -121,6 +131,7 @@ func handle_get_info_refs(user string, _ string, path string, w http.ResponseWri ...@@ -121,6 +131,7 @@ func handle_get_info_refs(user string, _ string, path string, w http.ResponseWri
func handle_post_rpc(user string, rpc string, path string, w http.ResponseWriter, r *http.Request) { func handle_post_rpc(user string, rpc string, path string, w http.ResponseWriter, r *http.Request) {
var body io.Reader var body io.Reader
var err error var err error
if r.Header.Get("Content-Encoding") == "gzip" { if r.Header.Get("Content-Encoding") == "gzip" {
body, err = gzip.NewReader(r.Body) body, err = gzip.NewReader(r.Body)
if err != nil { if err != nil {
......
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