summaryrefslogtreecommitdiff
path: root/contrib/cgi/cgi.go
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-10-10 14:47:54 -0600
committertjpcc <tjp@ctrl-c.club>2023-10-10 14:47:54 -0600
commit04449ed66e4272ee08bc1dd00f6bdefb2be51a43 (patch)
tree665ce77a841144bd9b70becba7e541f187867406 /contrib/cgi/cgi.go
parentd467d044798f61b34684e885cf5b1544c3fb2ee2 (diff)
support an overridden "cmd" for CGIs
Diffstat (limited to 'contrib/cgi/cgi.go')
-rw-r--r--contrib/cgi/cgi.go22
1 files changed, 10 insertions, 12 deletions
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