-
Notifications
You must be signed in to change notification settings - Fork 2
/
http_header_policy.go
77 lines (64 loc) · 2.29 KB
/
http_header_policy.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
69
70
71
72
73
74
75
76
77
package shared
import "strings"
// HTTPHeaderPolicy HeaderList定义
type HTTPHeaderPolicy struct {
Id int64 `yaml:"id" json:"id"` // ID
Name string `yaml:"name" json:"name"` // 名称 TODO
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用 TODO
Description string `yaml:"description" json:"description"` // 描述 TODO
SetHeaderRefs []*HTTPHeaderRef `yaml:"setHeaderRefs" json:"setHeaderRefs"`
SetHeaders []*HTTPHeaderConfig `yaml:"setHeaders" json:"setHeaders"`
DeleteHeaders []string `yaml:"deleteHeaders" json:"deleteHeaders"` // 删除的Header
Expires *HTTPExpireHeaderConfig `yaml:"expires" json:"expires"` // 内容过期设置 TODO
CORS *HTTPCORSHeaderConfig `yaml:"cors" json:"cors"` // CORS跨域设置
NonStandardHeaders []string `yaml:"nonStandardHeaders" json:"nonStandardHeaders"` // 非标Header列表
setHeaderNames []string
deleteHeaderMap map[string]bool // header => bool
}
// Init 校验
func (this *HTTPHeaderPolicy) Init() error {
this.setHeaderNames = []string{}
for _, h := range this.SetHeaders {
err := h.Init()
if err != nil {
return err
}
this.setHeaderNames = append(this.setHeaderNames, strings.ToUpper(h.Name))
}
// delete
this.deleteHeaderMap = map[string]bool{}
for _, header := range this.DeleteHeaders {
this.deleteHeaderMap[strings.ToUpper(header)] = true
}
// cors
if this.CORS != nil {
err := this.CORS.Init()
if err != nil {
return err
}
}
return nil
}
// IsEmpty 判断是否为空
func (this *HTTPHeaderPolicy) IsEmpty() bool {
return len(this.SetHeaders) == 0 &&
this.Expires == nil &&
len(this.DeleteHeaders) == 0 &&
len(this.NonStandardHeaders) == 0 &&
(this.CORS == nil || !this.CORS.IsOn)
}
// ContainsHeader 判断Add和Set中是否包含某个Header
func (this *HTTPHeaderPolicy) ContainsHeader(name string) bool {
name = strings.ToUpper(name)
for _, n := range this.setHeaderNames {
if n == name {
return true
}
}
return false
}
// ContainsDeletedHeader 判断删除列表中是否包含某个Header
func (this *HTTPHeaderPolicy) ContainsDeletedHeader(name string) bool {
_, ok := this.deleteHeaderMap[strings.ToUpper(name)]
return ok
}