From 66a1b1f39a1e1d5499b548b36d18c8daa872d7da Mon Sep 17 00:00:00 2001 From: tjpcc Date: Sat, 28 Jan 2023 14:52:35 -0700 Subject: gopher support. Some of the contrib packages were originally built gemini-specific and had to be refactored into generic core functionality and thin protocol-specific wrappers for each of gemini and gopher. --- contrib/cgi/gemini.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 contrib/cgi/gemini.go (limited to 'contrib/cgi/gemini.go') diff --git a/contrib/cgi/gemini.go b/contrib/cgi/gemini.go new file mode 100644 index 0000000..8302e7e --- /dev/null +++ b/contrib/cgi/gemini.go @@ -0,0 +1,47 @@ +package cgi + +import ( + "context" + "fmt" + "strings" + + "tildegit.org/tjp/gus" + "tildegit.org/tjp/gus/gemini" +) + +// GeminiCGIDirectory runs any executable files relative to a root directory on the file system. +// +// It will also find and run any executables _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 path. +func GeminiCGIDirectory(pathRoot, fsRoot string) gus.Handler { + fsRoot = strings.TrimRight(fsRoot, "/") + return func(ctx context.Context, request *gus.Request) *gus.Response { + if !strings.HasPrefix(request.Path, pathRoot) { + return nil + } + + filepath, pathinfo, err := ResolveCGI(request.Path[len(pathRoot):], fsRoot) + if err != nil { + return gemini.Failure(err) + } + if filepath == "" { + return nil + } + + stdout, exitCode, err := RunCGI(ctx, request, filepath, pathinfo) + if err != nil { + return gemini.Failure(err) + } + if exitCode != 0 { + return gemini.CGIError(fmt.Sprintf("CGI process exited with status %d", exitCode)) + } + + response, err := gemini.ParseResponse(stdout) + if err != nil { + return gemini.Failure(err) + } + return response + } +} -- cgit v1.2.3