-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompressor.go
More file actions
50 lines (38 loc) · 930 Bytes
/
compressor.go
File metadata and controls
50 lines (38 loc) · 930 Bytes
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
package main
import (
"bytes"
"compress/gzip"
"io"
)
// Compressor defines request/response compression behavior
// Currently only gzip is implemented as Nauthilus supports gzip exclusively.
type Compressor interface {
Name() string
Compress(data []byte) ([]byte, error)
Decompress(r io.Reader) (io.ReadCloser, error)
}
type GzipCompressor struct{}
func (GzipCompressor) Name() string {
return "gzip"
}
func (GzipCompressor) Compress(data []byte) ([]byte, error) {
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
if _, err := zw.Write(data); err != nil {
_ = zw.Close()
return nil, err
}
if err := zw.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (GzipCompressor) Decompress(r io.Reader) (io.ReadCloser, error) {
zr, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
return zr, nil
}
var _ Compressor = GzipCompressor{}
var gzipCompressor = GzipCompressor{}