支持brotli和deflate压缩方式

This commit is contained in:
GoEdgeLab
2021-09-29 19:37:32 +08:00
parent f90c362785
commit 8fabdd579e
16 changed files with 836 additions and 286 deletions

View File

@@ -0,0 +1,5 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package shared
type Formatter = func(s string) string

View File

@@ -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

View 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)
}

View 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"))
}
}