summaryrefslogtreecommitdiff
path: root/contrib/cgi/gemini.go
blob: 1e979397ea087f69bfc7a88ef9a51991ff9cebda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cgi

import (
	"bytes"
	"context"
	"fmt"
	"strings"

	sr "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.
//
// 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 GeminiCGIDirectory(pathRoot, fsRoot string) sr.Handler {
	fsRoot = strings.TrimRight(fsRoot, "/")
	return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response {
		if !strings.HasPrefix(request.Path, pathRoot) {
			return nil
		}

		filepath, pathinfo, err := ResolveCGI(request.Path[len(pathRoot):], fsRoot)
		if err != nil {
			return gemini.Failure(err)
		}
		if filepath == "" {
			return nil
		}

		stderr := &bytes.Buffer{}
		stdout, exitCode, err := RunCGI(ctx, request, filepath, pathinfo, 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
	})
}