mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-03-09 22:15:39 +08:00
支持brotli和deflate压缩方式
This commit is contained in:
5
pkg/serverconfigs/shared/formatter.go
Normal file
5
pkg/serverconfigs/shared/formatter.go
Normal file
@@ -0,0 +1,5 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package shared
|
||||
|
||||
type Formatter = func(s string) string
|
||||
@@ -40,8 +40,8 @@ func (this *HTTPRequestCondsConfig) Init() error {
|
||||
}
|
||||
|
||||
// MatchRequest 判断请求是否匹配
|
||||
func (this *HTTPRequestCondsConfig) MatchRequest(formatter func(s string) string) bool {
|
||||
if !this.IsOn && len(this.Groups) == 0 {
|
||||
func (this *HTTPRequestCondsConfig) MatchRequest(formatter Formatter) bool {
|
||||
if !this.IsOn || len(this.Groups) == 0 {
|
||||
return true
|
||||
}
|
||||
ok := false
|
||||
@@ -63,7 +63,7 @@ func (this *HTTPRequestCondsConfig) MatchRequest(formatter func(s string) string
|
||||
|
||||
// MatchResponse 判断响应是否匹配
|
||||
func (this *HTTPRequestCondsConfig) MatchResponse(formatter func(s string) string) bool {
|
||||
if !this.IsOn && len(this.Groups) == 0 {
|
||||
if !this.IsOn || len(this.Groups) == 0 {
|
||||
return true
|
||||
}
|
||||
ok := false
|
||||
|
||||
43
pkg/serverconfigs/shared/mime_type_rule.go
Normal file
43
pkg/serverconfigs/shared/mime_type_rule.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// MimeTypeRule mime type规则
|
||||
type MimeTypeRule struct {
|
||||
Value string
|
||||
|
||||
isAll bool
|
||||
regexp *regexp.Regexp
|
||||
}
|
||||
|
||||
func NewMimeTypeRule(mimeType string) (*MimeTypeRule, error) {
|
||||
mimeType = strings.ToLower(mimeType)
|
||||
|
||||
var rule = &MimeTypeRule{
|
||||
Value: mimeType,
|
||||
}
|
||||
if mimeType == "*/*" || mimeType == "*" {
|
||||
rule.isAll = true
|
||||
} else if strings.Contains(mimeType, "*") {
|
||||
mimeType = strings.ReplaceAll(regexp.QuoteMeta(mimeType), `\*`, ".+")
|
||||
reg, err := regexp.Compile("^(?i)" + mimeType + "$")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rule.regexp = reg
|
||||
}
|
||||
return rule, nil
|
||||
}
|
||||
|
||||
func (this *MimeTypeRule) Match(mimeType string) bool {
|
||||
if this.isAll {
|
||||
return true
|
||||
}
|
||||
if this.regexp == nil {
|
||||
return this.Value == strings.ToLower(mimeType)
|
||||
}
|
||||
return this.regexp.MatchString(mimeType)
|
||||
}
|
||||
49
pkg/serverconfigs/shared/mime_type_rule_test.go
Normal file
49
pkg/serverconfigs/shared/mime_type_rule_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package shared
|
||||
|
||||
import (
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMimeTypeRule_Match(t *testing.T) {
|
||||
a := assert.NewAssertion(t)
|
||||
|
||||
{
|
||||
rule, err := NewMimeTypeRule("text/plain")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a.IsTrue(rule.Match("text/plain"))
|
||||
a.IsTrue(rule.Match("TEXT/plain"))
|
||||
a.IsFalse(rule.Match("text/html"))
|
||||
}
|
||||
|
||||
{
|
||||
rule, err := NewMimeTypeRule("image/*")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a.IsTrue(rule.Match("image/png"))
|
||||
a.IsTrue(rule.Match("IMAGE/jpeg"))
|
||||
a.IsFalse(rule.Match("image/"))
|
||||
a.IsFalse(rule.Match("image1/png"))
|
||||
a.IsFalse(rule.Match("x-image/png"))
|
||||
}
|
||||
|
||||
{
|
||||
_, err := NewMimeTypeRule("x-image/*")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
rule, err := NewMimeTypeRule("*/*")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a.IsTrue(rule.Match("any/thing"))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user