diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-09-30 20:08:33 -0600 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-09-30 20:08:53 -0600 |
commit | 775c0c1040e6a6622fec39d49b354bfa194a6998 (patch) | |
tree | f46edde7ee0392ae714f4facfd4e64244814c040 /contrib/fs | |
parent | 09c482d5016cfc7b628058893a1644fdf5fa699f (diff) |
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
Diffstat (limited to 'contrib/fs')
-rw-r--r-- | contrib/fs/dir.go | 89 | ||||
-rw-r--r-- | contrib/fs/dir_test.go | 5 | ||||
-rw-r--r-- | contrib/fs/file.go | 37 | ||||
-rw-r--r-- | contrib/fs/file_test.go | 3 | ||||
-rw-r--r-- | contrib/fs/gemini.go | 138 | ||||
-rw-r--r-- | contrib/fs/gopher.go | 33 | ||||
-rw-r--r-- | contrib/fs/spartan.go | 124 | ||||
-rw-r--r-- | contrib/fs/stat.go | 8 |
8 files changed, 117 insertions, 320 deletions
diff --git a/contrib/fs/dir.go b/contrib/fs/dir.go index b00fe5c..e43a375 100644 --- a/contrib/fs/dir.go +++ b/contrib/fs/dir.go @@ -3,7 +3,7 @@ package fs import ( "bytes" "io" - "io/fs" + "os" "sort" "strings" "text/template" @@ -11,83 +11,6 @@ import ( sr "tildegit.org/tjp/sliderule" ) -// ResolveDirectory opens the directory corresponding to a request path. -// -// The string is the full path to the directory. If the returned ReadDirFile -// is not nil, it will be open and must be closed by the caller. -func ResolveDirectory( - request *sr.Request, - fileSystem fs.FS, -) (string, fs.ReadDirFile, error) { - path := strings.Trim(request.Path, "/") - if path == "" { - path = "." - } - - if isPrivate(path) { - return "", nil, nil - } - - file, err := fileSystem.Open(path) - if isNotFound(err) { - return "", nil, nil - } - if err != nil { - return "", nil, err - } - - isDir, err := fileIsDir(file) - if err != nil { - _ = file.Close() - return "", nil, err - } - - if !isDir { - _ = file.Close() - return "", nil, nil - } - - dirFile, ok := file.(fs.ReadDirFile) - if !ok { - _ = file.Close() - return "", nil, nil - } - - return path, dirFile, nil -} - -// ResolveDirectoryDefault finds any of the provided filenames within a directory. -// -// If it does not find any of the filenames it returns "", nil, nil. -func ResolveDirectoryDefault( - fileSystem fs.FS, - dirPath string, - dir fs.ReadDirFile, - filenames []string, -) (string, fs.File, error) { - entries, err := dir.ReadDir(0) - if err != nil { - return "", nil, err - } - sort.Slice(entries, func(a, b int) bool { - return entries[a].Name() < entries[b].Name() - }) - - for _, filename := range filenames { - idx := sort.Search(len(entries), func(i int) bool { - return entries[i].Name() >= filename - }) - - if idx < len(entries) && entries[idx].Name() == filename { - path := strings.TrimLeft(dirPath+"/"+filename, "./") - file, err := fileSystem.Open(path) - return path, file, err - } - } - - return "", nil, nil -} - // RenderDirectoryListing provides an io.Reader with the output of a directory listing template. // // The template is provided the following namespace: @@ -104,13 +27,13 @@ func ResolveDirectoryDefault( // - .Info is a method returning (fs.FileInfo, error) func RenderDirectoryListing( path string, - dir fs.ReadDirFile, + requestpath string, template *template.Template, server sr.Server, ) (io.Reader, error) { buf := &bytes.Buffer{} - environ, err := dirlistNamespace(path, dir, server) + environ, err := dirlistNamespace(path, requestpath, server) if err != nil { return nil, err } @@ -122,8 +45,8 @@ func RenderDirectoryListing( return buf, nil } -func dirlistNamespace(path string, dirFile fs.ReadDirFile, server sr.Server) (map[string]any, error) { - entries, err := dirFile.ReadDir(0) +func dirlistNamespace(path, requestpath string, server sr.Server) (map[string]any, error) { + entries, err := os.ReadDir(path) if err != nil { return nil, err } @@ -140,7 +63,7 @@ func dirlistNamespace(path string, dirFile fs.ReadDirFile, server sr.Server) (ma }) var dirname string - if path == "." { + if requestpath == "" { dirname = "(root)" } else { dirname = path[strings.LastIndex(path, "/")+1:] diff --git a/contrib/fs/dir_test.go b/contrib/fs/dir_test.go index 6b6f60f..a6b95cb 100644 --- a/contrib/fs/dir_test.go +++ b/contrib/fs/dir_test.go @@ -4,7 +4,6 @@ import ( "context" "io" "net/url" - "os" "testing" "github.com/stretchr/testify/assert" @@ -16,7 +15,7 @@ import ( ) func TestDirectoryDefault(t *testing.T) { - handler := fs.GeminiDirectoryDefault(os.DirFS("testdata"), "index.gmi") + handler := fs.GeminiDirectoryDefault("testdata", "", "index.gmi") tests := []struct { url string @@ -69,7 +68,7 @@ func TestDirectoryDefault(t *testing.T) { } func TestDirectoryListing(t *testing.T) { - handler := fs.GeminiDirectoryListing(os.DirFS("testdata"), nil) + handler := fs.GeminiDirectoryListing("testdata", "", nil) tests := []struct { url string diff --git a/contrib/fs/file.go b/contrib/fs/file.go index d231466..7690a62 100644 --- a/contrib/fs/file.go +++ b/contrib/fs/file.go @@ -1,47 +1,10 @@ package fs import ( - "io/fs" "mime" "strings" - - sr "tildegit.org/tjp/sliderule" ) -// ResolveFile finds a file from a filesystem based on a request path. -// -// It only returns a non-nil file if a file is found - not a directory. -// If there is any other sort of filesystem access error, it will be -// returned. -func ResolveFile(request *sr.Request, fileSystem fs.FS) (string, fs.File, error) { - filepath := strings.TrimPrefix(request.Path, "/") - - if isPrivate(filepath) { - return "", nil, nil - } - - file, err := fileSystem.Open(filepath) - if isNotFound(err) { - return "", nil, nil - } - if err != nil { - return "", nil, err - } - - isDir, err := fileIsDir(file) - if err != nil { - _ = file.Close() - return "", nil, err - } - - if isDir { - _ = file.Close() - return "", nil, nil - } - - return filepath, file, nil -} - func mediaType(filePath string) string { if strings.HasSuffix(filePath, ".gmi") { // This may not be present in the listings searched by mime.TypeByExtension, diff --git a/contrib/fs/file_test.go b/contrib/fs/file_test.go index 55e2a09..ce4d023 100644 --- a/contrib/fs/file_test.go +++ b/contrib/fs/file_test.go @@ -4,7 +4,6 @@ import ( "context" "io" "net/url" - "os" "testing" "github.com/stretchr/testify/assert" @@ -16,7 +15,7 @@ import ( ) func TestFileHandler(t *testing.T) { - handler := fs.GeminiFileHandler(os.DirFS("testdata")) + handler := fs.GeminiFileHandler("testdata", "") tests := []struct { url string diff --git a/contrib/fs/gemini.go b/contrib/fs/gemini.go index 7549ce6..d0ad2d8 100644 --- a/contrib/fs/gemini.go +++ b/contrib/fs/gemini.go @@ -4,10 +4,10 @@ import ( "context" "crypto/tls" "io" - "io/fs" "net/url" "os" "path" + "path/filepath" "strings" "text/template" @@ -20,11 +20,15 @@ import ( // // It is a middleware rather than a handler because after the upload is processed, // the server is still responsible for generating a response. -func TitanUpload(approver tlsauth.Approver, rootdir string) sr.Middleware { - rootdir = strings.TrimSuffix(rootdir, "/") +func TitanUpload(fsroot, urlroot string, approver tlsauth.Approver) sr.Middleware { + fsroot = strings.TrimSuffix(fsroot, "/") return func(responder sr.Handler) sr.Handler { handler := sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { + if !strings.HasPrefix(request.Path, urlroot) { + return nil + } + body := gemini.GetTitanRequestBody(request) tmpf, err := os.CreateTemp("", "titan_upload_") @@ -40,8 +44,8 @@ func TitanUpload(approver tlsauth.Approver, rootdir string) sr.Middleware { request = cloneRequest(request) request.Path = strings.SplitN(request.Path, ";", 2)[0] - filepath := strings.TrimPrefix(request.Path, "/") - filepath = path.Join(rootdir, filepath) + filepath := strings.Trim(strings.TrimPrefix(request.Path, urlroot), "/") + filepath = path.Join(fsroot, filepath) if err := os.Rename(tmpf.Name(), filepath); err != nil { _ = os.Remove(tmpf.Name()) return gemini.PermanentFailure(err) @@ -75,21 +79,33 @@ func cloneRequest(start *sr.Request) *sr.Request { return next } -// GeminiFileHandler builds a handler which serves up files from a file system. +// GeminiFileHandler builds a handler which serves up files from the file system. // // It only serves responses for paths which do not correspond to directories on disk. -func GeminiFileHandler(fileSystem fs.FS) sr.Handler { +func GeminiFileHandler(fsroot, urlroot string) sr.Handler { + fsroot = strings.TrimRight(fsroot, "/") + return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - filepath, file, err := ResolveFile(request, fileSystem) - if err != nil { - return gemini.Failure(err) + if !strings.HasPrefix(request.Path, urlroot) { + return nil } + requestpath := strings.Trim(strings.TrimPrefix(request.Path, urlroot), "/") - if file == nil { + fpath := filepath.Join(fsroot, requestpath) + if isPrivate(fpath) { + return nil + } + if isf, err := isFile(fpath); err != nil { + return gemini.Failure(err) + } else if !isf { return nil } - return gemini.Success(mediaType(filepath), file) + file, err := os.Open(fpath) + if err != nil { + return gemini.Failure(err) + } + return gemini.Success(mediaType(fpath), file) }) } @@ -102,30 +118,48 @@ func GeminiFileHandler(fileSystem fs.FS) sr.Handler { // // When it encounters a directory path which doesn't end in a trailing slash (/) it // redirects to a URL with the trailing slash appended. This is necessary for relative -// links not the directory's contents to function properly. -// -// It requires that files from the provided fs.FS implement fs.ReadDirFile. If they -// don't, it will produce nil responses for any directory paths. -func GeminiDirectoryDefault(fileSystem fs.FS, filenames ...string) sr.Handler { +// links in the directory's contents to function properly. +func GeminiDirectoryDefault(fsroot, urlroot string, filenames ...string) sr.Handler { + fsroot = strings.TrimRight(fsroot, "/") + return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - dirpath, dir, response := handleDirGemini(request, fileSystem) - if response != nil { - return response + if !strings.HasSuffix(request.Path, "/") { + u := *request.URL + u.Path += "/" + return gemini.PermanentRedirect(u.String()) } - if dir == nil { + + if !strings.HasPrefix(request.Path, urlroot) { return nil } - defer func() { _ = dir.Close() }() + requestpath := strings.Trim(strings.TrimPrefix(request.Path, urlroot), "/") - filepath, file, err := ResolveDirectoryDefault(fileSystem, dirpath, dir, filenames) - if err != nil { - return gemini.Failure(err) + fpath := filepath.Join(fsroot, requestpath) + if isPrivate(fpath) { + return nil } - if file == nil { + if isd, err := isDir(fpath); err != nil { + return gemini.Failure(err) + } else if !isd { return nil } - return gemini.Success(mediaType(filepath), file) + for _, fname := range filenames { + candidatepath := filepath.Join(fpath, fname) + if isf, err := isFile(candidatepath); err != nil { + return gemini.Failure(err) + } else if !isf { + continue + } + + file, err := os.Open(candidatepath) + if err != nil { + return gemini.Failure(err) + } + return gemini.Success(mediaType(candidatepath), file) + } + + return nil }) } @@ -137,26 +171,36 @@ func GeminiDirectoryDefault(fileSystem fs.FS, filenames ...string) sr.Handler { // redirects to a URL with the trailing slash appended. This is necessary for relative // links not the directory's contents to function properly. // -// It requires that files from the provided fs.FS implement fs.ReadDirFile. If they -// don't, it will produce "51 Not Found" responses for any directory paths. -// // The template may be nil, in which case DefaultGeminiDirectoryList is used instead. The // template is then processed with RenderDirectoryListing. -func GeminiDirectoryListing(fileSystem fs.FS, template *template.Template) sr.Handler { +func GeminiDirectoryListing(fsroot, urlroot string, template *template.Template) sr.Handler { + fsroot = strings.TrimRight(fsroot, "/") + return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - dirpath, dir, response := handleDirGemini(request, fileSystem) - if response != nil { - return response + if !strings.HasSuffix(request.Path, "/") { + u := *request.URL + u.Path += "/" + return gemini.PermanentRedirect(u.String()) } - if dir == nil { + if !strings.HasPrefix(request.Path, urlroot) { + return nil + } + requestpath := strings.Trim(strings.TrimPrefix(request.Path, urlroot), "/") + + fpath := filepath.Join(fsroot, requestpath) + if isPrivate(fpath) { + return nil + } + if isd, err := isDir(fpath); err != nil { + return gemini.Failure(err) + } else if !isd { return nil } - defer func() { _ = dir.Close() }() if template == nil { template = DefaultGeminiDirectoryList } - body, err := RenderDirectoryListing(dirpath, dir, template, request.Server) + body, err := RenderDirectoryListing(fpath, requestpath, template, request.Server) if err != nil { return gemini.Failure(err) } @@ -173,23 +217,3 @@ var DefaultGeminiDirectoryList = template.Must(template.New("gemini_dirlist").Pa {{ end }} => ../ `[1:])) - -func handleDirGemini(request *sr.Request, fileSystem fs.FS) (string, fs.ReadDirFile, *sr.Response) { - path, dir, err := ResolveDirectory(request, fileSystem) - if err != nil { - return "", nil, gemini.Failure(err) - } - - if dir == nil { - return "", nil, nil - } - - if !strings.HasSuffix(request.Path, "/") { - _ = dir.Close() - url := *request.URL - url.Path += "/" - return "", nil, gemini.PermanentRedirect(url.String()) - } - - return path, dir, nil -} 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 } diff --git a/contrib/fs/spartan.go b/contrib/fs/spartan.go deleted file mode 100644 index 70943ee..0000000 --- a/contrib/fs/spartan.go +++ /dev/null @@ -1,124 +0,0 @@ -package fs - -import ( - "context" - "io/fs" - "strings" - "text/template" - - sr "tildegit.org/tjp/sliderule" - "tildegit.org/tjp/sliderule/spartan" -) - -// SpartanFileHandler builds a handler which serves up files from a filesystem. -// -// It only serves responses for paths which do not correspond to directories on disk. -func SpartanFileHandler(fileSystem fs.FS) sr.Handler { - return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - filepath, file, err := ResolveFile(request, fileSystem) - if err != nil { - return spartan.ClientError(err) - } - - if file == nil { - return nil - } - - return spartan.Success(mediaType(filepath), file) - }) -} - -// SpartanDirectoryDefault serves up default files for directory path requests. -// -// If any of the supported filenames are found, the contents of the file is returned -// as the spartan response. -// -// It returns nil for any paths which don't correspond to a directory. -// -// When it encounters a directory path which doesn't end in a trailing slash (/) it -// redirects to the same URL with the slash appended. This is necessary for relative -// links not in the directory's contents to function properly. -// -// It requires that files from the provided fs.FS implement fs.ReadDirFile. If they -// don't, it will produce nil responses for any directory paths. -func SpartanDirectoryDefault(fileSystem fs.FS, filenames ...string) sr.Handler { - return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - dirpath, dir, response := handleDirSpartan(request, fileSystem) - if response != nil { - return response - } - if dir == nil { - return nil - } - defer func() { _ = dir.Close() }() - - filepath, file, err := ResolveDirectoryDefault(fileSystem, dirpath, dir, filenames) - if err != nil { - return spartan.ServerError(err) - } - if file == nil { - return nil - } - - return spartan.Success(mediaType(filepath), file) - }) -} - -// SpartanDirectoryListing produces a listing of the contents of any requested directories. -// -// It returns "4 Resource not found" for any paths which don't correspond to a filesystem directory. -// -// When it encounters a directory path which doesn't end in a trailing slash (/) it redirects to a -// URL with the trailing slash appended. This is necessary for relative links not in the directory's -// contents to function properly. -// -// It requires that files provided by the fs.FS implement fs.ReadDirFile. If they don't, it will -// produce "4 Resource not found" responses for any directory paths. -// -// The tmeplate may be nil, in which cause DefaultSpartanDirectoryList is used instead. The -// template is then processed with RenderDirectoryListing. -func SpartanDirectoryListing(filesystem fs.FS, template *template.Template) sr.Handler { - return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - dirpath, dir, response := handleDirSpartan(request, filesystem) - if response != nil { - return response - } - if dir == nil { - return nil - } - defer func() { _ = dir.Close() }() - - if template == nil { - template = DefaultSpartanDirectoryList - } - body, err := RenderDirectoryListing(dirpath, dir, template, request.Server) - if err != nil { - return spartan.ServerError(err) - } - - return spartan.Success("text/gemini", body) - }) -} - -// DefaultSpartanDirectoryList is a template which renders a reasonable gemtext dir listing. -var DefaultSpartanDirectoryList = DefaultGeminiDirectoryList - -func handleDirSpartan(request *sr.Request, filesystem fs.FS) (string, fs.ReadDirFile, *sr.Response) { - path, dir, err := ResolveDirectory(request, filesystem) - if err != nil { - return "", nil, spartan.ServerError(err) - } - - if dir == nil { - return "", nil, nil - } - - if !strings.HasSuffix(request.Path, "/") { - _ = dir.Close() - url := *request.URL - url.Path += "/" - return "", nil, spartan.Redirect(url.String()) - } - - return path, dir, nil -} diff --git a/contrib/fs/stat.go b/contrib/fs/stat.go index 4dd65d8..78b198f 100644 --- a/contrib/fs/stat.go +++ b/contrib/fs/stat.go @@ -18,11 +18,3 @@ func isNotFound(err error) bool { return false } - -func fileIsDir(file fs.File) (bool, error) { - info, err := file.Stat() - if err != nil { - return false, err - } - return info.IsDir(), nil -} |