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 | |
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
-rw-r--r-- | contrib/cgi/cgi_test.go | 2 | ||||
-rw-r--r-- | contrib/cgi/gemini.go | 8 | ||||
-rw-r--r-- | contrib/cgi/gopher.go | 32 | ||||
-rw-r--r-- | contrib/cgi/spartan.go | 54 | ||||
-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 | ||||
-rw-r--r-- | examples/cgi/main.go | 2 | ||||
-rw-r--r-- | examples/fileserver/main.go | 8 | ||||
-rw-r--r-- | examples/gopher_fileserver/main.go | 6 | ||||
-rw-r--r-- | gopher/gophermap/mdconv/convert_test.go | 2 | ||||
-rw-r--r-- | router.go | 25 |
17 files changed, 156 insertions, 420 deletions
diff --git a/contrib/cgi/cgi_test.go b/contrib/cgi/cgi_test.go index ff2c45d..5469fc8 100644 --- a/contrib/cgi/cgi_test.go +++ b/contrib/cgi/cgi_test.go @@ -21,7 +21,7 @@ func TestCGIDirectory(t *testing.T) { tlsconf, err := gemini.FileTLS("testdata/server.crt", "testdata/server.key") require.Nil(t, err) - handler := cgi.GeminiCGIDirectory("/cgi-bin", "./testdata") + handler := cgi.GeminiCGIDirectory("./testdata", "/cgi-bin") server, err := gemini.NewServer(context.Background(), "localhost", "tcp", "127.0.0.1:0", handler, nil, tlsconf) require.Nil(t, err) diff --git a/contrib/cgi/gemini.go b/contrib/cgi/gemini.go index 1e97939..3ad407d 100644 --- a/contrib/cgi/gemini.go +++ b/contrib/cgi/gemini.go @@ -17,14 +17,14 @@ 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 GeminiCGIDirectory(pathRoot, fsRoot string) sr.Handler { - fsRoot = strings.TrimRight(fsRoot, "/") +func GeminiCGIDirectory(fsroot, urlroot string) sr.Handler { + 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 } - filepath, pathinfo, err := ResolveCGI(request.Path[len(pathRoot):], fsRoot) + filepath, pathinfo, err := ResolveCGI(request.Path[len(urlroot):], fsroot) if err != nil { return gemini.Failure(err) } 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 diff --git a/contrib/cgi/spartan.go b/contrib/cgi/spartan.go deleted file mode 100644 index 272bd92..0000000 --- a/contrib/cgi/spartan.go +++ /dev/null @@ -1,54 +0,0 @@ -package cgi - -import ( - "bytes" - "context" - "fmt" - "strings" - - sr "tildegit.org/tjp/sliderule" - "tildegit.org/tjp/sliderule/logging" - "tildegit.org/tjp/sliderule/spartan" -) - -// SpartanCGIDirectory runs executable files relative to a root directory in the file system. -// -// It will also find any run any executable _part way_ through the path, so for example 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. -func SpartanCGIDirectory(pathRoot, fsRoot string) sr.Handler { - fsRoot = strings.TrimRight(fsRoot, "/") - return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - if !strings.HasPrefix(request.Path, pathRoot) { - return nil - } - - filepath, pathinfo, err := ResolveCGI(request.Path[len(pathRoot):], fsRoot) - if err != nil { - return spartan.ServerError(err) - } - if filepath == "" { - return nil - } - - stderr := &bytes.Buffer{} - stdout, exitCode, err := RunCGI(ctx, request, filepath, pathinfo, stderr) - if err != nil { - return spartan.ServerError(err) - } - if exitCode != 0 { - ctx.Value("warnlog").(logging.Logger).Log( - "msg", "cgi exited with non-zero exit code", - "code", exitCode, - "stderr", stderr.String(), - ) - return spartan.ServerError(fmt.Errorf("CGI process exited with status %d", exitCode)) - } - - response, err := spartan.ParseResponse(stdout) - if err != nil { - return spartan.ServerError(err) - } - return response - }) -} 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 -} diff --git a/examples/cgi/main.go b/examples/cgi/main.go index a582c9e..4d48422 100644 --- a/examples/cgi/main.go +++ b/examples/cgi/main.go @@ -23,7 +23,7 @@ func main() { } // make use of a CGI request handler - cgiHandler := cgi.GeminiCGIDirectory("/cgi-bin", "./cgi-bin") + cgiHandler := cgi.GeminiCGIDirectory("./cgi-bin", "/cgi-bin") _, infoLog, _, errLog := logging.DefaultLoggers() diff --git a/examples/fileserver/main.go b/examples/fileserver/main.go index c374cc4..e90fdd9 100644 --- a/examples/fileserver/main.go +++ b/examples/fileserver/main.go @@ -21,16 +21,14 @@ func main() { log.Fatal(err) } - // build the request handler - fileSystem := os.DirFS(".") // Fallthrough tries each handler in succession until it gets something other than "51 Not Found" handler := sr.FallthroughHandler( // first see if they're fetching a directory and we have <dir>/index.gmi - fs.GeminiDirectoryDefault(fileSystem, "index.gmi"), + fs.GeminiDirectoryDefault(".", "", "index.gmi"), // next (still if they requested a directory) build a directory listing response - fs.GeminiDirectoryListing(fileSystem, nil), + fs.GeminiDirectoryListing(".", "", nil), // finally, try to find a file at the request path and respond with that - fs.GeminiFileHandler(fileSystem), + fs.GeminiFileHandler(".", ""), ) router := &sr.Router{} diff --git a/examples/gopher_fileserver/main.go b/examples/gopher_fileserver/main.go index 057aa21..1cb7495 100644 --- a/examples/gopher_fileserver/main.go +++ b/examples/gopher_fileserver/main.go @@ -13,10 +13,10 @@ import ( func main() { handler := sr.FallthroughHandler( - fs.GopherDirectoryDefault(".", nil), - fs.GopherDirectoryListing(".", nil), + fs.GopherDirectoryDefault(".", "", nil), + fs.GopherDirectoryListing(".", "", nil), cgi.GopherCGIDirectory("/cgi-bin", "./cgi-bin", nil), - fs.GopherFileHandler(".", nil), + fs.GopherFileHandler(".", "", nil), ) _, infoLog, _, errLog := logging.DefaultLoggers() diff --git a/gopher/gophermap/mdconv/convert_test.go b/gopher/gophermap/mdconv/convert_test.go index 2e39106..7c2ab23 100644 --- a/gopher/gophermap/mdconv/convert_test.go +++ b/gopher/gophermap/mdconv/convert_test.go @@ -32,13 +32,11 @@ I am informational text continued on this line ` + "```" + ` - [this is my text file](/file.txt) ` + "```" + ` ` + "```" + ` - [here's a sub-menu](/sub/) `)[1:], @@ -39,20 +39,25 @@ func (r *Router) Route(pattern string, handler Handler) { r.routeAdded = true } -// Handler builds a Handler which matches the request path and dispatches to a route. +// Handle implements Handler // -// If no route matches, the handler returns a nil response. +// If no route matches, Handle returns a nil response. // Captured path parameters will be stored in the context passed into the handler // and can be retrieved with RouteParams(). -func (r Router) Handler() Handler { - return HandlerFunc(func(ctx context.Context, request *Request) *Response { - handler, params := r.Match(request) - if handler == nil { - return nil - } +func (r Router) Handle(ctx context.Context, request *Request) *Response { + handler, params := r.Match(request) + if handler == nil { + return nil + } - return handler.Handle(context.WithValue(ctx, routeParamsKey, params), request) - }) + return handler.Handle(context.WithValue(ctx, routeParamsKey, params), request) +} + +// Handler builds a Handler +// +// It is only here for compatibility because Router implements Handler directly. +func (r Router) Handler() Handler { + return r } // Match returns the matched handler and captured path parameters, or (nil, nil). |