-
Notifications
You must be signed in to change notification settings - Fork 3
/
http_compression_config.go
281 lines (241 loc) · 7.91 KB
/
http_compression_config.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
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package serverconfigs
import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/lists"
"strings"
)
var DefaultHTTPCompressionTypes = []HTTPCompressionType{HTTPCompressionTypeBrotli, HTTPCompressionTypeGzip, HTTPCompressionTypeDeflate}
type HTTPCompressionRef struct {
Id int64 `yaml:"id" json:"id"`
IsOn bool `yaml:"isOn" json:"isOn"`
}
// HTTPCompressionConfig 内容压缩配置
type HTTPCompressionConfig struct {
IsPrior bool `yaml:"isPrior" json:"isPrior"`
IsOn bool `yaml:"isOn" json:"isOn"`
UseDefaultTypes bool `yaml:"useDefaultTypes" json:"useDefaultTypes"` // 是否使用默认的类型
Types []HTTPCompressionType `yaml:"types" json:"types"` // 支持的类型,如果为空表示默认顺序
Level int8 `yaml:"level" json:"level"` // 级别:1-12
DecompressData bool `yaml:"decompressData" json:"decompressData"` // 是否解压已压缩内容
GzipRef *HTTPCompressionRef `yaml:"gzipRef" json:"gzipRef"`
DeflateRef *HTTPCompressionRef `yaml:"deflateRef" json:"deflateRef"`
BrotliRef *HTTPCompressionRef `yaml:"brotliRef" json:"brotliRef"`
Gzip *HTTPGzipCompressionConfig `yaml:"gzip" json:"gzip"`
Deflate *HTTPDeflateCompressionConfig `yaml:"deflate" json:"deflate"`
Brotli *HTTPBrotliCompressionConfig `yaml:"brotli" json:"brotli"`
MinLength *shared.SizeCapacity `yaml:"minLength" json:"minLength"` // 最小压缩对象比如4m, 24k
MaxLength *shared.SizeCapacity `yaml:"maxLength" json:"maxLength"` // 最大压缩对象
MimeTypes []string `yaml:"mimeTypes" json:"mimeTypes"` // 支持的MimeType,支持image/*这样的通配符使用
Extensions []string `yaml:"extensions" json:"extensions"` // 文件扩展名,包含点符号,不区分大小写
ExceptExtensions []string `yaml:"exceptExtensions" json:"exceptExtensions"` // 例外扩展名
Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 匹配条件
EnablePartialContent bool `yaml:"enablePartialContent" json:"enablePartialContent"` // 支持PartialContent压缩
minLength int64
maxLength int64
mimeTypeRules []*shared.MimeTypeRule
extensions []string
exceptExtensions []string
types []HTTPCompressionType
supportGzip bool
supportDeflate bool
supportBrotli bool
supportZSTD bool
}
// Init 初始化
func (this *HTTPCompressionConfig) Init() error {
if this.MinLength != nil {
this.minLength = this.MinLength.Bytes()
}
if this.MaxLength != nil {
this.maxLength = this.MaxLength.Bytes()
}
if this.Conds != nil {
err := this.Conds.Init()
if err != nil {
return err
}
}
// mime types
this.mimeTypeRules = []*shared.MimeTypeRule{}
for _, mimeType := range this.MimeTypes {
rule, err := shared.NewMimeTypeRule(mimeType)
if err != nil {
return err
}
this.mimeTypeRules = append(this.mimeTypeRules, rule)
}
// extensions
this.extensions = []string{}
for _, ext := range this.Extensions {
ext = strings.ToLower(ext)
if len(ext) > 0 && ext[0] != '.' {
ext = "." + ext
}
this.extensions = append(this.extensions, ext)
}
this.exceptExtensions = []string{}
for _, ext := range this.ExceptExtensions {
ext = strings.ToLower(ext)
if len(ext) > 0 && ext[0] != '.' {
ext = "." + ext
}
this.exceptExtensions = append(this.exceptExtensions, ext)
}
if this.Gzip != nil {
err := this.Gzip.Init()
if err != nil {
return err
}
}
if this.Deflate != nil {
err := this.Deflate.Init()
if err != nil {
return err
}
}
if this.Brotli != nil {
err := this.Brotli.Init()
if err != nil {
return err
}
}
var supportedTypes = []HTTPCompressionType{}
if !this.UseDefaultTypes {
supportedTypes = append(supportedTypes, this.Types...)
} else {
supportedTypes = append(supportedTypes, DefaultHTTPCompressionTypes...)
}
this.types = supportedTypes
this.supportGzip = false
this.supportDeflate = false
this.supportDeflate = false
for _, supportType := range supportedTypes {
switch supportType {
case HTTPCompressionTypeGzip:
if this.GzipRef == nil || (this.GzipRef != nil && this.GzipRef.IsOn && this.Gzip != nil && this.Gzip.IsOn) {
this.supportGzip = true
}
case HTTPCompressionTypeDeflate:
if this.DeflateRef == nil || (this.DeflateRef != nil && this.DeflateRef.IsOn && this.Deflate != nil && this.Deflate.IsOn) {
this.supportDeflate = true
}
case HTTPCompressionTypeBrotli:
if this.BrotliRef == nil || (this.BrotliRef != nil && this.BrotliRef.IsOn && this.Brotli != nil && this.Brotli.IsOn) {
this.supportBrotli = true
}
case HTTPCompressionTypeZSTD:
this.supportZSTD = true
}
}
return nil
}
// MinBytes 可压缩最小尺寸
func (this *HTTPCompressionConfig) MinBytes() int64 {
return this.minLength
}
// MaxBytes 可压缩最大尺寸
func (this *HTTPCompressionConfig) MaxBytes() int64 {
return this.maxLength
}
// MatchResponse 是否匹配响应
func (this *HTTPCompressionConfig) MatchResponse(mimeType string, contentLength int64, requestExt string, formatter shared.Formatter) bool {
if this.Conds != nil && formatter != nil {
if !this.Conds.MatchRequest(formatter) {
return false
}
if !this.Conds.MatchResponse(formatter) {
return false
}
}
// min length
if this.minLength > 0 && contentLength < this.minLength {
return false
}
// max length
if this.maxLength > 0 && contentLength > this.maxLength {
return false
}
// except extensions
if len(this.exceptExtensions) > 0 {
if len(requestExt) > 0 {
for _, ext := range this.exceptExtensions {
if ext == requestExt {
return false
}
}
}
}
// extensions
if len(this.extensions) > 0 {
if len(requestExt) > 0 {
for _, ext := range this.extensions {
if ext == requestExt {
return true
}
}
}
}
// mime types
if len(this.mimeTypeRules) > 0 {
if len(mimeType) > 0 {
var index = strings.Index(mimeType, ";")
if index >= 0 {
mimeType = mimeType[:index]
}
for _, rule := range this.mimeTypeRules {
if rule.Match(mimeType) {
return true
}
}
}
}
// 如果没有指定条件,则所有的都能压缩
if len(this.extensions) == 0 && len(this.mimeTypeRules) == 0 {
return true
}
return false
}
// MatchAcceptEncoding 根据Accept-Encoding选择优先的压缩方式
func (this *HTTPCompressionConfig) MatchAcceptEncoding(acceptEncodings string) (compressionType HTTPCompressionType, compressionEncoding string, ok bool) {
if len(acceptEncodings) == 0 {
return
}
if len(this.types) == 0 {
return
}
var pieces = strings.Split(acceptEncodings, ",")
var encodings = []string{}
for _, piece := range pieces {
var qualityIndex = strings.Index(piece, ";")
if qualityIndex >= 0 {
// TODO 实现优先级
piece = piece[:qualityIndex]
}
encodings = append(encodings, strings.TrimSpace(piece))
}
if len(encodings) == 0 {
return
}
for _, supportType := range this.types {
switch supportType {
case HTTPCompressionTypeGzip:
if this.supportGzip && lists.ContainsString(encodings, "gzip") {
return HTTPCompressionTypeGzip, "gzip", true
}
case HTTPCompressionTypeDeflate:
if this.supportDeflate && lists.ContainsString(encodings, "deflate") {
return HTTPCompressionTypeDeflate, "deflate", true
}
case HTTPCompressionTypeBrotli:
if this.supportBrotli && lists.ContainsString(encodings, "br") {
return HTTPCompressionTypeBrotli, "br", true
}
case HTTPCompressionTypeZSTD:
if this.supportZSTD && lists.ContainsString(encodings, "zstd") {
return HTTPCompressionTypeZSTD, "zstd", true
}
}
}
return "", "", false
}