summaryrefslogtreecommitdiff
path: root/contrib/fs/gopher.go
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-09-30 20:08:33 -0600
committertjpcc <tjp@ctrl-c.club>2023-09-30 20:08:53 -0600
commit775c0c1040e6a6622fec39d49b354bfa194a6998 (patch)
treef46edde7ee0392ae714f4facfd4e64244814c040 /contrib/fs/gopher.go
parent09c482d5016cfc7b628058893a1644fdf5fa699f (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/gopher.go')
-rw-r--r--contrib/fs/gopher.go33
1 files changed, 27 insertions, 6 deletions
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
}