-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
267 lines (234 loc) · 6.92 KB
/
handlers.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
package handler
import (
"compress/gzip"
"fmt"
"io"
"math"
"net/http"
"regexp"
"sort"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
)
// EncodingType is type for Encodings
type EncodingType string
const (
// AES128GCM is the const for encoding aes128gcm
AES128GCM EncodingType = "aes128gcm"
// BR is the const for encoding br
BR EncodingType = "br"
// Compress is the const for encoding compress
Compress EncodingType = "compress"
// Deflate is the const for encoding deflate
Deflate EncodingType = "deflate"
// EXI is the const for encoding exi
EXI EncodingType = "exi"
// GZip is the const for encoding gzip
GZip EncodingType = "gzip"
// Identity is the const for encoding identity
Identity EncodingType = "identity"
// Pack200GZip is the const for encoding pack200-gzip
Pack200GZip EncodingType = "pack200-gzip"
// ZStd is the const for encoding zstd
ZStd EncodingType = "zstd"
// XCompress is the const for encoding x-compress
XCompress EncodingType = "x-compress"
// XGZip is the const for encoding x-gzip
XGZip EncodingType = "x-gzip"
// All is the const for encoding *
All EncodingType = "*"
)
const preferEncoding = Identity
type acceptEncodingItem struct {
encoding EncodingType
qvalue float64
}
type sortedAcceptEncodingList []acceptEncodingItem
type disabledEncodingMap map[EncodingType]bool
type acceptEncoding struct {
sortAcceptEncodings sortedAcceptEncodingList
disabledEncodings disabledEncodingMap
}
// https://tools.ietf.org/html/rfc7231#section-5.3.1
const qvalueExp = "^q=((1(\\.0{0,3})?)|(0(\\.\\d{0,3})?))$"
type sortedAcceptEncodings []acceptEncoding
func verifyEncodingName(name string) EncodingType {
enc := EncodingType(strings.TrimSpace(name))
switch enc {
case AES128GCM, BR, Compress, Deflate, EXI, GZip,
Identity, Pack200GZip, ZStd, All:
return enc
case XCompress:
return Compress
case XGZip:
return GZip
default:
}
return ""
}
// For https://tools.ietf.org/html/rfc7231#section-5.3.1
func getQValue(qv string) float64 {
qv = strings.TrimSpace(qv)
if matched, err := regexp.MatchString(qvalueExp, qv); !matched || err != nil {
if err != nil {
log.Errorf("Error %v while match expression with %s.", err, qvalueExp)
}
return math.NaN()
}
num := qv[2:]
// error can be ignored, because the input has already
// verified by the regular expression
ret, _ := strconv.ParseFloat(num, 64)
return ret
}
func newAcceptEncoding() acceptEncoding {
accEncoding := acceptEncoding{}
accEncoding.disabledEncodings = make(disabledEncodingMap)
accEncoding.sortAcceptEncodings = make(sortedAcceptEncodingList, 0)
return accEncoding
}
func (a acceptEncoding) selectAcceptEncoding(encs map[EncodingType]bool, r *http.Request) EncodingType {
a.parseRequest(r)
for _, accenc := range a.sortAcceptEncodings {
enc := accenc.encoding
if accenc.encoding == All {
// Return preferEncoding directly.
// TODO, callers can set this in the future.
enc = preferEncoding
}
if encs[enc] {
// The encoding is suppoored by the handler
if !a.disabledEncodings[enc] {
return enc
}
// The coding is disabled
continue
}
}
return ""
}
func (a *acceptEncoding) parseRequest(r *http.Request) {
values, ok := r.Header["Accept-Encoding"]
if !ok {
// No Accept-Encoding header found
a.sortAcceptEncodings = append(a.sortAcceptEncodings,
acceptEncodingItem{All, 1.0})
return
}
if len(values) > 1 {
log.Warnf("Multiple Accept-Encoding header found in request, the values are %v. Only the first one %s will be used.", values, values[0])
}
headerValue := values[0]
if len(headerValue) == 0 {
// Accept-Encoding is not found, returns identity directly.
a.sortAcceptEncodings = append(a.sortAcceptEncodings,
acceptEncodingItem{Identity, 1.0})
return
}
// https://tools.ietf.org/html/rfc7231#section-3.1.2.1
// The value of encoding is case-insensitive
// So convert the value to lower case
headerValue = strings.ToLower(headerValue)
for _, oneEnc := range strings.Split(headerValue, ",") {
a.addOneAcceptEncoding(oneEnc)
}
// sort
sort.Slice(a.sortAcceptEncodings, func(i, j int) bool {
if math.Abs(a.sortAcceptEncodings[i].qvalue-a.sortAcceptEncodings[j].qvalue) < 0.0001 {
// The two qvalud are the same
if a.sortAcceptEncodings[i].encoding == "*" {
return false
}
if a.sortAcceptEncodings[j].encoding == "*" {
return true
}
// Dont swap the two encodings with same qvalue.
return false
}
return a.sortAcceptEncodings[i].qvalue > a.sortAcceptEncodings[j].qvalue
})
}
func (a *acceptEncoding) addOneAcceptEncoding(oneEnc string) {
fs := strings.Split(oneEnc, ";")
if len(fs) < 1 || len(fs) > 2 {
// This is an invalid Accept-Encoding defination
return
}
encName := verifyEncodingName(fs[0])
if len(encName) == 0 {
// the encoding name doesn't have any content, this is an invalid Accept-Encoding defination
return
}
item := acceptEncodingItem{encName, 1.0}
if len(fs) == 2 {
item.qvalue = getQValue(fs[1])
if math.IsNaN(item.qvalue) {
// This is an invalid qvalue.
return
}
if item.qvalue-0.0 < 0.0001 {
// Equals to zero, that means the encoding is disabled.
a.disabledEncodings[encName] = true
return
}
}
a.sortAcceptEncodings = append(a.sortAcceptEncodings, item)
}
type gzipWriter struct {
httpw http.ResponseWriter
gzipw io.Writer
}
func (g *gzipWriter) Write(b []byte) (int, error) {
return g.gzipw.Write(b)
}
func (g *gzipWriter) WriteHeader(statusCode int) {
g.httpw.WriteHeader(statusCode)
}
func (g *gzipWriter) Header() http.Header {
return g.httpw.Header()
}
func gzipWrapper(next http.Handler, w http.ResponseWriter, r *http.Request) {
// gzip
gzipw := gzip.NewWriter(w)
defer gzipw.Close()
gw := gzipWriter{
httpw: w,
gzipw: gzipw,
}
gw.Header().Add("Content-Encoding", "gzip")
next.ServeHTTP(&gw, r)
}
// EncodingHandler handles http requests with "Accept-Encoding" header
func EncodingHandler(allowedEncodingList []EncodingType, next http.Handler) (http.Handler, error) {
if allowedEncodingList == nil || len(allowedEncodingList) == 0 {
log.Warnf("Inputed allowedEncodingList is null or empty.")
return next, fmt.Errorf("no item in allowedEncodingList")
}
allowedEncMap := make(map[EncodingType]bool, len(allowedEncodingList))
for _, encStr := range allowedEncodingList {
if enc := verifyEncodingName(string(encStr)); enc != "" {
allowedEncMap[enc] = true
} else {
log.Warnf("Unknow encoding %s.", encStr)
}
}
// No allowed encoding list was passed
if len(allowedEncMap) == 0 {
log.Warnf("No valid encoding in allowedEncodingList %v.", allowedEncodingList)
return next, fmt.Errorf("no valid encoding in allowedEncodingList")
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
accencs := newAcceptEncoding()
selenc := accencs.selectAcceptEncoding(allowedEncMap, r)
switch selenc {
case GZip:
gzipWrapper(next, w, r)
return
case Identity:
next.ServeHTTP(w, r)
return
}
w.WriteHeader(http.StatusNotAcceptable)
}), nil
}