From 04449ed66e4272ee08bc1dd00f6bdefb2be51a43 Mon Sep 17 00:00:00 2001 From: tjpcc Date: Tue, 10 Oct 2023 14:47:54 -0600 Subject: support an overridden "cmd" for CGIs --- contrib/cgi/cgi.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'contrib/cgi/cgi.go') diff --git a/contrib/cgi/cgi.go b/contrib/cgi/cgi.go index 48af04a..b7dd14a 100644 --- a/contrib/cgi/cgi.go +++ b/contrib/cgi/cgi.go @@ -11,6 +11,7 @@ import ( "net" "os" "os/exec" + "path/filepath" "strings" sr "tildegit.org/tjp/sliderule" @@ -88,16 +89,9 @@ func RunCGI( request *sr.Request, executable string, pathInfo string, + workdir string, stderr io.Writer, ) (*bytes.Buffer, int, error) { - pathSegments := strings.Split(executable, "/") - - dirPath := "." - if len(pathSegments) > 1 { - dirPath = strings.Join(pathSegments[:len(pathSegments)-1], "/") - } - basename := pathSegments[len(pathSegments)-1] - infoLen := len(pathInfo) if pathInfo == "/" { infoLen = 0 @@ -106,9 +100,14 @@ func RunCGI( scriptName := request.Path[:len(request.Path)-infoLen] scriptName = strings.TrimSuffix(scriptName, "/") - cmd := exec.CommandContext(ctx, "./"+basename) + execpath, err := filepath.Abs(executable) + if err != nil { + return nil, 0, err + } + + cmd := exec.CommandContext(ctx, execpath) cmd.Env = prepareCGIEnv(ctx, request, scriptName, pathInfo) - cmd.Dir = dirPath + cmd.Dir = workdir if body, ok := request.Meta.(io.Reader); ok { cmd.Stdin = body @@ -117,8 +116,7 @@ func RunCGI( cmd.Stdout = responseBuffer cmd.Stderr = stderr - err := cmd.Run() - if err != nil { + if err := cmd.Run(); err != nil { var exErr *exec.ExitError if errors.As(err, &exErr) { return responseBuffer, exErr.ExitCode(), nil -- cgit v1.2.3