/
decompress.go
244 lines (203 loc) · 5.38 KB
/
decompress.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package builtin
import (
"compress/flate"
"compress/gzip"
"fmt"
"github.com/andybalholm/brotli"
"io"
"net/http"
"runtime"
"strings"
"sync"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/filters"
)
const (
// DecompressionNotPossible is the state-bag key to indicate
// to the subsequent filters during response processing that the
// content is compressed, but decompression was not possible, e.g
// because the encoding is not supported.
DecompressionNotPossible = "filter::decompress::not-possible"
// DecompressionError is the state-bag key to indicate to the
// subsequent filters during response processing that the
// decompression of the content was attempted but failed. The
// response body may have been sniffed, and therefore it was
// discarded.
DecompressionError = "filter::decompress::error"
)
type decodedBody struct {
enc string
original io.Closer
decoder io.ReadCloser
isFromPool bool
}
type decodingError struct {
decoder error
original error
}
type decompress struct{}
// workaround to make brotli library compatible with decompress
type brotliWrapper struct {
brotli.Reader
}
func (brotliWrapper) Close() error { return nil }
var supportedEncodingsDecompress = map[string]*sync.Pool{
"gzip": {},
"deflate": {},
"br": {},
}
func init() {
// #cpu * 4: pool size decided based on some
// simple tests, checking performance by binary
// steps (https://github.com/zalando/skipper)
for enc, pool := range supportedEncodingsDecompress {
for i := 0; i < runtime.NumCPU()*4; i++ {
pool.Put(newDecoder(enc))
}
}
}
func newDecoder(enc string) io.ReadCloser {
switch enc {
case "gzip":
return new(gzip.Reader)
case "br":
return new(brotliWrapper)
default:
return flate.NewReader(nil)
}
}
func fromPool(enc string) (io.ReadCloser, bool) {
d, ok := supportedEncodingsDecompress[enc].Get().(io.ReadCloser)
return d, ok
}
func reset(decoder, original io.ReadCloser, enc string) error {
switch enc {
case "gzip":
return decoder.(*gzip.Reader).Reset(original)
case "br":
return decoder.(*brotliWrapper).Reset(original)
default:
return decoder.(flate.Resetter).Reset(original, nil)
}
}
func newDecodedBody(original io.ReadCloser, encs []string) (body io.ReadCloser, err error) {
if len(encs) == 0 {
body = original
return
}
last := len(encs) - 1
enc := encs[last]
encs = encs[:last]
decoder, isFromPool := fromPool(enc)
if !isFromPool {
decoder = newDecoder(enc)
}
if err = reset(decoder, original, enc); err != nil {
return
}
decoded := decodedBody{
enc: enc,
original: original,
decoder: decoder,
isFromPool: isFromPool,
}
return newDecodedBody(decoded, encs)
}
func (b decodedBody) Read(p []byte) (int, error) {
return b.decoder.Read(p)
}
func (b decodedBody) Close() error {
derr := b.decoder.Close()
if b.isFromPool {
if derr != nil {
supportedEncodingsDecompress[b.enc].Put(newDecoder(b.enc))
} else {
supportedEncodingsDecompress[b.enc].Put(b.decoder)
}
}
oerr := b.original.Close()
var err error
if derr != nil || oerr != nil {
err = decodingError{
decoder: derr,
original: oerr,
}
}
return err
}
func (e decodingError) Error() string {
switch {
case e.decoder == nil:
return e.original.Error()
case e.original == nil:
return e.decoder.Error()
default:
return fmt.Sprintf("%v; %v", e.decoder, e.original)
}
}
// NewDecompress creates a filter specification for the decompress() filter.
// The filter attempts to decompress the response body, if it was compressed
// with any of deflate, gzip or br.
//
// If decompression is not possible, but the body is compressed, then it indicates it
// with the "filter::decompress::not-possible" key in the state-bag. If the decompression
// was attempted and failed to get initialized, it indicates it in addition with the
// "filter::decompress::error" state-bag key, storing the error. Due to the streaming,
// decompression may fail after all the filters were processed.
//
// The filter does not need any parameters.
//
func NewDecompress() filters.Spec {
return decompress{}
}
func (d decompress) Name() string { return filters.DecompressName }
func (d decompress) CreateFilter([]interface{}) (filters.Filter, error) {
return d, nil
}
func (d decompress) Request(filters.FilterContext) {}
func getEncodings(header string) []string {
raw := strings.Split(header, ",")
var encs []string
for _, r := range raw {
r = strings.TrimSpace(r)
if r != "" {
encs = append(encs, r)
}
}
return encs
}
func encodingsSupported(encs []string) bool {
for _, e := range encs {
if _, supported := supportedEncodingsDecompress[e]; !supported {
return false
}
}
return true
}
func (d decompress) Response(ctx filters.FilterContext) {
rsp := ctx.Response()
encs := getEncodings(rsp.Header.Get("Content-Encoding"))
if len(encs) == 0 {
return
}
if !encodingsSupported(encs) {
ctx.StateBag()[DecompressionNotPossible] = true
return
}
rsp.Header.Del("Content-Encoding")
rsp.Header.Del("Vary")
rsp.Header.Del("Content-Length")
rsp.ContentLength = -1
b, err := newDecodedBody(rsp.Body, encs)
if err != nil {
// we may have already sniffed from the response via the gzip.Reader
rsp.Body.Close()
rsp.Body = http.NoBody
sb := ctx.StateBag()
sb[DecompressionNotPossible] = true
sb[DecompressionError] = err
log.Errorf("Error while initializing decompression: %v", err)
return
}
rsp.Body = b
}