From e77f66c29fa35adbc44ef889d80921acabd7d3e6 Mon Sep 17 00:00:00 2001 From: Sean Marciniak Date: Fri, 13 Sep 2024 16:34:57 +0930 Subject: [PATCH] Reducing scope of PR --- config/confighttp/compression.go | 24 ++++++++++++++++++++---- config/confighttp/compression_test.go | 2 +- config/confighttp/compressor.go | 4 ++-- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/config/confighttp/compression.go b/config/confighttp/compression.go index 8ae0f891a60..e6da0de1dac 100644 --- a/config/confighttp/compression.go +++ b/config/confighttp/compression.go @@ -32,7 +32,11 @@ var availableDecoders = map[string]func(body io.ReadCloser) (io.ReadCloser, erro return nil, nil }, "gzip": func(body io.ReadCloser) (io.ReadCloser, error) { - return gzip.NewReader(body) + gr, err := gzip.NewReader(body) + if err != nil { + return nil, err + } + return gr, nil }, "zstd": func(body io.ReadCloser) (io.ReadCloser, error) { zr, err := zstd.NewReader( @@ -49,11 +53,23 @@ var availableDecoders = map[string]func(body io.ReadCloser) (io.ReadCloser, erro return zr.IOReadCloser(), nil }, "zlib": func(body io.ReadCloser) (io.ReadCloser, error) { - return zlib.NewReader(body) + zr, err := zlib.NewReader(body) + if err != nil { + return nil, err + } + return zr, nil }, - //nolint:unparam // Require to have error return so it can be part of map "snappy": func(body io.ReadCloser) (io.ReadCloser, error) { - return io.NopCloser(snappy.NewReader(body)), nil + sr := snappy.NewReader(body) + sb := new(bytes.Buffer) + _, err := io.Copy(sb, sr) + if err != nil { + return nil, err + } + if err = body.Close(); err != nil { + return nil, err + } + return io.NopCloser(sb), nil }, //nolint:unparam // Require to have error return so it can be part of map "lz4": func(body io.ReadCloser) (io.ReadCloser, error) { diff --git a/config/confighttp/compression_test.go b/config/confighttp/compression_test.go index 6a3517e9a86..ff9db6a28dd 100644 --- a/config/confighttp/compression_test.go +++ b/config/confighttp/compression_test.go @@ -244,7 +244,7 @@ func TestHTTPContentDecompressionHandler(t *testing.T) { encoding: "snappy", reqBody: bytes.NewBuffer(testBody), respCode: http.StatusBadRequest, - respBody: "snappy: corrupt input", + respBody: "snappy: corrupt input\n", }, { name: "InvalidLz4", diff --git a/config/confighttp/compressor.go b/config/confighttp/compressor.go index 46f137a0629..5c8fefa92cc 100644 --- a/config/confighttp/compressor.go +++ b/config/confighttp/compressor.go @@ -7,7 +7,7 @@ import ( "bytes" "compress/gzip" "compress/zlib" - "fmt" + "errors" "io" "sync" @@ -61,7 +61,7 @@ func newCompressor(compressionType configcompression.Type) (*compressor, error) case configcompression.TypeLz4: return lz4Pool, nil } - return nil, fmt.Errorf("unsupported compression type %q", compressionType) + return nil, errors.New("unsupported compression type, ") } func (p *compressor) compress(buf *bytes.Buffer, body io.ReadCloser) error {