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: Preserve transformed root (fix #3838)
Matthew Holt committed
Nov 24, 2020 at 12:24 UTC
0a7721dcfe30b76c66c6364ccb6874a2eadcba21
3 files changed
+8
-8
modules/caddyhttp/fileserver/browse.go
+5
-5
@@ -35,7 +35,7 @@ type Browse struct {
35
template *template.Template
36
}
37
38
-func (fsrv *FileServer) serveBrowse(dirPath string, w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
38
+func (fsrv *FileServer) serveBrowse(root, dirPath string, w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
39
// navigation on the client-side gets messed up if the
40
// URL doesn't end in a trailing slash because hrefs like
41
// "/b/c" on a path like "/a" end up going to "/b/c" instead
@@ -55,7 +55,7 @@ func (fsrv *FileServer) serveBrowse(dirPath string, w http.ResponseWriter, r *ht
55
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
56
57
// calling path.Clean here prevents weird breadcrumbs when URL paths are sketchy like /%2e%2e%2f
58
- listing, err := fsrv.loadDirectoryContents(dir, path.Clean(r.URL.Path), repl)
58
+ listing, err := fsrv.loadDirectoryContents(dir, root, path.Clean(r.URL.Path), repl)
59
switch {
60
case os.IsPermission(err):
61
return caddyhttp.Error(http.StatusForbidden, err)
@@ -87,7 +87,7 @@ func (fsrv *FileServer) serveBrowse(dirPath string, w http.ResponseWriter, r *ht
87
return nil
88
}
89
90
-func (fsrv *FileServer) loadDirectoryContents(dir *os.File, urlPath string, repl *caddy.Replacer) (browseListing, error) {
90
+func (fsrv *FileServer) loadDirectoryContents(dir *os.File, root, urlPath string, repl *caddy.Replacer) (browseListing, error) {
91
files, err := dir.Readdir(-1)
92
if err != nil {
93
return browseListing{}, err
@@ -95,9 +95,9 @@ func (fsrv *FileServer) loadDirectoryContents(dir *os.File, urlPath string, repl
95
96
// determine if user can browse up another folder
97
curPathDir := path.Dir(strings.TrimSuffix(urlPath, "/"))
98
- canGoUp := strings.HasPrefix(curPathDir, fsrv.Root)
98
+ canGoUp := strings.HasPrefix(curPathDir, root)
99
100
- return fsrv.directoryListing(files, canGoUp, urlPath, repl), nil
100
+ return fsrv.directoryListing(files, canGoUp, root, urlPath, repl), nil
101
}
102
103
// browseApplyQueryParams applies query parameters to the listing.
modules/caddyhttp/fileserver/browselisting.go
+2
-2
@@ -27,7 +27,7 @@ import (
27
"github.com/dustin/go-humanize"
28
)
29
30
-func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, urlPath string, repl *caddy.Replacer) browseListing {
30
+func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, root, urlPath string, repl *caddy.Replacer) browseListing {
31
filesToHide := fsrv.transformHidePaths(repl)
32
33
var dirCount, fileCount int
@@ -40,7 +40,7 @@ func (fsrv *FileServer) directoryListing(files []os.FileInfo, canGoUp bool, urlP
40
continue
41
}
42
43
- isDir := f.IsDir() || isSymlinkTargetDir(f, fsrv.Root, urlPath)
43
+ isDir := f.IsDir() || isSymlinkTargetDir(f, root, urlPath)
44
45
if isDir {
46
name += "/"
modules/caddyhttp/fileserver/staticfiles.go
+1
-1
@@ -184,7 +184,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
184
// to browse or return an error
185
if info.IsDir() {
186
if fsrv.Browse != nil && !fileHidden(filename, filesToHide) {
187
- return fsrv.serveBrowse(filename, w, r, next)
187
+ return fsrv.serveBrowse(root, filename, w, r, next)
188
}
189
return fsrv.notFound(w, r, next)
190
}