From 1adb697cdb23d64242d858fb616942a9b8ded49c Mon Sep 17 00:00:00 2001 From: Surma Date: Thu, 4 Aug 2016 20:40:36 +0100 Subject: [PATCH] Only compress if client sets `Accept-Encoding` header (fixes #14) --- simplehttp2server.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/simplehttp2server.go b/simplehttp2server.go index 9e3f982..25442ac 100644 --- a/simplehttp2server.go +++ b/simplehttp2server.go @@ -80,10 +80,13 @@ func main() { if !*disableGzip { oldfs := fs fs = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Encoding", "gzip") - grw := GzipResponseWriter{gzip.NewWriter(w), w} - oldfs.ServeHTTP(grw, r) - grw.WriteCloser.Close() + if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { + w.Header().Set("Content-Encoding", "gzip") + grw := GzipResponseWriter{gzip.NewWriter(w), w} + defer grw.WriteCloser.Close() + w = grw + } + oldfs.ServeHTTP(w, r) }) }