From 2874cc4eedf1e152f8327c1d93fb5d6a6a973a15 Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Fri, 21 Dec 2012 07:03:00 +1100 Subject: [PATCH] cmd/godoc: redirect for file with trailing / Fixes #4543. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/6971050 --- src/cmd/godoc/godoc.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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 } -- 2.48.1