From 14fdd3f06615f5297381b44b6ec2cbdd8d8cae45 Mon Sep 17 00:00:00 2001 From: mpl Date: Fri, 22 Nov 2013 00:04:54 +0100 Subject: [PATCH] website: more redirects to the issues tracker http://camlistore.org/issue/243 Change-Id: I25424ae5e69f602e8a8bb8d69954f37f28b8f687 --- website/camweb.go | 25 ++++++++++++++++++------- website/camweb_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/website/camweb.go b/website/camweb.go index 44362aa92..1f4e9f993 100644 --- a/website/camweb.go +++ b/website/camweb.go @@ -199,6 +199,11 @@ func mainHandler(rw http.ResponseWriter, req *http.Request) { return } + if dest, ok := issueRedirect(req.URL.Path); ok { + http.Redirect(rw, req, dest, http.StatusFound) + return + } + relPath := req.URL.Path[1:] // serveFile URL paths start with '/' if strings.Contains(relPath, "..") { return @@ -336,7 +341,6 @@ func main() { mux.HandleFunc("/r/", gerritRedirect) mux.HandleFunc("/debugz/ip", ipHandler) - mux.HandleFunc("/issue/", issueRedirect) mux.HandleFunc("/", mainHandler) if *buildbotHost != "" && *buildbotBackend != "" { @@ -384,15 +388,22 @@ func main() { log.Fatalf("Serve error: %v", <-errc) } -var issueNum = regexp.MustCompile(`^/issue/(\d+)$`) +var issueNum = regexp.MustCompile(`^/(?:issue(?:s)?|bugs)(/\d*)?$`) -func issueRedirect(w http.ResponseWriter, r *http.Request) { - m := issueNum.FindStringSubmatch(r.URL.Path) +// issueRedirect returns whether the request should be redirected to the +// issues tracker, and the url for that redirection if yes, the empty +// string otherwise. +func issueRedirect(urlPath string) (string, bool) { + m := issueNum.FindStringSubmatch(urlPath) if m == nil { - http.Error(w, "Bad request", 400) - return + return "", false } - http.Redirect(w, r, "https://code.google.com/p/camlistore/issues/detail?id="+m[1], http.StatusFound) + issueNumber := strings.TrimPrefix(m[1], "/") + suffix := "list" + if issueNumber != "" { + suffix = "detail?id=" + issueNumber + } + return "https://code.google.com/p/camlistore/issues/" + suffix, true } func gerritRedirect(w http.ResponseWriter, r *http.Request) { diff --git a/website/camweb_test.go b/website/camweb_test.go index 0ad9f324e..142f83ddb 100644 --- a/website/camweb_test.go +++ b/website/camweb_test.go @@ -43,3 +43,32 @@ func TestRedirect(t *testing.T) { } } + +func TestIsIssueRequest(t *testing.T) { + wantNum := "https://code.google.com/p/camlistore/issues/detail?id=34" + wantList := "https://code.google.com/p/camlistore/issues/list" + tests := []struct { + urlPath string + redirects bool + dest string + }{ + {"/issue", true, wantList}, + {"/issue/", true, wantList}, + {"/issue/34", true, wantNum}, + {"/issue34", false, ""}, + {"/issues", true, wantList}, + {"/issues/", true, wantList}, + {"/issues/34", true, wantNum}, + {"/issues34", false, ""}, + {"/bugs", true, wantList}, + {"/bugs/", true, wantList}, + {"/bugs/34", true, wantNum}, + {"/bugs34", false, ""}, + } + for _, tt := range tests { + dest, ok := issueRedirect(tt.urlPath) + if ok != tt.redirects || dest != tt.dest { + t.Errorf("issueRedirect(%q) = %q, %v; want %q, %v", tt.urlPath, dest, ok, tt.dest, tt.redirects) + } + } +}