-
Notifications
You must be signed in to change notification settings - Fork 34
/
header.go
68 lines (54 loc) · 1.35 KB
/
header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package shared
import (
"github.com/iwind/TeaGo/utils/string"
"regexp"
)
var regexpNamedVariable = regexp.MustCompile("\\${[\\w.-]+}")
// 头部信息定义
type HeaderConfig struct {
On bool `yaml:"on" json:"on"` // 是否开启
Id string `yaml:"id" json:"id"` // ID
Name string `yaml:"name" json:"name"` // Name
Value string `yaml:"value" json:"value"` // Value
Always bool `yaml:"always" json:"always"` // 是否忽略状态码
Status []int `yaml:"status" json:"status"` // 支持的状态码
statusMap map[int]bool
hasVariables bool
}
// 获取新Header对象
func NewHeaderConfig() *HeaderConfig {
return &HeaderConfig{
On: true,
Id: stringutil.Rand(16),
}
}
// 校验
func (this *HeaderConfig) Validate() error {
this.statusMap = map[int]bool{}
this.hasVariables = regexpNamedVariable.MatchString(this.Value)
if this.Status == nil {
this.Status = []int{}
}
for _, status := range this.Status {
this.statusMap[status] = true
}
return nil
}
// 判断是否匹配状态码
func (this *HeaderConfig) Match(statusCode int) bool {
if !this.On {
return false
}
if this.Always {
return true
}
if this.statusMap != nil {
_, found := this.statusMap[statusCode]
return found
}
return false
}
// 是否有变量
func (this *HeaderConfig) HasVariables() bool {
return this.hasVariables
}