From b7fd1f4e9ea044566e246b0768f2254238400d3e Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Sat, 12 Dec 2015 14:04:48 +0100 Subject: [PATCH] FastCGI: separate standard and error output responses. --- middleware/fastcgi/fastcgi.go | 15 ++++++++++++++- middleware/fastcgi/fcgiclient.go | 21 +++++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/middleware/fastcgi/fastcgi.go b/middleware/fastcgi/fastcgi.go index 77fb668f..b7a86b66 100755 --- a/middleware/fastcgi/fastcgi.go +++ b/middleware/fastcgi/fastcgi.go @@ -115,7 +115,12 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) return http.StatusBadGateway, err } - return 0, nil + // FastCGI stderr outputs + if fcgi.stderr.Len() != 0 { + err = LogError(fcgi.stderr.String()) + } + + return 0, err } } @@ -281,3 +286,11 @@ var ( // ErrIndexMissingSplit describes an index configuration error. ErrIndexMissingSplit = errors.New("configured index file(s) must include split value") ) + +// LogError is a non fatal error that allows requests to go through. +type LogError string + +// Error satisfies error interface. +func (l LogError) Error() string { + return string(l) +} diff --git a/middleware/fastcgi/fcgiclient.go b/middleware/fastcgi/fcgiclient.go index b8b03fe7..97dc6153 100644 --- a/middleware/fastcgi/fcgiclient.go +++ b/middleware/fastcgi/fcgiclient.go @@ -164,6 +164,7 @@ type FCGIClient struct { rwc io.ReadWriteCloser h header buf bytes.Buffer + stderr bytes.Buffer keepAlive bool reqID uint16 } @@ -346,10 +347,22 @@ func (w *streamReader) Read(p []byte) (n int, err error) { if len(p) > 0 { if len(w.buf) == 0 { - rec := &record{} - w.buf, err = rec.read(w.c.rwc) - if err != nil { - return + + // filter outputs for error log + for { + rec := &record{} + var buf []byte + buf, err = rec.read(w.c.rwc) + if err != nil { + return + } + // standard error output + if rec.h.Type == Stderr { + w.c.stderr.Write(buf) + continue + } + w.buf = buf + break } }