实现gzip

This commit is contained in:
刘祥超
2020-09-29 17:23:11 +08:00
parent 738c74d99f
commit cdc7dfcc36
16 changed files with 375 additions and 358 deletions

View File

@@ -2,7 +2,6 @@ package serverconfigs
import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"strings"
)
// 默认的文件类型
@@ -12,14 +11,15 @@ var (
// gzip配置
type HTTPGzipConfig struct {
Id int64 `yaml:"id" json:"id"` // ID
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
Level int8 `yaml:"level" json:"level"` // 1-9
MinLength *shared.SizeCapacity `yaml:"minLength" json:"minLength"` // 最小压缩对象比如4m, 24k
MaxLength *shared.SizeCapacity `yaml:"maxLength" json:"maxLength"` // 最大压缩对象 TODO 需要实现
CondGroups []*shared.HTTPRequestCondGroup `yaml:"condGroups" json:"condGroups"` // 匹配条件
Id int64 `yaml:"id" json:"id"` // ID
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
Level int8 `yaml:"level" json:"level"` // 1-9
MinLength *shared.SizeCapacity `yaml:"minLength" json:"minLength"` // 最小压缩对象比如4m, 24k
MaxLength *shared.SizeCapacity `yaml:"maxLength" json:"maxLength"` // 最大压缩对象
Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 匹配条件
minLength int64
maxLength int64
mimeTypes []*MimeTypeRule
}
@@ -28,6 +28,16 @@ func (this *HTTPGzipConfig) 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
}
}
return nil
}
@@ -37,18 +47,7 @@ func (this *HTTPGzipConfig) MinBytes() int64 {
return this.minLength
}
// 检查是否匹配Content-Type
func (this *HTTPGzipConfig) MatchContentType(contentType string) bool {
index := strings.Index(contentType, ";")
if index >= 0 {
contentType = contentType[:index]
}
for _, mimeType := range this.mimeTypes {
if mimeType.Regexp == nil && contentType == mimeType.Value {
return true
} else if mimeType.Regexp != nil && mimeType.Regexp.MatchString(contentType) {
return true
}
}
return false
// 可压缩最大尺寸
func (this *HTTPGzipConfig) MaxBytes() int64 {
return this.maxLength
}