From ee615371a87b068cdf75792bcc669647fd2d9edd Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sat, 6 Aug 2016 14:40:58 -0600 Subject: [PATCH] Export staticfiles.Redirect for convenience in preserving query string --- caddyhttp/browse/browse.go | 2 +- caddyhttp/staticfiles/fileserver.go | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/caddyhttp/browse/browse.go b/caddyhttp/browse/browse.go index 4e804f05..63ad14af 100644 --- a/caddyhttp/browse/browse.go +++ b/caddyhttp/browse/browse.go @@ -288,7 +288,7 @@ inScope: // Browsing navigation gets messed up if browsing a directory // that doesn't end in "/" (which it should, anyway) 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 } diff --git a/caddyhttp/staticfiles/fileserver.go b/caddyhttp/staticfiles/fileserver.go index a2b874f8..99ef406a 100644 --- a/caddyhttp/staticfiles/fileserver.go +++ b/caddyhttp/staticfiles/fileserver.go @@ -81,13 +81,13 @@ func (fs FileServer) serveFile(w http.ResponseWriter, r *http.Request, name stri if d.IsDir() { // Ensure / at end of directory url if !strings.HasSuffix(url, "/") { - redirect(w, r, path.Base(url)+"/") + Redirect(w, r, path.Base(url)+"/", http.StatusMovedPermanently) return http.StatusMovedPermanently, nil } } else { // Ensure no / at end of file url if strings.HasSuffix(url, "/") { - redirect(w, r, "../"+path.Base(url)) + Redirect(w, r, "../"+path.Base(url), http.StatusMovedPermanently) return http.StatusMovedPermanently, nil } } @@ -149,14 +149,14 @@ func (fs FileServer) isHidden(d os.FileInfo) bool { return false } -// redirect is taken from http.localRedirect of the std lib. It -// sends an HTTP permanent redirect to the client but will -// preserve the query string for the new path. -func redirect(w http.ResponseWriter, r *http.Request, newPath string) { +// Redirect sends an HTTP redirect to the client but will preserve +// the query string for the new path. Based on http.localRedirect +// from the Go standard library. +func Redirect(w http.ResponseWriter, r *http.Request, newPath string, statusCode int) { if q := r.URL.RawQuery; 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