@marcel91200 / caddy Mirror · read-only / commits / f9b54454
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

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.

diamondburned committed Jun 7, 2021 at 11:20 UTC f9b54454a19e2b070159ce8d2af76d819658244e
2 files changed +12 -10
modules/caddyhttp/fileserver/browse.go
+4 -4
@@ -47,10 +47,10 @@ func (fsrv *FileServer) serveBrowse(root, dirPath string, w http.ResponseWriter,
47 // URL doesn't end in a trailing slash because hrefs like
48 // "/b/c" on a path like "/a" end up going to "/b/c" instead
49 // of "/a/b/c" - so we have to redirect in this case
50 - if !strings.HasSuffix(r.URL.Path, "/") {
51 - fsrv.logger.Debug("redirecting to trailing slash to preserve hrefs", zap.String("request_path", r.URL.Path))
52 - r.URL.Path += "/"
53 - http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently)
50 + oldReq := r.Context().Value(caddyhttp.OriginalRequestCtxKey).(http.Request)
51 + if !strings.HasSuffix(oldReq.URL.Path, "/") {
52 + fsrv.logger.Debug("redirecting to trailing slash to preserve hrefs", zap.String("request_path", oldReq.URL.Path))
53 + http.Redirect(w, r, oldReq.URL.Path+"/", http.StatusMovedPermanently)
54 return nil
55 }
56
modules/caddyhttp/fileserver/staticfiles.go
+8 -6
@@ -243,12 +243,14 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
243 // trailing slash - not enforcing this can break relative hrefs
244 // in HTML (see https://github.com/caddyserver/caddy/issues/2741)
245 if fsrv.CanonicalURIs == nil || *fsrv.CanonicalURIs {
246 - if implicitIndexFile && !strings.HasSuffix(r.URL.Path, "/") {
247 - fsrv.logger.Debug("redirecting to canonical URI (adding trailing slash for directory)", zap.String("path", r.URL.Path))
248 - return redirect(w, r, r.URL.Path+"/")
249 - } else if !implicitIndexFile && strings.HasSuffix(r.URL.Path, "/") {
250 - fsrv.logger.Debug("redirecting to canonical URI (removing trailing slash for file)", zap.String("path", r.URL.Path))
251 - return redirect(w, r, r.URL.Path[:len(r.URL.Path)-1])
246 + oldReq := r.Context().Value(caddyhttp.OriginalRequestCtxKey).(http.Request)
247 +
248 + if implicitIndexFile && !strings.HasSuffix(oldReq.URL.Path, "/") {
249 + fsrv.logger.Debug("redirecting to canonical URI (adding trailing slash for directory)", zap.String("path", oldReq.URL.Path))
250 + return redirect(w, r, oldReq.URL.Path+"/")
251 + } else if !implicitIndexFile && strings.HasSuffix(oldReq.URL.Path, "/") {
252 + fsrv.logger.Debug("redirecting to canonical URI (removing trailing slash for file)", zap.String("path", oldReq.URL.Path))
253 + return redirect(w, r, oldReq.URL.Path[:len(oldReq.URL.Path)-1])
254 }
255 }
256