Skip to content

Commit cf34bc1

Browse files
committed
fixed janky handling of gzipped content returned with non-standard headers
1 parent 7e8b53b commit cf34bc1

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

pkg/requestutil/decompress.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ import (
1111
"github.com/mholt/archives"
1212
)
1313

14-
const ContentTypeGzip = "application/gzip"
14+
var ContentTypesGzip = []string{
15+
"application/gzip",
16+
"application/x-gzip",
17+
}
1518

1619
func WithGzip(out io.Writer) requests.ResponseHandler {
1720
return func(response *http.Response) error {
1821
log := logr.FromContextOrDiscard(response.Request.Context())
1922
var stream io.ReadCloser
2023

2124
// if it's a gzip response, decompress it
22-
if mimetype.EqualsAny(response.Header.Get("Content-Type"), ContentTypeGzip) {
25+
if isGzipped(response.Header.Get("Content-Type")) {
2326
log.V(8).Info("decompressing gzip response")
2427
dec, err := archives.Gz{}.OpenReader(response.Body)
2528
if err != nil {
@@ -37,3 +40,7 @@ func WithGzip(out io.Writer) requests.ResponseHandler {
3740
return nil
3841
}
3942
}
43+
44+
func isGzipped(s string) bool {
45+
return mimetype.EqualsAny(s, ContentTypesGzip...)
46+
}

pkg/requestutil/decompress_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package requestutil
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestIsGzipped(t *testing.T) {
10+
var cases = []struct {
11+
s string
12+
ok bool
13+
}{
14+
{
15+
"application/gzip",
16+
true,
17+
},
18+
{
19+
"application/x-gzip",
20+
true,
21+
},
22+
{
23+
"application/javascript",
24+
false,
25+
},
26+
}
27+
28+
for _, tt := range cases {
29+
t.Run(tt.s, func(t *testing.T) {
30+
ok := isGzipped(tt.s)
31+
assert.EqualValues(t, tt.ok, ok)
32+
})
33+
}
34+
}

0 commit comments

Comments
 (0)