diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-09-09 08:42:21 -0600 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-09-09 08:42:21 -0600 |
commit | 33648cc286e812a8603743c29e96830de3b4acb8 (patch) | |
tree | 2253b0c71035f866fb14e6692baf5587b1925fb4 | |
parent | 195bdb565e9cb46e8d88ee364dcfc96a65a7159a (diff) |
log stderr on failed CGIs
-rw-r--r-- | contrib/cgi/cgi.go | 2 | ||||
-rw-r--r-- | contrib/cgi/gemini.go | 10 | ||||
-rw-r--r-- | contrib/cgi/gopher.go | 10 | ||||
-rw-r--r-- | contrib/cgi/spartan.go | 10 | ||||
-rw-r--r-- | gopher/gophermap/mdconv/convert.go | 1 | ||||
-rw-r--r-- | internal/server.go | 8 |
6 files changed, 37 insertions, 4 deletions
diff --git a/contrib/cgi/cgi.go b/contrib/cgi/cgi.go index 749a284..de0d35f 100644 --- a/contrib/cgi/cgi.go +++ b/contrib/cgi/cgi.go @@ -88,6 +88,7 @@ func RunCGI( request *sr.Request, executable string, pathInfo string, + stderr io.Writer, ) (*bytes.Buffer, int, error) { pathSegments := strings.Split(executable, "/") @@ -114,6 +115,7 @@ func RunCGI( } responseBuffer := &bytes.Buffer{} cmd.Stdout = responseBuffer + cmd.Stderr = stderr err := cmd.Run() if err != nil { diff --git a/contrib/cgi/gemini.go b/contrib/cgi/gemini.go index d245c8e..1e97939 100644 --- a/contrib/cgi/gemini.go +++ b/contrib/cgi/gemini.go @@ -1,12 +1,14 @@ 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. @@ -30,11 +32,17 @@ func GeminiCGIDirectory(pathRoot, fsRoot string) sr.Handler { return nil } - stdout, exitCode, err := RunCGI(ctx, request, filepath, pathinfo) + 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)) } diff --git a/contrib/cgi/gopher.go b/contrib/cgi/gopher.go index 67ea688..2f90f22 100644 --- a/contrib/cgi/gopher.go +++ b/contrib/cgi/gopher.go @@ -1,6 +1,7 @@ package cgi import ( + "bytes" "context" "fmt" "os" @@ -11,6 +12,7 @@ import ( sr "tildegit.org/tjp/sliderule" "tildegit.org/tjp/sliderule/gopher" "tildegit.org/tjp/sliderule/gopher/gophermap" + "tildegit.org/tjp/sliderule/logging" ) // GopherCGIDirectory runs any executable files relative to a root directory on the file system. @@ -113,11 +115,17 @@ func runGopherCGI( pathinfo string, settings gophermap.FileSystemSettings, ) *sr.Response { - stdout, exitCode, err := RunCGI(ctx, request, fullpath, pathinfo) + stderr := &bytes.Buffer{} + stdout, exitCode, err := RunCGI(ctx, request, fullpath, pathinfo, stderr) if err != nil { return gopher.Error(err).Response() } if exitCode != 0 { + ctx.Value("warnlog").(logging.Logger).Log( + "msg", "cgi exited with non-zero exit code", + "code", exitCode, + "stderr", stderr.String(), + ) return gopher.Error( fmt.Errorf("CGI process exited with status %d", exitCode), ).Response() diff --git a/contrib/cgi/spartan.go b/contrib/cgi/spartan.go index 6994466..272bd92 100644 --- a/contrib/cgi/spartan.go +++ b/contrib/cgi/spartan.go @@ -1,11 +1,13 @@ package cgi import ( + "bytes" "context" "fmt" "strings" sr "tildegit.org/tjp/sliderule" + "tildegit.org/tjp/sliderule/logging" "tildegit.org/tjp/sliderule/spartan" ) @@ -29,11 +31,17 @@ func SpartanCGIDirectory(pathRoot, fsRoot string) sr.Handler { return nil } - stdout, exitCode, err := RunCGI(ctx, request, filepath, pathinfo) + 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)) } diff --git a/gopher/gophermap/mdconv/convert.go b/gopher/gophermap/mdconv/convert.go index 5d4da08..cb9add0 100644 --- a/gopher/gophermap/mdconv/convert.go +++ b/gopher/gophermap/mdconv/convert.go @@ -35,7 +35,6 @@ var baseTmpl = template.Must(template.New("mdconv").Parse(` ` + "```" + ` {{.}} ` + "```" + ` - {{ end }} {{ define "link" -}} diff --git a/internal/server.go b/internal/server.go index f2b3485..1ae594b 100644 --- a/internal/server.go +++ b/internal/server.go @@ -5,6 +5,8 @@ import ( "net" "strings" "sync" + + "tildegit.org/tjp/sliderule/logging" ) type logger interface { @@ -40,6 +42,12 @@ func NewServer( networkAddr := listener.Addr() ctx, cancel := context.WithCancel(ctx) + debuglog, infolog, warnlog, errlog := logging.DefaultLoggers() + ctx = context.WithValue(ctx, "debuglog", debuglog) + ctx = context.WithValue(ctx, "infolog", infolog) + ctx = context.WithValue(ctx, "warnlog", warnlog) + ctx = context.WithValue(ctx, "errlog", errlog) + return Server{ Ctx: ctx, Cancel: cancel, |