-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoding.go
54 lines (45 loc) · 1.02 KB
/
decoding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package http
import (
"compress/gzip"
"compress/zlib"
"io"
"strings"
"github.com/andybalholm/brotli"
"github.com/klauspost/compress/zstd"
http "github.com/vimbing/fhttp"
)
func decodeResponseBody(headers http.Header, body io.Reader) (io.Reader, error) {
var encoding string
for k, v := range headers {
if strings.EqualFold(k, "content-encoding") && len(v) > 0 {
encoding = strings.ToLower(v[0])
}
}
switch encoding {
case "br":
return brotoliDecode(body), nil
case "gzip":
return gzipDecode(body)
case "deflate":
return deflateDecode(body)
case "zstd":
return zstdDecode(body)
default:
return body, nil
}
}
func brotoliDecode(body io.Reader) io.Reader {
return brotli.NewReader(body)
}
func deflateDecode(body io.Reader) (io.Reader, error) {
zr, err := zlib.NewReader(body)
return zr, err
}
func gzipDecode(body io.Reader) (io.Reader, error) {
gz, err := gzip.NewReader(body)
return gz, err
}
func zstdDecode(body io.Reader) (io.Reader, error) {
zs, err := zstd.NewReader(body)
return zs, err
}