2020-09-26 08:07:24 +08:00
|
|
|
package shared
|
|
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
2021-06-15 10:33:30 +08:00
|
|
|
// HTTPHeaderPolicy HeaderList定义
|
2020-09-26 08:07:24 +08:00
|
|
|
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
|
|
|
|
|
|
2021-12-14 21:26:43 +08:00
|
|
|
SetHeaderRefs []*HTTPHeaderRef `yaml:"setHeaderRefs" json:"setHeaderRefs"`
|
|
|
|
|
SetHeaders []*HTTPHeaderConfig `yaml:"setHeaders" json:"setHeaders"`
|
|
|
|
|
DeleteHeaders []string `yaml:"deleteHeaders" json:"deleteHeaders"` // 删除的Header
|
2020-09-26 08:07:24 +08:00
|
|
|
|
2023-05-19 19:52:22 +08:00
|
|
|
Expires *HTTPExpireHeaderConfig `yaml:"expires" json:"expires"` // 内容过期设置 TODO
|
|
|
|
|
CORS *HTTPCORSHeaderConfig `yaml:"cors" json:"cors"` // CORS跨域设置
|
|
|
|
|
NonStandardHeaders []string `yaml:"nonStandardHeaders" json:"nonStandardHeaders"` // 非标Header列表
|
2020-09-26 08:07:24 +08:00
|
|
|
|
2020-09-26 11:21:38 +08:00
|
|
|
setHeaderNames []string
|
2020-09-26 08:07:24 +08:00
|
|
|
deleteHeaderMap map[string]bool // header => bool
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 10:33:30 +08:00
|
|
|
// Init 校验
|
2020-09-26 08:07:24 +08:00
|
|
|
func (this *HTTPHeaderPolicy) Init() error {
|
2020-09-26 11:21:38 +08:00
|
|
|
this.setHeaderNames = []string{}
|
2020-09-26 08:07:24 +08:00
|
|
|
for _, h := range this.SetHeaders {
|
|
|
|
|
err := h.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-09-26 11:21:38 +08:00
|
|
|
this.setHeaderNames = append(this.setHeaderNames, strings.ToUpper(h.Name))
|
2020-09-26 08:07:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// delete
|
|
|
|
|
this.deleteHeaderMap = map[string]bool{}
|
|
|
|
|
for _, header := range this.DeleteHeaders {
|
|
|
|
|
this.deleteHeaderMap[strings.ToUpper(header)] = true
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 17:16:11 +08:00
|
|
|
// cors
|
|
|
|
|
if this.CORS != nil {
|
|
|
|
|
err := this.CORS.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-26 08:07:24 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 10:33:30 +08:00
|
|
|
// IsEmpty 判断是否为空
|
2020-09-26 08:07:24 +08:00
|
|
|
func (this *HTTPHeaderPolicy) IsEmpty() bool {
|
2023-07-22 15:00:15 +08:00
|
|
|
return len(this.SetHeaders) == 0 &&
|
|
|
|
|
this.Expires == nil &&
|
|
|
|
|
len(this.DeleteHeaders) == 0 &&
|
|
|
|
|
len(this.NonStandardHeaders) == 0 &&
|
|
|
|
|
(this.CORS == nil || !this.CORS.IsOn)
|
2020-09-26 08:07:24 +08:00
|
|
|
}
|
|
|
|
|
|
2021-06-15 10:33:30 +08:00
|
|
|
// ContainsHeader 判断Add和Set中是否包含某个Header
|
2020-09-26 11:21:38 +08:00
|
|
|
func (this *HTTPHeaderPolicy) ContainsHeader(name string) bool {
|
|
|
|
|
name = strings.ToUpper(name)
|
|
|
|
|
|
|
|
|
|
for _, n := range this.setHeaderNames {
|
|
|
|
|
if n == name {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 10:33:30 +08:00
|
|
|
// ContainsDeletedHeader 判断删除列表中是否包含某个Header
|
2020-09-26 11:21:38 +08:00
|
|
|
func (this *HTTPHeaderPolicy) ContainsDeletedHeader(name string) bool {
|
|
|
|
|
_, ok := this.deleteHeaderMap[strings.ToUpper(name)]
|
2020-09-26 08:07:24 +08:00
|
|
|
return ok
|
|
|
|
|
}
|