forked from ooni/oohttp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compression.go
288 lines (246 loc) · 7.21 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package http
import (
"bytes"
"compress/flate"
"compress/gzip"
"compress/lzw"
"compress/zlib"
"errors"
"fmt"
"io"
"strings"
"sync"
"github.com/andybalholm/brotli"
"github.com/klauspost/compress/zstd"
)
type CompressionFactory func(writer io.Writer) (io.Writer, error)
type DecompressionFactory func(reader io.Reader) (io.Reader, error)
type EncodingName = string
type CompressionRegistry map[EncodingName]CompressionFactory
type DecompressionRegistry map[EncodingName]DecompressionFactory
var (
DefaultCompressionRegistry = CompressionRegistry{
"": func(writer io.Writer) (io.Writer, error) { return writer, nil },
"identity": func(writer io.Writer) (io.Writer, error) { return writer, nil },
"gzip": func(writer io.Writer) (io.Writer, error) { return gzip.NewWriter(writer), nil },
"zlib": func(writer io.Writer) (io.Writer, error) { return zlib.NewWriter(writer), nil },
"br": func(writer io.Writer) (io.Writer, error) { return brotli.NewWriter(writer), nil },
"deflate": func(writer io.Writer) (io.Writer, error) { return flate.NewWriter(writer, -1) },
// TODO: Confirm compress
"compress": func(writer io.Writer) (io.Writer, error) { return lzw.NewWriter(writer, lzw.LSB, 8), nil },
"zstd": func(writer io.Writer) (io.Writer, error) { return zstd.NewWriter(writer) },
}
DefaultDecompressionRegistry = DecompressionRegistry{
"": func(reader io.Reader) (io.Reader, error) { return reader, nil },
"identity": func(reader io.Reader) (io.Reader, error) { return reader, nil },
"gzip": func(reader io.Reader) (io.Reader, error) { return gzip.NewReader(reader) },
"zlib": func(reader io.Reader) (io.Reader, error) { return zlib.NewReader(reader) },
"br": func(reader io.Reader) (io.Reader, error) { return brotli.NewReader(reader), nil },
"deflate": func(reader io.Reader) (io.Reader, error) { return flate.NewReader(reader), nil },
"compress": func(reader io.Reader) (io.Reader, error) { return lzw.NewReader(reader, lzw.LSB, 8), nil },
"zstd": func(reader io.Reader) (io.Reader, error) { return zstd.NewReader(reader) },
}
)
func compress(data []byte, registry CompressionRegistry, order ...EncodingName) ([]byte, error) {
var (
err error
writers []io.Writer
writer io.Writer
)
if registry == nil {
registry = DefaultCompressionRegistry
}
dst := bytes.NewBuffer(nil)
writer = dst
for idx, compression := range order {
// fmt.Println(fmt.Sprintf("Compression added: %s", compression))
mapping, ok := registry[compression]
if !ok {
return nil, errors.New(compression + " is not supported")
}
writer, err = mapping(writer)
if err != nil {
return nil, fmt.Errorf("mapping[%d:%s]: %w", idx, compression, err)
}
writers = append(writers, writer)
}
_, err = writers[len(writers)-1].Write(data)
if err != nil {
return nil, fmt.Errorf("writer.Write: %w", err)
}
// Close all writers in reverse order to ensure all data is flushed
for i := len(writers) - 1; i >= 0; i-- {
err = writers[i].(io.Closer).Close()
if err != nil {
return nil, fmt.Errorf("writers[%d].(io.Closer).Close: %w", i, err)
}
}
// fmt.Printf("lenIn: %d lenOut: %d\n", len(data), dst.Len())
return dst.Bytes(), nil
}
// CompressorWriter compressor writer
type CompressorWriter struct {
io.Writer
Registry CompressionRegistry
Order []EncodingName
wrs []io.Writer
once sync.Once
}
var _ io.WriteCloser = (*CompressorWriter)(nil)
func (cw *CompressorWriter) init() error {
if cw.Registry == nil {
cw.Registry = DefaultCompressionRegistry
}
cw.wrs = nil
for i := 0; i < len(cw.Order); i++ {
directive := cw.Order[i]
directive = strings.Trim(directive, " ")
if directive == "" {
continue
}
compressorWrapper, exist := cw.Registry[directive]
if !exist {
return fmt.Errorf("%s is not supported", directive)
}
writer, err := compressorWrapper(cw.Writer)
if err != nil {
return fmt.Errorf("compressor wrapper init: %s: %w", directive, err)
}
cw.wrs = append(cw.wrs, writer)
cw.Writer = writer
}
return nil
}
// Init initialize decompressor early instead of lazy-initialize on first read op
func (cw *CompressorWriter) Init() (err error) {
cw.once.Do(func() {
err = cw.init()
})
return
}
// Write write buffer to compressor
func (cw *CompressorWriter) Write(b []byte) (nb int, err error) {
cw.once.Do(func() {
err = cw.init()
})
if err != nil {
return
}
nb, err = cw.Writer.Write(b)
return
}
// Close close compressor
func (cw *CompressorWriter) Close() error {
for i := len(cw.wrs) - 1; i >= 0; i-- {
if closer, ok := cw.wrs[i].(io.Closer); ok {
err := closer.Close()
if err != nil {
return err
}
}
}
return nil
}
func decompress(data []byte, registry DecompressionRegistry, order ...EncodingName) ([]byte, error) {
var (
err error
reader io.Reader
readers []io.Reader
)
if registry == nil {
registry = DefaultDecompressionRegistry
}
src := bytes.NewBuffer(data)
reader = src
readers = append(readers, src)
// Reverse the order of compressions for decompression
for idx := 0; idx < len(order); idx++ {
compression := order[idx]
// fmt.Println(fmt.Sprintf("Decompression added: %s", compression))
mapping, ok := registry[compression]
if !ok {
return nil, errors.New(compression + " is not supported")
}
reader, err = mapping(reader)
if err != nil {
return nil, fmt.Errorf("mapping[%d:%s]: %w", idx, compression, err)
}
readers = append(readers, reader)
}
dataOut, err := io.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("io.ReadAll: %w", err)
}
for _, readerObj := range readers {
typedReader, ok := readerObj.(io.Closer)
if ok {
defer typedReader.Close()
}
}
// fmt.Printf("lenIn: %d lenOut: %d\n", len(data), len(dataOut))
return dataOut, nil
}
// DecompressorReader decompressor reader
type DecompressorReader struct {
io.Reader
Registry DecompressionRegistry
Order []EncodingName
rds []io.Reader
once sync.Once
}
var _ io.ReadCloser = (*DecompressorReader)(nil)
func (dr *DecompressorReader) init() error {
if dr.Registry == nil {
dr.Registry = DefaultDecompressionRegistry
}
dr.rds = nil
for i := 0; i < len(dr.Order); i++ {
directive := dr.Order[i]
directive = strings.Trim(directive, " ")
if directive == "" {
continue
}
// fmt.Println(directive)
decompressorWrapper, exist := dr.Registry[directive]
if !exist {
return fmt.Errorf("%s is not supported", directive)
}
reader, err := decompressorWrapper(dr.Reader)
if err != nil {
return fmt.Errorf("decompressor wrapper init: %s: %w", directive, err)
}
dr.rds = append(dr.rds, reader)
dr.Reader = reader
}
return nil
}
// Init initialize decompressor early instead of lazy-initialize on first read op
func (dr *DecompressorReader) Init() (err error) {
dr.once.Do(func() {
err = dr.init()
})
return
}
// Read read buffer from decompressor
func (dr *DecompressorReader) Read(b []byte) (nb int, err error) {
dr.once.Do(func() {
err = dr.init()
})
if err != nil {
return
}
nb, err = dr.Reader.Read(b)
return
}
// Close close decompressor
func (dr *DecompressorReader) Close() error {
for i := len(dr.rds) - 1; i >= 0; i-- {
if closer, ok := dr.rds[i].(io.Closer); ok {
err := closer.Close()
if err != nil {
return err
}
}
}
return nil
}