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
|
package ext
import (
"compress/gzip"
"compress/lzw"
"errors"
"io"
"log/slog"
"net/http"
"strconv"
"strings"
"git.gabrielgio.me/cerrado/pkg/u"
"github.com/andybalholm/brotli"
"github.com/klauspost/compress/zstd"
)
var (
invalidParamErr = errors.New("Invalid weighted param")
)
type CompressionResponseWriter struct {
innerWriter http.ResponseWriter
compressWriter io.Writer
}
func Compress(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if accept, ok := r.Header["Accept-Encoding"]; ok {
if compress, algo := GetCompressionWriter(u.FirstOrZero(accept), w); algo != "" {
defer compress.Close()
w.Header().Add("Content-Encoding", algo)
w = &CompressionResponseWriter{
innerWriter: w,
compressWriter: compress,
}
}
}
next(w, r)
}
}
func GetCompressionWriter(header string, inner io.Writer) (io.WriteCloser, string) {
c := GetCompression(header)
switch c {
case "br":
return GetBrotliWriter(inner), c
case "gzip":
return GetGZIPWriter(inner), c
case "compress":
return GetLZWWriter(inner), c
case "zstd":
return GetZSTDWriter(inner), c
default:
return nil, ""
}
}
func (c *CompressionResponseWriter) Header() http.Header {
return c.innerWriter.Header()
}
func (c *CompressionResponseWriter) Write(b []byte) (int, error) {
return c.compressWriter.Write(b)
}
func (c *CompressionResponseWriter) WriteHeader(statusCode int) {
c.innerWriter.WriteHeader(statusCode)
}
func GetCompression(header string) string {
c := "*"
q := 0.0
if header == "" {
return c
}
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding
for _, e := range strings.Split(header, ",") {
ps := strings.Split(e, ";")
if len(ps) == 2 {
w, err := getWeighedValue(ps[1])
if err != nil {
slog.Error(
"Error parsing weighting from Accept-Encoding",
"error", err,
)
continue
}
// gettting weighting value
if w > q {
q = w
c = strings.Trim(ps[0], " ")
}
} else {
if 1 > q {
q = 1
c = strings.Trim(ps[0], " ")
}
}
}
return c
}
func GetGZIPWriter(w io.Writer) io.WriteCloser {
// error can be ignored here since it will only err when compression level
// is not valid
r, _ := gzip.NewWriterLevel(w, gzip.BestCompression)
return r
}
func GetBrotliWriter(w io.Writer) io.WriteCloser {
return brotli.NewWriterLevel(w, brotli.BestCompression)
}
func GetZSTDWriter(w io.Writer) io.WriteCloser {
// error can be ignored here since it will only opts are given
r, _ := zstd.NewWriter(w)
return r
}
func GetLZWWriter(w io.Writer) io.WriteCloser {
return lzw.NewWriter(w, lzw.LSB, 8)
}
func getWeighedValue(part string) (float64, error) {
ps := strings.SplitN(part, "=", 2)
if len(ps) != 2 {
return 0, invalidParamErr
}
if name := strings.TrimSpace(ps[0]); name == "q" {
w, err := strconv.ParseFloat(ps[1], 64)
if err != nil {
return 0, err
}
return w, nil
}
return 0, invalidParamErr
}
|