diff options
author | diamondburned <[email protected]> | 2021-06-07 11:20:08 -0700 |
---|---|---|
committer | GitHub <[email protected]> | 2021-06-07 12:20:08 -0600 |
commit | f9b54454a19e2b070159ce8d2af76d819658244e (patch) | |
tree | 888ab85d70377385e617328ec34001b9695106b7 | |
parent | 658772ff24b9e1eabf6f254d039d91e8abfcb775 (diff) | |
download | caddy-f9b54454a19e2b070159ce8d2af76d819658244e.tar.gz caddy-f9b54454a19e2b070159ce8d2af76d819658244e.zip |
fileserver: Redirect within the original URL (#4179)
This commit changes the file_server directive to redirect using the
original request's URL instead of the possibly trimmed URL. This should
make file_server work with handle_path.
This fix is taken from mholt's comment in
https://caddy.community/t/file-servers-on-different-paths-not-working/11698/11.
-rw-r--r-- | modules/caddyhttp/fileserver/browse.go | 8 | ||||
-rw-r--r-- | modules/caddyhttp/fileserver/staticfiles.go | 14 |
2 files changed, 12 insertions, 10 deletions
diff --git a/modules/caddyhttp/fileserver/browse.go b/modules/caddyhttp/fileserver/browse.go index fc8bddb67..444a65620 100644 --- a/modules/caddyhttp/fileserver/browse.go +++ b/modules/caddyhttp/fileserver/browse.go @@ -47,10 +47,10 @@ func (fsrv *FileServer) serveBrowse(root, dirPath string, w http.ResponseWriter, // URL doesn't end in a trailing slash because hrefs like // "/b/c" on a path like "/a" end up going to "/b/c" instead // of "/a/b/c" - so we have to redirect in this case - if !strings.HasSuffix(r.URL.Path, "/") { - fsrv.logger.Debug("redirecting to trailing slash to preserve hrefs", zap.String("request_path", r.URL.Path)) - r.URL.Path += "/" - http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently) + oldReq := r.Context().Value(caddyhttp.OriginalRequestCtxKey).(http.Request) + if !strings.HasSuffix(oldReq.URL.Path, "/") { + fsrv.logger.Debug("redirecting to trailing slash to preserve hrefs", zap.String("request_path", oldReq.URL.Path)) + http.Redirect(w, r, oldReq.URL.Path+"/", http.StatusMovedPermanently) return nil } diff --git a/modules/caddyhttp/fileserver/staticfiles.go b/modules/caddyhttp/fileserver/staticfiles.go index f2320aab4..87e0a7717 100644 --- a/modules/caddyhttp/fileserver/staticfiles.go +++ b/modules/caddyhttp/fileserver/staticfiles.go @@ -243,12 +243,14 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c // trailing slash - not enforcing this can break relative hrefs // in HTML (see https://github.com/caddyserver/caddy/issues/2741) if fsrv.CanonicalURIs == nil || *fsrv.CanonicalURIs { - if implicitIndexFile && !strings.HasSuffix(r.URL.Path, "/") { - fsrv.logger.Debug("redirecting to canonical URI (adding trailing slash for directory)", zap.String("path", r.URL.Path)) - return redirect(w, r, r.URL.Path+"/") - } else if !implicitIndexFile && strings.HasSuffix(r.URL.Path, "/") { - fsrv.logger.Debug("redirecting to canonical URI (removing trailing slash for file)", zap.String("path", r.URL.Path)) - return redirect(w, r, r.URL.Path[:len(r.URL.Path)-1]) + oldReq := r.Context().Value(caddyhttp.OriginalRequestCtxKey).(http.Request) + + if implicitIndexFile && !strings.HasSuffix(oldReq.URL.Path, "/") { + fsrv.logger.Debug("redirecting to canonical URI (adding trailing slash for directory)", zap.String("path", oldReq.URL.Path)) + return redirect(w, r, oldReq.URL.Path+"/") + } else if !implicitIndexFile && strings.HasSuffix(oldReq.URL.Path, "/") { + fsrv.logger.Debug("redirecting to canonical URI (removing trailing slash for file)", zap.String("path", oldReq.URL.Path)) + return redirect(w, r, oldReq.URL.Path[:len(oldReq.URL.Path)-1]) } } |