summaryrefslogtreecommitdiff
path: root/contrib/cgi
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-10-30 11:57:04 -0600
committertjpcc <tjp@ctrl-c.club>2023-10-30 11:57:04 -0600
commitf85930d875494c043fc5ac3ddcf843ddfac14ec9 (patch)
treed905a0d8a8b9d8277c4d3949b9500e44999f452e /contrib/cgi
parent629956103b945e1596bab5c5dea163e849a8bf73 (diff)
spartan support in fs and cgi contribs
fixes #17
Diffstat (limited to 'contrib/cgi')
-rw-r--r--contrib/cgi/spartan.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/contrib/cgi/spartan.go b/contrib/cgi/spartan.go
new file mode 100644
index 0000000..36aaa36
--- /dev/null
+++ b/contrib/cgi/spartan.go
@@ -0,0 +1,61 @@
+package cgi
+
+import (
+ "bytes"
+ "context"
+ "fmt"
+ "path/filepath"
+ "strings"
+
+ sr "tildegit.org/tjp/sliderule"
+ "tildegit.org/tjp/sliderule/logging"
+ "tildegit.org/tjp/sliderule/spartan"
+)
+
+// SpartanCGIDirectory 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 SpartanCGIDirectory(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 spartan.ServerError(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 spartan.ServerError(err)
+ }
+ if exitCode != 0 {
+ ctx.Value("warnlog").(logging.Logger).Log(
+ "msg", "cgi exited with non-zero exit code",
+ "code", exitCode,
+ "stderr", stderr.String(),
+ )
+ return spartan.ServerError(fmt.Errorf("CGI process exited with status %d", exitCode))
+ }
+
+ response, err := spartan.ParseResponse(stdout)
+ if err != nil {
+ return spartan.ServerError(err)
+ }
+ return response
+ })
+}