实现gzip

This commit is contained in:
GoEdgeLab
2020-09-29 17:23:11 +08:00
parent 3af4c6052c
commit aa0ca23211
16 changed files with 375 additions and 358 deletions

View File

@@ -18,7 +18,8 @@ import (
// 重写条件定义
type HTTPRequestCond struct {
Type string `yaml:"type" json:"type"` // 类型,在特殊条件时使用
Type string `yaml:"type" json:"type"` // 类型,在特殊条件时使用
IsRequest bool `yaml:"isRequest" json:"isRequest"` // 是否为请求的条件,用来区分在什么阶段执行
// 要测试的字符串
// 其中可以使用跟请求相关的参数,比如:

View File

@@ -7,26 +7,48 @@ type HTTPRequestCondGroup struct {
Conds []*HTTPRequestCond `yaml:"conds" json:"conds"` // 条件列表
IsReverse bool `yaml:"isReverse" json:"isReverse"` // 是否反向匹配
Description string `yaml:"description" json:"description"` // 说明
requestConds []*HTTPRequestCond
responseConds []*HTTPRequestCond
}
// 初始化
func (this *HTTPRequestCondGroup) Init() error {
if len(this.Connector) == 0 {
this.Connector = "or"
}
if len(this.Conds) > 0 {
for _, cond := range this.Conds {
err := cond.Init()
if err != nil {
return err
}
if cond.IsRequest {
this.requestConds = append(this.requestConds, cond)
} else {
this.responseConds = append(this.responseConds, cond)
}
}
}
return nil
}
func (this *HTTPRequestCondGroup) Match(formatter func(source string) string) bool {
if !this.IsOn {
func (this *HTTPRequestCondGroup) MatchRequest(formatter func(source string) string) bool {
return this.match(this.requestConds, formatter)
}
func (this *HTTPRequestCondGroup) MatchResponse(formatter func(source string) string) bool {
return this.match(this.responseConds, formatter)
}
func (this *HTTPRequestCondGroup) match(conds []*HTTPRequestCond, formatter func(source string) string) bool {
if !this.IsOn || len(conds) == 0 {
return !this.IsReverse
}
for _, cond := range this.Conds {
ok := false
for _, cond := range conds {
isMatched := cond.Match(formatter)
if this.Connector == "or" && isMatched {
return !this.IsReverse
@@ -34,6 +56,13 @@ func (this *HTTPRequestCondGroup) Match(formatter func(source string) string) bo
if this.Connector == "and" && !isMatched {
return this.IsReverse
}
if isMatched {
// 对于OR来说至少要有一个返回true
ok = true
}
}
return !this.IsReverse
if this.IsReverse {
return !ok
}
return ok
}

View File

@@ -0,0 +1,141 @@
package shared
import (
"github.com/iwind/TeaGo/assert"
"testing"
)
func TestHTTPRequestCondGroup_MatchRequest(t *testing.T) {
a := assert.NewAssertion(t)
{
group := &HTTPRequestCondGroup{}
group.Connector = "or"
group.IsOn = false
err := group.Init()
if err != nil {
t.Fatal(err)
}
a.IsTrue(group.MatchRequest(func(source string) string {
return source
}))
a.IsTrue(group.MatchResponse(func(source string) string {
return source
}))
}
{
group := &HTTPRequestCondGroup{}
group.IsOn = true
err := group.Init()
if err != nil {
t.Fatal(err)
}
a.IsTrue(group.MatchRequest(func(source string) string {
return source
}))
a.IsTrue(group.MatchResponse(func(source string) string {
return source
}))
}
{
group := &HTTPRequestCondGroup{}
group.IsOn = true
group.Connector = "or"
group.Conds = []*HTTPRequestCond{
{
IsRequest: true,
Param: "456",
Operator: "gt",
Value: "123",
},
{
IsRequest: false,
Param: "123",
Operator: "gt",
Value: "456",
},
}
err := group.Init()
if err != nil {
t.Fatal(err)
}
a.IsTrue(group.MatchRequest(func(source string) string {
return source
}))
a.IsFalse(group.MatchResponse(func(source string) string {
return source
}))
}
{
group := &HTTPRequestCondGroup{}
group.IsOn = true
group.Connector = "or"
group.Conds = []*HTTPRequestCond{
{
IsRequest: true,
Param: "456",
Operator: "gt",
Value: "1234",
},
{
IsRequest: true,
Param: "456",
Operator: "gt",
Value: "123",
},
{
IsRequest: false,
Param: "123",
Operator: "gt",
Value: "456",
},
}
err := group.Init()
if err != nil {
t.Fatal(err)
}
a.IsTrue(group.MatchRequest(func(source string) string {
return source
}))
a.IsFalse(group.MatchResponse(func(source string) string {
return source
}))
}
{
group := &HTTPRequestCondGroup{}
group.IsOn = true
group.Connector = "and"
group.Conds = []*HTTPRequestCond{
{
IsRequest: true,
Param: "456",
Operator: "gt",
Value: "123",
},
{
IsRequest: true,
Param: "456",
Operator: "gt",
Value: "1234",
},
{
IsRequest: false,
Param: "123",
Operator: "gt",
Value: "456",
},
}
err := group.Init()
if err != nil {
t.Fatal(err)
}
a.IsFalse(group.MatchRequest(func(source string) string {
return source
}))
a.IsFalse(group.MatchResponse(func(source string) string {
return source
}))
}
}

View File

@@ -0,0 +1,69 @@
package shared
// 条件配置
// 数据结构conds -> []groups -> []cond
type HTTPRequestCondsConfig struct {
IsOn bool `yaml:"isOn" json:"isOn"`
Connector string `yaml:"connector" json:"connector"`
Groups []*HTTPRequestCondGroup `yaml:"groups" json:"groups"`
}
// 初始化
func (this *HTTPRequestCondsConfig) Init() error {
if len(this.Connector) == 0 {
this.Connector = "or"
}
for _, group := range this.Groups {
err := group.Init()
if err != nil {
return err
}
}
return nil
}
// 判断请求是否匹配
func (this *HTTPRequestCondsConfig) MatchRequest(formatter func(s string) string) bool {
if !this.IsOn && len(this.Groups) == 0 {
return true
}
ok := false
for _, group := range this.Groups {
b := group.MatchRequest(formatter)
if !b && this.Connector == "and" {
return false
}
if b && this.Connector == "or" {
return true
}
if b {
// 对于 or 来说至少有一个分组要返回 true
ok = true
}
}
return ok
}
// 判断响应是否匹配
func (this *HTTPRequestCondsConfig) MatchResponse(formatter func(s string) string) bool {
if !this.IsOn && len(this.Groups) == 0 {
return true
}
ok := false
for _, group := range this.Groups {
b := group.MatchResponse(formatter)
if !b && this.Connector == "and" {
return false
}
if b && this.Connector == "or" {
return true
}
if b {
// 对于 or 来说至少有一个分组要返回 true
ok = true
}
}
return ok
}