From 775c0c1040e6a6622fec39d49b354bfa194a6998 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Sat, 30 Sep 2023 20:08:33 -0600 Subject: file serving refactor * do away with fs.FS usage in gemini, like the previous refactor in gopher * remove spartan code in contrib * standardize fsroot/urlroot string arguments to file serving handlers --- contrib/fs/gopher.go | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'contrib/fs/gopher.go') diff --git a/contrib/fs/gopher.go b/contrib/fs/gopher.go index 4d86ba6..db21227 100644 --- a/contrib/fs/gopher.go +++ b/contrib/fs/gopher.go @@ -15,9 +15,16 @@ import ( // GopherFileHandler builds a handler which serves up files from a file system. // // It only serves responses for paths which correspond to files, not directories. -func GopherFileHandler(rootpath string, settings *gophermap.FileSystemSettings) sr.Handler { +func GopherFileHandler(fsroot, urlroot string, settings *gophermap.FileSystemSettings) sr.Handler { + fsroot = strings.TrimRight(fsroot, "/") + return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - path := filepath.Join(rootpath, strings.Trim(request.Path, "/")) + if !strings.HasPrefix(request.Path, urlroot) { + return nil + } + requestpath := strings.Trim(strings.TrimPrefix(request.Path, urlroot), "/") + + path := filepath.Join(fsroot, requestpath) if isPrivate(path) { return nil } @@ -61,9 +68,16 @@ func GopherFileHandler(rootpath string, settings *gophermap.FileSystemSettings) // contents of that file is returned as the gopher response. // // It returns nil for any paths which don't correspond to a directory. -func GopherDirectoryDefault(rootpath string, settings *gophermap.FileSystemSettings) sr.Handler { +func GopherDirectoryDefault(fsroot, urlroot string, settings *gophermap.FileSystemSettings) sr.Handler { + fsroot = strings.TrimRight(fsroot, "/") + return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - path := filepath.Join(rootpath, strings.Trim(request.Path, "/")) + if !strings.HasPrefix(request.Path, urlroot) { + return nil + } + requestpath := strings.Trim(strings.TrimPrefix(request.Path, urlroot), "/") + + path := filepath.Join(fsroot, requestpath) if isPrivate(path) { return nil } @@ -115,9 +129,16 @@ func GopherDirectoryDefault(rootpath string, settings *gophermap.FileSystemSetti // GopherDirectoryListing produces a listing of the contents of any requested directories. // // It returns nil for any paths which don't correspond to a filesystem directory. -func GopherDirectoryListing(rootpath string, settings *gophermap.FileSystemSettings) sr.Handler { +func GopherDirectoryListing(fsroot, urlroot string, settings *gophermap.FileSystemSettings) sr.Handler { + fsroot = strings.TrimRight(fsroot, "/") + return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - path := filepath.Join(rootpath, strings.Trim(request.Path, "/")) + if !strings.HasPrefix(request.Path, urlroot) { + return nil + } + requestpath := strings.Trim(strings.TrimPrefix(request.Path, urlroot), "/") + + path := filepath.Join(fsroot, requestpath) if isPrivate(path) { return nil } -- cgit v1.2.3