From ef5f9c771d54696687b35081436a3abb516014b1 Mon Sep 17 00:00:00 2001 From: Benoit Benedetti Date: Wed, 24 Feb 2016 19:35:21 +0100 Subject: [PATCH] FastCGI: Explicitly set Content-Length #626 --- middleware/fastcgi/fastcgi.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) mode change 100755 => 100644 middleware/fastcgi/fastcgi.go diff --git a/middleware/fastcgi/fastcgi.go b/middleware/fastcgi/fastcgi.go old mode 100755 new mode 100644 index bddb04705..3d01c4162 --- a/middleware/fastcgi/fastcgi.go +++ b/middleware/fastcgi/fastcgi.go @@ -4,6 +4,7 @@ package fastcgi import ( + "bytes" "errors" "io" "net/http" @@ -105,13 +106,21 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) return http.StatusBadGateway, err } + // Write the response body to a buffer + // To explicitly set Content-Length + // For FastCGI app that don't set it + var buf bytes.Buffer + io.Copy(&buf, resp.Body) + if r.Header.Get("Content-Length") == "" { + w.Header().Set("Content-Length", strconv.Itoa(buf.Len())) + } writeHeader(w, resp) // Write the response body // TODO: If this has an error, the response will already be // partly written. We should copy out of resp.Body into a buffer // first, then write it to the response... - _, err = io.Copy(w, resp.Body) + _, err = io.Copy(w, &buf) if err != nil { return http.StatusBadGateway, err }