]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/godoc: redirect for file with trailing /
authorAndrew Gerrand <adg@golang.org>
Thu, 20 Dec 2012 20:03:00 +0000 (07:03 +1100)
committerAndrew Gerrand <adg@golang.org>
Thu, 20 Dec 2012 20:03:00 +0000 (07:03 +1100)
Fixes #4543.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6971050

src/cmd/godoc/godoc.go

index 70e1984b2a616a9270aec3d3b9f1612ddb9a3742..1a9c43b6db37635dcd9e06c7531d9abefca5985b 100644 (file)
@@ -658,6 +658,18 @@ func redirect(w http.ResponseWriter, r *http.Request) (redirected bool) {
        return
 }
 
+func redirectFile(w http.ResponseWriter, r *http.Request) (redirected bool) {
+       c := pathpkg.Clean(r.URL.Path)
+       for strings.HasSuffix("/", c) {
+               c = c[:len(c)-1]
+       }
+       if r.URL.Path != c {
+               http.Redirect(w, r, c, http.StatusMovedPermanently)
+               redirected = true
+       }
+       return
+}
+
 func serveTextFile(w http.ResponseWriter, r *http.Request, abspath, relpath, title string) {
        src, err := ReadFile(fs, abspath)
        if err != nil {
@@ -749,6 +761,9 @@ func serveFile(w http.ResponseWriter, r *http.Request) {
        }
 
        if isTextFile(abspath) {
+               if redirectFile(w, r) {
+                       return
+               }
                serveTextFile(w, r, abspath, relpath, "Text file")
                return
        }