From b95b87381a282e1fe57295d145b71645d7801f07 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Wed, 16 Sep 2020 20:09:28 -0400 Subject: fileserver: Fix try_files for directories; windows fix (#3684) * fileserver: Fix try_files for directories, windows fix * fileserver: Add new file type placeholder, refactoring, tests * fileserver: Review cleanup * fileserver: Flip the return args order --- modules/caddyhttp/fileserver/staticfiles.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'modules/caddyhttp/fileserver/staticfiles.go') diff --git a/modules/caddyhttp/fileserver/staticfiles.go b/modules/caddyhttp/fileserver/staticfiles.go index 735352b..2a7e013 100644 --- a/modules/caddyhttp/fileserver/staticfiles.go +++ b/modules/caddyhttp/fileserver/staticfiles.go @@ -23,7 +23,6 @@ import ( "mime" "net/http" "os" - "path" "path/filepath" "strconv" "strings" @@ -323,7 +322,18 @@ func sanitizedPathJoin(root, reqPath string) string { if root == "" { root = "." } - return filepath.Join(root, filepath.FromSlash(path.Clean("/"+reqPath))) + + path := filepath.Join(root, filepath.Clean("/"+reqPath)) + + // filepath.Join also cleans the path, and cleaning strips + // the trailing slash, so we need to re-add it afterwards. + // if the length is 1, then it's a path to the root, + // and that should return ".", so we don't append the separator. + if strings.HasSuffix(reqPath, "/") && len(reqPath) > 1 { + path += string(filepath.Separator) + } + + return path } // fileHidden returns true if filename is hidden -- cgit v1.2.3