From d01e5aecb66687003b4fdc3f390f289eea508789 Mon Sep 17 00:00:00 2001 From: Sherlock Holo Date: Mon, 11 Mar 2019 18:57:17 +0800 Subject: [PATCH] Fix a bug: respReporter result body doesn't close Signed-off-by: Sherlock Holo --- server/gzip.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/server/gzip.go b/server/gzip.go index f13d966..72d672e 100644 --- a/server/gzip.go +++ b/server/gzip.go @@ -32,11 +32,15 @@ func enableGzip(handler http.Handler) http.Handler { handler.ServeHTTP(respRecorder, r) result := respRecorder.Result() + defer result.Body.Close() if result.StatusCode != http.StatusOK { copyHeader(w.Header(), result.Header) w.WriteHeader(result.StatusCode) - io.Copy(w, result.Body) + if _, err := io.Copy(w, result.Body); err != nil { + log.Printf("%+v", xerrors.Errorf("write status not OK http response failed: %w", err)) + http.Error(w, "internal server error", http.StatusInternalServerError) + } return } @@ -81,8 +85,6 @@ func shouldCompress(req *http.Request) bool { return true } - // log.Println("extension:", extension) - switch extension { case ".png", ".gif", ".jpeg", ".jpg": return false