Commit ee615371 authored by Matthew Holt's avatar Matthew Holt

Export staticfiles.Redirect for convenience in preserving query string

parent 4c6082df
...@@ -288,7 +288,7 @@ inScope: ...@@ -288,7 +288,7 @@ inScope:
// Browsing navigation gets messed up if browsing a directory // Browsing navigation gets messed up if browsing a directory
// that doesn't end in "/" (which it should, anyway) // that doesn't end in "/" (which it should, anyway)
if !strings.HasSuffix(r.URL.Path, "/") { if !strings.HasSuffix(r.URL.Path, "/") {
http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect) staticfiles.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect)
return 0, nil return 0, nil
} }
......
...@@ -81,13 +81,13 @@ func (fs FileServer) serveFile(w http.ResponseWriter, r *http.Request, name stri ...@@ -81,13 +81,13 @@ func (fs FileServer) serveFile(w http.ResponseWriter, r *http.Request, name stri
if d.IsDir() { if d.IsDir() {
// Ensure / at end of directory url // Ensure / at end of directory url
if !strings.HasSuffix(url, "/") { if !strings.HasSuffix(url, "/") {
redirect(w, r, path.Base(url)+"/") Redirect(w, r, path.Base(url)+"/", http.StatusMovedPermanently)
return http.StatusMovedPermanently, nil return http.StatusMovedPermanently, nil
} }
} else { } else {
// Ensure no / at end of file url // Ensure no / at end of file url
if strings.HasSuffix(url, "/") { if strings.HasSuffix(url, "/") {
redirect(w, r, "../"+path.Base(url)) Redirect(w, r, "../"+path.Base(url), http.StatusMovedPermanently)
return http.StatusMovedPermanently, nil return http.StatusMovedPermanently, nil
} }
} }
...@@ -149,14 +149,14 @@ func (fs FileServer) isHidden(d os.FileInfo) bool { ...@@ -149,14 +149,14 @@ func (fs FileServer) isHidden(d os.FileInfo) bool {
return false return false
} }
// redirect is taken from http.localRedirect of the std lib. It // Redirect sends an HTTP redirect to the client but will preserve
// sends an HTTP permanent redirect to the client but will // the query string for the new path. Based on http.localRedirect
// preserve the query string for the new path. // from the Go standard library.
func redirect(w http.ResponseWriter, r *http.Request, newPath string) { func Redirect(w http.ResponseWriter, r *http.Request, newPath string, statusCode int) {
if q := r.URL.RawQuery; q != "" { if q := r.URL.RawQuery; q != "" {
newPath += "?" + q newPath += "?" + q
} }
http.Redirect(w, r, newPath, http.StatusMovedPermanently) http.Redirect(w, r, newPath, statusCode)
} }
// IndexPages is a list of pages that may be understood as // IndexPages is a list of pages that may be understood as
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment