-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcompression.go
72 lines (62 loc) · 1.38 KB
/
compression.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package azuretls
import (
"bytes"
"compress/gzip"
"compress/zlib"
"fmt"
"github.com/andybalholm/brotli"
"io/ioutil"
)
// DecompressBody unzips compressed data
func DecompressBody(Body []byte, encoding string) (parsedBody []byte) {
if len(encoding) > 0 {
if encoding == "gzip" {
unz, err := GUnzipData(Body)
if err != nil {
return Body
}
parsedBody = unz
} else if encoding == "deflate" {
unz, err := EnflateData(Body)
if err != nil {
return Body
}
parsedBody = unz
} else if encoding == "br" {
unz, err := UnBrotliData(Body)
if err != nil {
return Body
}
parsedBody = unz
} else {
fmt.Print("Unknown Encoding" + encoding)
parsedBody = Body
}
} else {
parsedBody = Body
}
return parsedBody
}
func GUnzipData(data []byte) (resData []byte, err error) {
gz, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return []byte{}, err
}
defer gz.Close()
respBody, err := ioutil.ReadAll(gz)
return respBody, err
}
func EnflateData(data []byte) (resData []byte, err error) {
zr, err := zlib.NewReader(bytes.NewReader(data))
if err != nil {
return []byte{}, err
}
defer zr.Close()
enflated, err := ioutil.ReadAll(zr)
return enflated, err
}
func UnBrotliData(data []byte) (resData []byte, err error) {
br := brotli.NewReader(bytes.NewReader(data))
respBody, err := ioutil.ReadAll(br)
return respBody, err
}