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/gopher.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'contrib/cgi/gopher.go') diff --git a/contrib/cgi/gopher.go b/contrib/cgi/gopher.go index bb3e73e..7067a6d 100644 --- a/contrib/cgi/gopher.go +++ b/contrib/cgi/gopher.go @@ -21,7 +21,7 @@ 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(fsroot, urlroot string, settings *gophermap.FileSystemSettings) sr.Handler { +func GopherCGIDirectory(fsroot, urlroot, cmd 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 }) } @@ -41,12 +41,12 @@ func GopherCGIDirectory(fsroot, urlroot string, settings *gophermap.FileSystemSe return nil } - return runGopherCGI(ctx, request, fullpath, pathinfo, *settings) + return runGopherCGI(ctx, request, fullpath, pathinfo, cmd, *settings) }) } // ExecGopherMaps runs any gophermaps -func ExecGopherMaps(fsroot, urlroot string, settings *gophermap.FileSystemSettings) sr.Handler { +func ExecGopherMaps(fsroot, urlroot, cmd 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 }) } @@ -85,7 +85,7 @@ func ExecGopherMaps(fsroot, urlroot string, settings *gophermap.FileSystemSettin if !m.IsRegular() || m&5 != 5 { continue } - return runGopherCGI(ctx, request, fpath, "/", *settings) + return runGopherCGI(ctx, request, fpath, "/", cmd, *settings) } return nil @@ -96,7 +96,7 @@ func ExecGopherMaps(fsroot, urlroot string, settings *gophermap.FileSystemSettin return nil } - return runGopherCGI(ctx, request, fullpath, "/", *settings) + return runGopherCGI(ctx, request, fullpath, "/", cmd, *settings) }) } @@ -105,10 +105,16 @@ func runGopherCGI( request *sr.Request, fullpath string, pathinfo string, + cmd string, settings gophermap.FileSystemSettings, ) *sr.Response { + workdir := filepath.Dir(fullpath) + if cmd != "" { + fullpath = cmd + } + stderr := &bytes.Buffer{} - stdout, exitCode, err := RunCGI(ctx, request, fullpath, pathinfo, stderr) + stdout, exitCode, err := RunCGI(ctx, request, fullpath, pathinfo, workdir, stderr) if err != nil { return gopher.Error(err).Response() } -- cgit v1.2.3