mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-01-28 04:05:54 +08:00
实现gzip
This commit is contained in:
@@ -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"` // 是否为请求的条件,用来区分在什么阶段执行
|
||||
|
||||
// 要测试的字符串
|
||||
// 其中可以使用跟请求相关的参数,比如:
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
141
pkg/serverconfigs/shared/http_request_cond_group_test.go
Normal file
141
pkg/serverconfigs/shared/http_request_cond_group_test.go
Normal 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
|
||||
}))
|
||||
}
|
||||
}
|
||||
69
pkg/serverconfigs/shared/http_request_conds_config.go
Normal file
69
pkg/serverconfigs/shared/http_request_conds_config.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user