Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 14:15 UTC
Drop requirement for filesystems to implement fs.StatFS
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Dave Henderson committed
Sep 5, 2022 at 19:25 UTC
fded2644f8ddda33002353ee3aedd15195f17cb5
5 files changed
+21
-20
modules/caddyhttp/fileserver/browse.go
+1
-1
@@ -210,7 +210,7 @@ func (fsrv *FileServer) isSymlinkTargetDir(f fs.FileInfo, root, urlPath string)
210
return false
211
}
212
target := caddyhttp.SanitizedPathJoin(root, path.Join(urlPath, f.Name()))
213
- targetInfo, err := fsrv.fileSystem.Stat(target)
213
+ targetInfo, err := fs.Stat(fsrv.fileSystem, target)
214
if err != nil {
215
return false
216
}
modules/caddyhttp/fileserver/browsetplcontext.go
+1
-1
@@ -65,7 +65,7 @@ func (fsrv *FileServer) directoryListing(entries []fs.DirEntry, canGoUp bool, ro
65
fileIsSymlink := isSymlink(info)
66
if fileIsSymlink {
67
path := caddyhttp.SanitizedPathJoin(root, path.Join(urlPath, info.Name()))
68
- fileInfo, err := fsrv.fileSystem.Stat(path)
68
+ fileInfo, err := fs.Stat(fsrv.fileSystem, path)
69
if err == nil {
70
size = fileInfo.Size()
71
}
modules/caddyhttp/fileserver/caddyfile.go
+3
-3
@@ -77,11 +77,11 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
77
if err != nil {
78
return nil, err
79
}
80
- statFS, ok := unm.(fs.StatFS)
80
+ fsys, ok := unm.(fs.FS)
81
if !ok {
82
- return nil, h.Errf("module %s (%T) is not a supported file system implementation (requires fs.StatFS)", modID, unm)
82
+ return nil, h.Errf("module %s (%T) is not a supported file system implementation (requires fs.FS)", modID, unm)
83
}
84
- fsrv.FileSystemRaw = caddyconfig.JSONModuleObject(statFS, "backend", name, nil)
84
+ fsrv.FileSystemRaw = caddyconfig.JSONModuleObject(fsys, "backend", name, nil)
85
86
case "hide":
87
fsrv.Hide = h.RemainingArgs()
modules/caddyhttp/fileserver/matcher.go
+6
-6
@@ -64,7 +64,7 @@ type MatchFile struct {
64
// The file system implementation to use. By default, the
65
// local disk file system will be used.
66
FileSystemRaw json.RawMessage `json:"file_system,omitempty" caddy:"namespace=caddy.fs inline_key=backend"`
67
- fileSystem fs.StatFS
67
+ fileSystem fs.FS
68
69
// The root directory, used for creating absolute
70
// file paths, and required when working with
@@ -264,7 +264,7 @@ func (m *MatchFile) Provision(ctx caddy.Context) error {
264
if err != nil {
265
return fmt.Errorf("loading file system module: %v", err)
266
}
267
- m.fileSystem = mod.(fs.StatFS)
267
+ m.fileSystem = mod.(fs.FS)
268
}
269
if m.fileSystem == nil {
270
m.fileSystem = osFS{}
@@ -418,7 +418,7 @@ func (m MatchFile) selectFile(r *http.Request) (matched bool) {
418
for _, pattern := range m.TryFiles {
419
candidates := makeCandidates(pattern)
420
for _, c := range candidates {
421
- info, err := m.fileSystem.Stat(c.fullpath)
421
+ info, err := fs.Stat(m.fileSystem, c.fullpath)
422
if err == nil && info.Size() > largestSize {
423
largestSize = info.Size()
424
largest = c
@@ -439,7 +439,7 @@ func (m MatchFile) selectFile(r *http.Request) (matched bool) {
439
for _, pattern := range m.TryFiles {
440
candidates := makeCandidates(pattern)
441
for _, c := range candidates {
442
- info, err := m.fileSystem.Stat(c.fullpath)
442
+ info, err := fs.Stat(m.fileSystem, c.fullpath)
443
if err == nil && (smallestSize == 0 || info.Size() < smallestSize) {
444
smallestSize = info.Size()
445
smallest = c
@@ -459,7 +459,7 @@ func (m MatchFile) selectFile(r *http.Request) (matched bool) {
459
for _, pattern := range m.TryFiles {
460
candidates := makeCandidates(pattern)
461
for _, c := range candidates {
462
- info, err := m.fileSystem.Stat(c.fullpath)
462
+ info, err := fs.Stat(m.fileSystem, c.fullpath)
463
if err == nil &&
464
(recentInfo == nil || info.ModTime().After(recentInfo.ModTime())) {
465
recent = c
@@ -498,7 +498,7 @@ func parseErrorCode(input string) error {
498
// NOT end in a forward slash, the file must NOT
499
// be a directory.
500
func (m MatchFile) strictFileExists(file string) (os.FileInfo, bool) {
501
- info, err := m.fileSystem.Stat(file)
501
+ info, err := fs.Stat(m.fileSystem, file)
502
if err != nil {
503
// in reality, this can be any error
504
// such as permission or even obscure
modules/caddyhttp/fileserver/staticfiles.go
+10
-9
@@ -83,14 +83,14 @@ type FileServer struct {
83
// disk file system.
84
//
85
// File system modules used here must adhere to the following requirements:
86
- // - Implement fs.StatFS interface.
86
+ // - Implement fs.FS interface.
87
// - Support seeking on opened files; i.e.returned fs.File values must
88
// implement the io.Seeker interface. This is required for determining
89
// Content-Length and satisfying Range requests.
90
// - fs.File values that represent directories must implement the
91
// fs.ReadDirFile interface so that directory listings can be procured.
92
FileSystemRaw json.RawMessage `json:"file_system,omitempty" caddy:"namespace=caddy.fs inline_key=backend"`
93
- fileSystem fs.StatFS
93
+ fileSystem fs.FS
94
95
// The path to the root of the site. Default is `{http.vars.root}` if set,
96
// or current working directory otherwise. This should be a trusted value.
@@ -175,7 +175,7 @@ func (fsrv *FileServer) Provision(ctx caddy.Context) error {
175
if err != nil {
176
return fmt.Errorf("loading file system module: %v", err)
177
}
178
- fsrv.fileSystem = mod.(fs.StatFS)
178
+ fsrv.fileSystem = mod.(fs.FS)
179
}
180
if fsrv.fileSystem == nil {
181
fsrv.fileSystem = osFS{}
@@ -244,7 +244,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
244
zap.String("result", filename))
245
246
// get information about the file
247
- info, err := fsrv.fileSystem.Stat(filename)
247
+ info, err := fs.Stat(fsrv.fileSystem, filename)
248
if err != nil {
249
err = fsrv.mapDirOpenError(err, filename)
250
if errors.Is(err, fs.ErrNotExist) {
@@ -270,7 +270,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
270
continue
271
}
272
273
- indexInfo, err := fsrv.fileSystem.Stat(indexPath)
273
+ indexInfo, err := fs.Stat(fsrv.fileSystem, indexPath)
274
if err != nil {
275
continue
276
}
@@ -350,7 +350,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
350
continue
351
}
352
compressedFilename := filename + precompress.Suffix()
353
- compressedInfo, err := fsrv.fileSystem.Stat(compressedFilename)
353
+ compressedInfo, err := fs.Stat(fsrv.fileSystem, compressedFilename)
354
if err != nil || compressedInfo.IsDir() {
355
fsrv.logger.Debug("precompressed file not accessible", zap.String("filename", compressedFilename), zap.Error(err))
356
continue
@@ -490,7 +490,7 @@ func (fsrv *FileServer) mapDirOpenError(originalErr error, name string) error {
490
if parts[i] == "" {
491
continue
492
}
493
- fi, err := fsrv.fileSystem.Stat(strings.Join(parts[:i+1], separator))
493
+ fi, err := fs.Stat(fsrv.fileSystem, strings.Join(parts[:i+1], separator))
494
if err != nil {
495
return originalErr
496
}
@@ -613,13 +613,13 @@ func (wr statusOverrideResponseWriter) WriteHeader(int) {
613
wr.ResponseWriter.WriteHeader(wr.code)
614
}
615
616
-// osFS is a simple fs.StatFS implementation that uses the local
616
+// osFS is a simple fs.FS implementation that uses the local
617
// file system. (We do not use os.DirFS because we do our own
618
// rooting or path prefixing without being constrained to a single
619
// root folder. The standard os.DirFS implementation is problematic
620
// since roots can be dynamic in our application.)
621
//
622
-// osFS also implements fs.GlobFS, fs.ReadDirFS, and fs.ReadFileFS.
622
+// osFS also implements fs.StatFS, fs.GlobFS, fs.ReadDirFS, and fs.ReadFileFS.
623
type osFS struct{}
624
625
func (osFS) Open(name string) (fs.File, error) { return os.Open(name) }
@@ -640,6 +640,7 @@ var (
640
_ caddy.Provisioner = (*FileServer)(nil)
641
_ caddyhttp.MiddlewareHandler = (*FileServer)(nil)
642
643
+ _ fs.StatFS = (*osFS)(nil)
644
_ fs.GlobFS = (*osFS)(nil)
645
_ fs.ReadDirFS = (*osFS)(nil)
646
_ fs.ReadFileFS = (*osFS)(nil)