2020-09-26 08:07:24 +08:00
|
|
|
package shared
|
|
|
|
|
|
|
|
|
|
// 请求条件分组
|
|
|
|
|
type HTTPRequestCondGroup struct {
|
2020-09-29 11:28:52 +08:00
|
|
|
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
|
|
|
|
|
Connector string `yaml:"connector" json:"connector"` // 条件之间的关系
|
|
|
|
|
Conds []*HTTPRequestCond `yaml:"conds" json:"conds"` // 条件列表
|
|
|
|
|
IsReverse bool `yaml:"isReverse" json:"isReverse"` // 是否反向匹配
|
|
|
|
|
Description string `yaml:"description" json:"description"` // 说明
|
2020-09-29 17:23:11 +08:00
|
|
|
|
|
|
|
|
requestConds []*HTTPRequestCond
|
|
|
|
|
responseConds []*HTTPRequestCond
|
2020-09-26 08:07:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化
|
|
|
|
|
func (this *HTTPRequestCondGroup) Init() error {
|
2020-09-29 17:23:11 +08:00
|
|
|
if len(this.Connector) == 0 {
|
|
|
|
|
this.Connector = "or"
|
|
|
|
|
}
|
2020-10-05 16:54:21 +08:00
|
|
|
|
2020-09-26 08:07:24 +08:00
|
|
|
if len(this.Conds) > 0 {
|
|
|
|
|
for _, cond := range this.Conds {
|
|
|
|
|
err := cond.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-09-29 17:23:11 +08:00
|
|
|
|
|
|
|
|
if cond.IsRequest {
|
|
|
|
|
this.requestConds = append(this.requestConds, cond)
|
|
|
|
|
} else {
|
|
|
|
|
this.responseConds = append(this.responseConds, cond)
|
|
|
|
|
}
|
2020-09-26 08:07:24 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 17:23:11 +08:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 16:54:21 +08:00
|
|
|
func (this *HTTPRequestCondGroup) HasRequestConds() bool {
|
|
|
|
|
return len(this.requestConds) > 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *HTTPRequestCondGroup) HasResponseConds() bool {
|
|
|
|
|
return len(this.responseConds) > 0
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 17:23:11 +08:00
|
|
|
func (this *HTTPRequestCondGroup) match(conds []*HTTPRequestCond, formatter func(source string) string) bool {
|
|
|
|
|
if !this.IsOn || len(conds) == 0 {
|
2020-09-26 08:07:24 +08:00
|
|
|
return !this.IsReverse
|
|
|
|
|
}
|
2020-09-29 17:23:11 +08:00
|
|
|
ok := false
|
|
|
|
|
for _, cond := range conds {
|
2020-09-26 08:07:24 +08:00
|
|
|
isMatched := cond.Match(formatter)
|
|
|
|
|
if this.Connector == "or" && isMatched {
|
|
|
|
|
return !this.IsReverse
|
|
|
|
|
}
|
|
|
|
|
if this.Connector == "and" && !isMatched {
|
|
|
|
|
return this.IsReverse
|
|
|
|
|
}
|
2020-09-29 17:23:11 +08:00
|
|
|
if isMatched {
|
|
|
|
|
// 对于OR来说至少要有一个返回true
|
|
|
|
|
ok = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if this.IsReverse {
|
|
|
|
|
return !ok
|
2020-09-26 08:07:24 +08:00
|
|
|
}
|
2020-09-29 17:23:11 +08:00
|
|
|
return ok
|
2020-09-26 08:07:24 +08:00
|
|
|
}
|