summaryrefslogtreecommitdiff
path: root/contrib/cgi
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/cgi')
-rw-r--r--contrib/cgi/cgi_test.go2
-rw-r--r--contrib/cgi/gemini.go8
-rw-r--r--contrib/cgi/gopher.go32
-rw-r--r--contrib/cgi/spartan.go54
4 files changed, 17 insertions, 79 deletions
diff --git a/contrib/cgi/cgi_test.go b/contrib/cgi/cgi_test.go
index ff2c45d..5469fc8 100644
--- a/contrib/cgi/cgi_test.go
+++ b/contrib/cgi/cgi_test.go
@@ -21,7 +21,7 @@ func TestCGIDirectory(t *testing.T) {
tlsconf, err := gemini.FileTLS("testdata/server.crt", "testdata/server.key")
require.Nil(t, err)
- handler := cgi.GeminiCGIDirectory("/cgi-bin", "./testdata")
+ handler := cgi.GeminiCGIDirectory("./testdata", "/cgi-bin")
server, err := gemini.NewServer(context.Background(), "localhost", "tcp", "127.0.0.1:0", handler, nil, tlsconf)
require.Nil(t, err)
diff --git a/contrib/cgi/gemini.go b/contrib/cgi/gemini.go
index 1e97939..3ad407d 100644
--- a/contrib/cgi/gemini.go
+++ b/contrib/cgi/gemini.go
@@ -17,14 +17,14 @@ 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(pathRoot, fsRoot string) sr.Handler {
- fsRoot = strings.TrimRight(fsRoot, "/")
+func GeminiCGIDirectory(fsroot, urlroot 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) {
+ if !strings.HasPrefix(request.Path, urlroot) {
return nil
}
- filepath, pathinfo, err := ResolveCGI(request.Path[len(pathRoot):], fsRoot)
+ filepath, pathinfo, err := ResolveCGI(request.Path[len(urlroot):], fsroot)
if err != nil {
return gemini.Failure(err)
}
diff --git a/contrib/cgi/gopher.go b/contrib/cgi/gopher.go
index 2f90f22..bb3e73e 100644
--- a/contrib/cgi/gopher.go
+++ b/contrib/cgi/gopher.go
@@ -21,24 +21,19 @@ 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 GopherCGIDirectory(pathRoot, fsRoot string, settings *gophermap.FileSystemSettings) sr.Handler {
- if settings == nil {
- settings = &gophermap.FileSystemSettings{}
- }
-
- if !settings.Exec {
+func GopherCGIDirectory(fsroot, urlroot string, settings *gophermap.FileSystemSettings) sr.Handler {
+ if settings == nil || !settings.Exec {
return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { return nil })
}
+ fsroot = strings.TrimRight(fsroot, "/")
- fsRoot = strings.TrimRight(fsRoot, "/")
return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response {
- if !strings.HasPrefix(request.Path, pathRoot) {
+ if !strings.HasPrefix(request.Path, urlroot) {
return nil
}
+ requestpath := strings.Trim(strings.TrimPrefix(request.Path, urlroot), "/")
- requestPath := strings.Trim(strings.TrimPrefix(request.Path, pathRoot), "/")
-
- fullpath, pathinfo, err := resolveGopherCGI(fsRoot, requestPath)
+ fullpath, pathinfo, err := resolveGopherCGI(fsroot, requestpath)
if err != nil {
return gopher.Error(err).Response()
}
@@ -51,22 +46,19 @@ func GopherCGIDirectory(pathRoot, fsRoot string, settings *gophermap.FileSystemS
}
// ExecGopherMaps runs any gophermaps
-func ExecGopherMaps(pathRoot, fsRoot string, settings *gophermap.FileSystemSettings) sr.Handler {
- if settings == nil {
- settings = &gophermap.FileSystemSettings{}
- }
-
- if !settings.Exec {
+func ExecGopherMaps(fsroot, urlroot string, settings *gophermap.FileSystemSettings) sr.Handler {
+ if settings == nil || !settings.Exec {
return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { return nil })
}
+ fsroot = strings.TrimRight(fsroot, "/")
- fsRoot = strings.TrimRight(fsRoot, "/")
return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response {
- if !strings.HasPrefix(request.Path, pathRoot) {
+ if !strings.HasPrefix(request.Path, urlroot) {
return nil
}
+ requestpath := strings.Trim(strings.TrimPrefix(request.Path, urlroot), "/")
- fullpath := filepath.Join(fsRoot, strings.Trim(request.Path, "/"))
+ fullpath := filepath.Join(fsroot, requestpath)
info, err := os.Stat(fullpath)
if isNotExistError(err) {
return nil
diff --git a/contrib/cgi/spartan.go b/contrib/cgi/spartan.go
deleted file mode 100644
index 272bd92..0000000
--- a/contrib/cgi/spartan.go
+++ /dev/null
@@ -1,54 +0,0 @@
-package cgi
-
-import (
- "bytes"
- "context"
- "fmt"
- "strings"
-
- sr "tildegit.org/tjp/sliderule"
- "tildegit.org/tjp/sliderule/logging"
- "tildegit.org/tjp/sliderule/spartan"
-)
-
-// SpartanCGIDirectory runs executable files relative to a root directory in the file system.
-//
-// It will also find any run any executable _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.
-func SpartanCGIDirectory(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 spartan.ServerError(err)
- }
- if filepath == "" {
- return nil
- }
-
- stderr := &bytes.Buffer{}
- stdout, exitCode, err := RunCGI(ctx, request, filepath, pathinfo, 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
- })
-}