From 6dda7bfb81749aa8d5820b26624ab0237af9dbf6 Mon Sep 17 00:00:00 2001 From: Nedyalko Andreev <n@andreev.sh> Date: Wed, 21 Mar 2018 13:22:34 +0200 Subject: [PATCH] Fix deflate encoded responses RFC 2616 (HTTP/1.1) says in the "3.5 Content Codings" [1] section about deflate: The "zlib" format defined in RFC 1950 [31] in combination with the "deflate" compression mechanism described in RFC 1951 [29]. The zlib package [2] from Go's standard library imlpements RFC 1950 and it also uses[3] the flate package internally, which implements RFC 1951 [4] [1] https://tools.ietf.org/html/rfc2616#section-3.5 [2] https://golang.org/pkg/compress/zlib/#pkg-overview [3] https://golang.org/src/compress/zlib/writer.go#L15 [4] https://golang.org/pkg/compress/flate/#pkg-overview --- httpbin/handlers.go | 4 ++-- httpbin/handlers_test.go | 7 +++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/httpbin/handlers.go b/httpbin/handlers.go index e119766..2583cf9 100644 --- a/httpbin/handlers.go +++ b/httpbin/handlers.go @@ -2,8 +2,8 @@ package httpbin import ( "bytes" - "compress/flate" "compress/gzip" + "compress/zlib" "encoding/json" "fmt" "math/rand" @@ -109,7 +109,7 @@ func (h *HTTPBin) Deflate(w http.ResponseWriter, r *http.Request) { body, _ := json.Marshal(resp) buf := &bytes.Buffer{} - w2, _ := flate.NewWriter(buf, flate.DefaultCompression) + w2 := zlib.NewWriter(buf) w2.Write(body) w2.Close() diff --git a/httpbin/handlers_test.go b/httpbin/handlers_test.go index 47ed24f..c8313ae 100644 --- a/httpbin/handlers_test.go +++ b/httpbin/handlers_test.go @@ -3,8 +3,8 @@ package httpbin import ( "bufio" "bytes" - "compress/flate" "compress/gzip" + "compress/zlib" "encoding/json" "fmt" "io/ioutil" @@ -1261,7 +1261,10 @@ func TestDeflate(t *testing.T) { t.Fatal(err) } - reader := flate.NewReader(w.Body) + reader, err := zlib.NewReader(w.Body) + if err != nil { + t.Fatal(err) + } body, err := ioutil.ReadAll(reader) if err != nil { t.Fatal(err) -- GitLab