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/cgi/gopher.go | |
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/cgi/gopher.go')
-rw-r--r-- | contrib/cgi/gopher.go | 32 |
1 files changed, 12 insertions, 20 deletions
diff --git a/contrib/cgi/gopher.go b/contrib/cgi/gopher.go index 2f90f22..bb3e73e 100644 --- a/contrib/cgi/gopher.go +++ b/contrib/cgi/gopher.go @@ -21,24 +21,19 @@ import ( // a request for /foo/bar/baz can also run an executable found at /foo or /foo/bar. In // such a case the PATH_INFO environment variable will include the remaining portion of // the URI path. -func GopherCGIDirectory(pathRoot, fsRoot string, settings *gophermap.FileSystemSettings) sr.Handler { - if settings == nil { - settings = &gophermap.FileSystemSettings{} - } - - if !settings.Exec { +func GopherCGIDirectory(fsroot, urlroot string, settings *gophermap.FileSystemSettings) sr.Handler { + if settings == nil || !settings.Exec { return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { return nil }) } + fsroot = strings.TrimRight(fsroot, "/") - fsRoot = strings.TrimRight(fsRoot, "/") return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - if !strings.HasPrefix(request.Path, pathRoot) { + if !strings.HasPrefix(request.Path, urlroot) { return nil } + requestpath := strings.Trim(strings.TrimPrefix(request.Path, urlroot), "/") - requestPath := strings.Trim(strings.TrimPrefix(request.Path, pathRoot), "/") - - fullpath, pathinfo, err := resolveGopherCGI(fsRoot, requestPath) + fullpath, pathinfo, err := resolveGopherCGI(fsroot, requestpath) if err != nil { return gopher.Error(err).Response() } @@ -51,22 +46,19 @@ func GopherCGIDirectory(pathRoot, fsRoot string, settings *gophermap.FileSystemS } // ExecGopherMaps runs any gophermaps -func ExecGopherMaps(pathRoot, fsRoot string, settings *gophermap.FileSystemSettings) sr.Handler { - if settings == nil { - settings = &gophermap.FileSystemSettings{} - } - - if !settings.Exec { +func ExecGopherMaps(fsroot, urlroot string, settings *gophermap.FileSystemSettings) sr.Handler { + if settings == nil || !settings.Exec { return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { return nil }) } + fsroot = strings.TrimRight(fsroot, "/") - fsRoot = strings.TrimRight(fsRoot, "/") return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - if !strings.HasPrefix(request.Path, pathRoot) { + if !strings.HasPrefix(request.Path, urlroot) { return nil } + requestpath := strings.Trim(strings.TrimPrefix(request.Path, urlroot), "/") - fullpath := filepath.Join(fsRoot, strings.Trim(request.Path, "/")) + fullpath := filepath.Join(fsroot, requestpath) info, err := os.Stat(fullpath) if isNotExistError(err) { return nil |