From 1b17072a89721b8fdbe42c812f7d2d610b5f70dc Mon Sep 17 00:00:00 2001 From: Thomas Hansen Date: Sun, 26 Apr 2015 22:01:20 -0600 Subject: [PATCH 1/2] gzip middleware now strips encoding header --- middleware/fastcgi/fastcgi.go | 18 ++++++------------ middleware/gzip/gzip.go | 1 + 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/middleware/fastcgi/fastcgi.go b/middleware/fastcgi/fastcgi.go index 9a9994d7..3c9f9eab 100644 --- a/middleware/fastcgi/fastcgi.go +++ b/middleware/fastcgi/fastcgi.go @@ -5,9 +5,7 @@ package fastcgi import ( "errors" - "fmt" "io" - "io/ioutil" "net/http" "os" "path/filepath" @@ -105,11 +103,11 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) } w.WriteHeader(resp.StatusCode) - body, err := ioutil.ReadAll(resp.Body) - fmt.Printf("%s", body) - fmt.Printf("%d\n", resp.StatusCode) - fmt.Printf("%d\n", len(body)) - w.Write(body) + // Write the response body + _, err = io.Copy(w, resp.Body) + if err != nil { + return http.StatusBadGateway, err + } return resp.StatusCode, nil } @@ -196,11 +194,7 @@ func (h Handler) buildEnv(r *http.Request, rule Rule) (map[string]string, error) for field, val := range r.Header { header := strings.ToUpper(field) header = headerNameReplacer.Replace(header) - // We don't want to pass the encoding header to prevent the fastcgi server from gzipping - // TODO: is there a better way. - if header != "ACCEPT_ENCODING" { - env["HTTP_"+header] = strings.Join(val, ", ") - } + env["HTTP_"+header] = strings.Join(val, ", ") } return env, nil diff --git a/middleware/gzip/gzip.go b/middleware/gzip/gzip.go index c9f62bc4..d6844af4 100644 --- a/middleware/gzip/gzip.go +++ b/middleware/gzip/gzip.go @@ -33,6 +33,7 @@ func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { return g.Next.ServeHTTP(w, r) } + r.Header.Del("Accept-Encoding") w.Header().Set("Content-Encoding", "gzip") gzipWriter := gzip.NewWriter(w) defer gzipWriter.Close() From 9d26a9268b54c88b5f11cd6bd52f8853cb53c95a Mon Sep 17 00:00:00 2001 From: Thomas Hansen Date: Sun, 26 Apr 2015 22:15:43 -0600 Subject: [PATCH 2/2] added comment about encoding header --- middleware/gzip/gzip.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/middleware/gzip/gzip.go b/middleware/gzip/gzip.go index d6844af4..35d29e6f 100644 --- a/middleware/gzip/gzip.go +++ b/middleware/gzip/gzip.go @@ -33,7 +33,7 @@ func (g Gzip) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { return g.Next.ServeHTTP(w, r) } - r.Header.Del("Accept-Encoding") + r.Header.Del("Accept-Encoding") // Removes header to prevent double encoding by ambitious fastcgi implementations w.Header().Set("Content-Encoding", "gzip") gzipWriter := gzip.NewWriter(w) defer gzipWriter.Close()