Files
EdgeCommon/pkg/serverconfigs/shared/http_request_conds_config.go

95 lines
2.1 KiB
Go
Raw Normal View History

2020-09-29 17:23:11 +08:00
package shared
2021-06-09 17:15:12 +08:00
// HTTPRequestCondsConfig 条件配置
2020-09-29 17:23:11 +08:00
// 数据结构conds -> []groups -> []cond
type HTTPRequestCondsConfig struct {
IsOn bool `yaml:"isOn" json:"isOn"`
Connector string `yaml:"connector" json:"connector"`
Groups []*HTTPRequestCondGroup `yaml:"groups" json:"groups"`
2020-10-05 16:54:21 +08:00
hasRequestConds bool
hasResponseConds bool
2020-09-29 17:23:11 +08:00
}
2021-06-09 17:15:12 +08:00
// Init 初始化
2020-09-29 17:23:11 +08:00
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
}
}
2020-10-05 16:54:21 +08:00
// 是否有请求条件
for _, group := range this.Groups {
if group.IsOn {
if group.HasRequestConds() {
this.hasRequestConds = true
}
if group.HasResponseConds() {
this.hasResponseConds = true
}
}
}
2020-09-29 17:23:11 +08:00
return nil
}
2021-06-09 17:15:12 +08:00
// MatchRequest 判断请求是否匹配
2020-09-29 17:23:11 +08:00
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
}
2021-06-09 17:15:12 +08:00
// MatchResponse 判断响应是否匹配
2020-09-29 17:23:11 +08:00
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
}
2020-10-05 16:54:21 +08:00
2021-06-09 17:15:12 +08:00
// HasRequestConds 判断是否有请求条件
2020-10-05 16:54:21 +08:00
func (this *HTTPRequestCondsConfig) HasRequestConds() bool {
return this.hasRequestConds
}
2021-06-09 17:15:12 +08:00
// HasResponseConds 判断是否有响应条件
2020-10-05 16:54:21 +08:00
func (this *HTTPRequestCondsConfig) HasResponseConds() bool {
return this.hasResponseConds
}