From: Andrew Gerrand Date: Thu, 20 Dec 2012 20:03:00 +0000 (+1100) Subject: cmd/godoc: redirect for file with trailing / X-Git-Tag: go1.1rc2~1579 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=2874cc4eedf1e152f8327c1d93fb5d6a6a973a15;p=gostls13.git cmd/godoc: redirect for file with trailing / Fixes #4543. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/6971050 --- diff --git a/src/cmd/godoc/godoc.go b/src/cmd/godoc/godoc.go index 70e1984b2a..1a9c43b6db 100644 --- a/src/cmd/godoc/godoc.go +++ b/src/cmd/godoc/godoc.go @@ -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 }