diff options
author | tjp <tjp@ctrl-c.club> | 2023-11-13 07:25:39 -0700 |
---|---|---|
committer | tjp <tjp@ctrl-c.club> | 2023-11-13 07:27:16 -0700 |
commit | 1e0f8e0aaeaf1bd2ee39c02e922238b641bcf88b (patch) | |
tree | 020e5de91f2343119fed10dede9d2c8262a3cd83 /contrib/cgi/gemini.go | |
parent | a808b4692656c10bb43e2d54a2f5ef2746d231d5 (diff) |
refactor contribs to work with a Protocol interface
Diffstat (limited to 'contrib/cgi/gemini.go')
-rw-r--r-- | contrib/cgi/gemini.go | 51 |
1 files changed, 3 insertions, 48 deletions
diff --git a/contrib/cgi/gemini.go b/contrib/cgi/gemini.go index 0aa3044..9e4d68f 100644 --- a/contrib/cgi/gemini.go +++ b/contrib/cgi/gemini.go @@ -1,15 +1,8 @@ package cgi import ( - "bytes" - "context" - "fmt" - "path/filepath" - "strings" - - sr "tildegit.org/tjp/sliderule" + "tildegit.org/tjp/sliderule" "tildegit.org/tjp/sliderule/gemini" - "tildegit.org/tjp/sliderule/logging" ) // GeminiCGIDirectory runs any executable files relative to a root directory on the file system. @@ -18,44 +11,6 @@ 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(fsroot, urlroot, cmd string) sr.Handler { - fsroot = strings.TrimRight(fsroot, "/") - return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - if !strings.HasPrefix(request.Path, urlroot) { - return nil - } - - execpath, pathinfo, err := ResolveCGI(request.Path[len(urlroot):], fsroot) - if err != nil { - return gemini.Failure(err) - } - if execpath == "" { - return nil - } - workdir := filepath.Dir(execpath) - - if cmd != "" { - execpath = cmd - } - - stderr := &bytes.Buffer{} - stdout, exitCode, err := RunCGI(ctx, request, execpath, pathinfo, workdir, stderr) - if err != nil { - return gemini.Failure(err) - } - if exitCode != 0 { - ctx.Value("warnlog").(logging.Logger).Log( - "msg", "cgi exited with non-zero exit code", - "code", exitCode, - "stderr", stderr.String(), - ) - 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 - }) +func GeminiCGIDirectory(fsroot, urlroot, cmd string) sliderule.Handler { + return cgiDirectory(gemini.ServerProtocol, fsroot, urlroot, cmd) } |