From 2ef530daa47b301a40c1ee93cd43b8f36fc68c0b Mon Sep 17 00:00:00 2001 From: tjpcc Date: Tue, 17 Jan 2023 15:59:29 -0700 Subject: pull request, response, handlers out of the gemini package --- contrib/cgi/cgi.go | 15 ++++++++------- contrib/fs/dir.go | 23 ++++++++++++----------- contrib/fs/file.go | 9 +++++---- contrib/log/log.go | 8 ++++---- 4 files changed, 29 insertions(+), 26 deletions(-) (limited to 'contrib') diff --git a/contrib/cgi/cgi.go b/contrib/cgi/cgi.go index e43f1ef..7f88e57 100644 --- a/contrib/cgi/cgi.go +++ b/contrib/cgi/cgi.go @@ -13,6 +13,7 @@ import ( "os/exec" "strings" + "tildegit.org/tjp/gus" "tildegit.org/tjp/gus/gemini" ) @@ -22,12 +23,12 @@ 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 CGIDirectory(pathRoot, fsRoot string) gemini.Handler { +func CGIDirectory(pathRoot, fsRoot string) gus.Handler { fsRoot = strings.TrimRight(fsRoot, "/") - return func(ctx context.Context, req *gemini.Request) *gemini.Response { + return func(ctx context.Context, req *gus.Request) *gus.Response { if !strings.HasPrefix(req.Path, pathRoot) { - return gemini.NotFound("Resource does not exist.") + return nil } path := req.Path[len(pathRoot):] @@ -53,7 +54,7 @@ func CGIDirectory(pathRoot, fsRoot string) gemini.Handler { } } - return gemini.NotFound("Resource does not exist.") + return nil } } @@ -97,10 +98,10 @@ func isNotExistError(err error) bool { // RunCGI runs a specific program as a CGI script. func RunCGI( ctx context.Context, - req *gemini.Request, + req *gus.Request, executable string, pathInfo string, -) *gemini.Response { +) *gus.Response { pathSegments := strings.Split(executable, "/") dirPath := "." @@ -139,7 +140,7 @@ func RunCGI( func prepareCGIEnv( ctx context.Context, - req *gemini.Request, + req *gus.Request, scriptName string, pathInfo string, ) []string { diff --git a/contrib/fs/dir.go b/contrib/fs/dir.go index b219e22..6292f67 100644 --- a/contrib/fs/dir.go +++ b/contrib/fs/dir.go @@ -8,6 +8,7 @@ import ( "strings" "text/template" + "tildegit.org/tjp/gus" "tildegit.org/tjp/gus/gemini" ) @@ -24,10 +25,10 @@ import ( // // It requires that files from the provided fs.FS implement fs.ReadDirFile. If they don't, // it will also produce "51 Not Found" responses for directory paths. -func DirectoryDefault(fileSystem fs.FS, fileNames ...string) gemini.Handler { - return func(ctx context.Context, req *gemini.Request) *gemini.Response { +func DirectoryDefault(fileSystem fs.FS, fileNames ...string) gus.Handler { + return func(ctx context.Context, req *gus.Request) *gus.Response { path, dirFile, resp := handleDir(req, fileSystem) - if resp != nil { + if dirFile == nil { return resp } defer dirFile.Close() @@ -50,7 +51,7 @@ func DirectoryDefault(fileSystem fs.FS, fileNames ...string) gemini.Handler { } } - return gemini.NotFound("Resource does not exist.") + return nil } } @@ -69,10 +70,10 @@ func DirectoryDefault(fileSystem fs.FS, fileNames ...string) gemini.Handler { // - .FullPath: the complete path to the listed directory // - .DirName: the name of the directory itself // - .Entries: the []fs.DirEntry of the directory contents -// +// // The template argument may be nil, in which case a simple default template is used. -func DirectoryListing(fileSystem fs.FS, template *template.Template) gemini.Handler { - return func(ctx context.Context, req *gemini.Request) *gemini.Response { +func DirectoryListing(fileSystem fs.FS, template *template.Template) gus.Handler { + return func(ctx context.Context, req *gus.Request) *gus.Response { path, dirFile, resp := handleDir(req, fileSystem) if resp != nil { return resp @@ -132,7 +133,7 @@ func dirlistNamespace(path string, dirFile fs.ReadDirFile) (map[string]any, erro return m, nil } -func handleDir(req *gemini.Request, fileSystem fs.FS) (string, fs.ReadDirFile, *gemini.Response) { +func handleDir(req *gus.Request, fileSystem fs.FS) (string, fs.ReadDirFile, *gus.Response) { path := strings.Trim(req.Path, "/") if path == "" { path = "." @@ -140,7 +141,7 @@ func handleDir(req *gemini.Request, fileSystem fs.FS) (string, fs.ReadDirFile, * file, err := fileSystem.Open(path) if isNotFound(err) { - return "", nil, gemini.NotFound("Resource does not exist.") + return "", nil, nil } if err != nil { return "", nil, gemini.Failure(err) @@ -154,7 +155,7 @@ func handleDir(req *gemini.Request, fileSystem fs.FS) (string, fs.ReadDirFile, * if !isDir { file.Close() - return "", nil, gemini.NotFound("Resource does not exist.") + return "", nil, nil } if !strings.HasSuffix(req.Path, "/") { @@ -167,7 +168,7 @@ func handleDir(req *gemini.Request, fileSystem fs.FS) (string, fs.ReadDirFile, * dirFile, ok := file.(fs.ReadDirFile) if !ok { file.Close() - return "", nil, gemini.NotFound("Resource does not exist.") + return "", nil, nil } return path, dirFile, nil diff --git a/contrib/fs/file.go b/contrib/fs/file.go index cdcd1a9..8cb1aeb 100644 --- a/contrib/fs/file.go +++ b/contrib/fs/file.go @@ -6,15 +6,16 @@ import ( "mime" "strings" + "tildegit.org/tjp/gus" "tildegit.org/tjp/gus/gemini" ) // FileHandler builds a handler function which serves up a file system. -func FileHandler(fileSystem fs.FS) gemini.Handler { - return func(ctx context.Context, req *gemini.Request) *gemini.Response { +func FileHandler(fileSystem fs.FS) gus.Handler { + return func(ctx context.Context, req *gus.Request) *gus.Response { file, err := fileSystem.Open(strings.TrimPrefix(req.Path, "/")) if isNotFound(err) { - return gemini.NotFound("Resource does not exist.") + return nil } if err != nil { return gemini.Failure(err) @@ -26,7 +27,7 @@ func FileHandler(fileSystem fs.FS) gemini.Handler { } if isDir { - return gemini.NotFound("Resource does not exist.") + return nil } return gemini.Success(mediaType(req.Path), file) diff --git a/contrib/log/log.go b/contrib/log/log.go index 2ccd3bc..0060f4e 100644 --- a/contrib/log/log.go +++ b/contrib/log/log.go @@ -7,16 +7,16 @@ import ( kitlog "github.com/go-kit/log" - "tildegit.org/tjp/gus/gemini" + "tildegit.org/tjp/gus" ) -func Requests(out io.Writer, logger kitlog.Logger) gemini.Middleware { +func Requests(out io.Writer, logger kitlog.Logger) gus.Middleware { if logger == nil { logger = kitlog.NewLogfmtLogger(kitlog.NewSyncWriter(out)) } - return func(next gemini.Handler) gemini.Handler { - return func(ctx context.Context, r *gemini.Request) (resp *gemini.Response) { + return func(next gus.Handler) gus.Handler { + return func(ctx context.Context, r *gus.Request) (resp *gus.Response) { start := time.Now() defer func() { end := time.Now() -- cgit v1.2.3