From 1e0f8e0aaeaf1bd2ee39c02e922238b641bcf88b Mon Sep 17 00:00:00 2001 From: tjp Date: Mon, 13 Nov 2023 07:25:39 -0700 Subject: refactor contribs to work with a Protocol interface --- contrib/cgi/gemini.go | 51 +++------------------------------------------------ 1 file changed, 3 insertions(+), 48 deletions(-) (limited to 'contrib/cgi/gemini.go') 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) } -- cgit v1.2.3