From d5192826d36605b89c1e60cf1c3329b0c85fc2a6 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 25 Jan 2011 10:38:36 -0800 Subject: [PATCH] Put gitweb.cgi at /code/ on the website. --- website/camweb.go | 23 +++++++++++++++++++++++ website/cgi.go | 27 ++++++++++++--------------- website/tmpl/page.html | 2 +- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/website/camweb.go b/website/camweb.go index 3173a6e8e..99b1fa0ce 100644 --- a/website/camweb.go +++ b/website/camweb.go @@ -19,6 +19,8 @@ const defaultAddr = ":31798" // default webserver address var ( httpAddr = flag.String("http", defaultAddr, "HTTP service address (e.g., '"+defaultAddr+"')") root = flag.String("root", "", "Website root (parent of 'static', 'content', and 'tmpl") + gitwebScript = flag.String("gitwebscript", "/usr/lib/cgi-bin/gitweb.cgi", "Path to gitweb.cgi, or blank to disable.") + gitwebFiles = flag.String("gitwebfiles", "/usr/share/gitweb", "Path to gitweb's static files.") pageHtml, errorHtml *template.Template ) @@ -154,6 +156,20 @@ func serveFile(rw http.ResponseWriter, req *http.Request, relPath, absPath strin servePage(rw, "", "", []byte(data)) } +type gitwebHandler struct{ + Cgi http.Handler + Static http.Handler +} + +func (h *gitwebHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { + if r.URL.RawPath == "/code/" || + strings.HasPrefix(r.URL.RawPath, "/code/?") { + h.Cgi.ServeHTTP(rw, r) + } else { + h.Static.ServeHTTP(rw, r) + } +} + func main() { flag.Parse() readTemplates() @@ -162,6 +178,13 @@ func main() { mux.Handle("/favicon.ico", http.FileServer(path.Join(*root, "static"), "/")) mux.Handle("/static/", http.FileServer(path.Join(*root, "static"), "/static/")) mux.Handle("/test.cgi", &CgiHandler{ExecutablePath: path.Join(*root, "test.cgi")}) + mux.Handle("/code", http.RedirectHandler("/code/", http.StatusFound)) + if *gitwebScript != "" { + mux.Handle("/code/", &gitwebHandler{ + Cgi: &CgiHandler{ExecutablePath: *gitwebScript}, + Static: http.FileServer(*gitwebFiles, "/code/"), + }) + } mux.HandleFunc("/", mainHandler) if err := http.ListenAndServe(*httpAddr, mux); err != nil { diff --git a/website/cgi.go b/website/cgi.go index 77bef8404..990335bd3 100644 --- a/website/cgi.go +++ b/website/cgi.go @@ -75,8 +75,8 @@ func (h *CgiHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { } linebody := line.NewReader(cmd.Stdout, 1024) - unsent := make(map[string]string) - sentStatus := false + headers := make(map[string]string) + statusCode := http.StatusOK for { line, isPrefix, err := linebody.ReadLine() if isPrefix { @@ -102,32 +102,29 @@ func (h *CgiHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { } h, v := parts[0], parts[1] h = strings.TrimSpace(h) - v = strings.TrimSpace(h) + v = strings.TrimSpace(v) switch { case h == "Status": if len(v) < 3 { - log.Printf("CGI: bogus status (short)") + log.Printf("CGI: bogus status (short): %q", v) return } code, err := strconv.Atoi(v[0:3]) if err != nil { - log.Printf("CGI: bogus status") + log.Printf("CGI: bogus status: %q", v) + log.Printf("CGI: line was %q", line) return } - rw.WriteHeader(code) - sentStatus = true - case sentStatus: - rw.SetHeader(h, v) - case !sentStatus: - unsent[h] = v + statusCode = code + default: + headers[h] = v } } - if !sentStatus { - rw.WriteHeader(http.StatusOK) - } - for h, v := range unsent { + for h, v := range headers { rw.SetHeader(h, v) } + rw.WriteHeader(statusCode) + _, err = io.Copy(rw, linebody) if err != nil { log.Printf("CGI: copy error: %v", err) diff --git a/website/tmpl/page.html b/website/tmpl/page.html index 65e15dd12..39159190c 100644 --- a/website/tmpl/page.html +++ b/website/tmpl/page.html @@ -17,7 +17,7 @@