diff options
Diffstat (limited to 'contrib/fs/gopher.go')
-rw-r--r-- | contrib/fs/gopher.go | 33 |
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 } |