fileserver: Redirect within the original URL

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.
This commit is contained in:
diamondburned 2021-05-25 15:21:18 -07:00
parent b4cef492cc
commit 23dadc0d86
2 changed files with 12 additions and 10 deletions

View File

@ -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 // 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 // "/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 // of "/a/b/c" - so we have to redirect in this case
if !strings.HasSuffix(r.URL.Path, "/") { oldReq := r.Context().Value(caddyhttp.OriginalRequestCtxKey).(http.Request)
fsrv.logger.Debug("redirecting to trailing slash to preserve hrefs", zap.String("request_path", r.URL.Path)) if !strings.HasSuffix(oldReq.URL.Path, "/") {
r.URL.Path += "/" fsrv.logger.Debug("redirecting to trailing slash to preserve hrefs", zap.String("request_path", oldReq.URL.Path))
http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently) http.Redirect(w, r, oldReq.URL.Path+"/", http.StatusMovedPermanently)
return nil return nil
} }

View File

@ -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 // trailing slash - not enforcing this can break relative hrefs
// in HTML (see https://github.com/caddyserver/caddy/issues/2741) // in HTML (see https://github.com/caddyserver/caddy/issues/2741)
if fsrv.CanonicalURIs == nil || *fsrv.CanonicalURIs { if fsrv.CanonicalURIs == nil || *fsrv.CanonicalURIs {
if implicitIndexFile && !strings.HasSuffix(r.URL.Path, "/") { oldReq := r.Context().Value(caddyhttp.OriginalRequestCtxKey).(http.Request)
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+"/") if implicitIndexFile && !strings.HasSuffix(oldReq.URL.Path, "/") {
} else if !implicitIndexFile && strings.HasSuffix(r.URL.Path, "/") { fsrv.logger.Debug("redirecting to canonical URI (adding trailing slash for directory)", zap.String("path", oldReq.URL.Path))
fsrv.logger.Debug("redirecting to canonical URI (removing trailing slash for file)", zap.String("path", r.URL.Path)) return redirect(w, r, oldReq.URL.Path+"/")
return redirect(w, r, r.URL.Path[:len(r.URL.Path)-1]) } 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])
} }
} }