增加CORS自适应跨域

This commit is contained in:
GoEdgeLab
2022-12-29 17:16:11 +08:00
parent 51c6977123
commit bb6e1ce109
6 changed files with 346 additions and 162 deletions

View File

@@ -0,0 +1,19 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package shared
// HTTPCORSHeaderConfig 参考 https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
type HTTPCORSHeaderConfig struct {
IsOn bool `yaml:"isOn" json:"isOn"`
AllowMethods []string `yaml:"allowMethods" json:"allowMethods"` // TODO
AllowOrigin string `yaml:"allowOrigin" json:"allowOrigin"` // TODO
AllowCredentials bool `yaml:"allowCredentials" json:"allowCredentials"` // TODO
ExposeHeaders []string `yaml:"exposeHeaders" json:"exposeHeaders"` // TODO
MaxAge int32 `yaml:"maxAge" json:"maxAge"` // TODO
RequestHeaders []string `yaml:"requestHeaders" json:"requestHeaders"` // TODO
RequestMethod string `yaml:"requestMethod" json:"requestMethod"` // TODO
}
func (this *HTTPCORSHeaderConfig) Init() error {
return nil
}

View File

@@ -14,6 +14,7 @@ type HTTPHeaderPolicy struct {
DeleteHeaders []string `yaml:"deleteHeaders" json:"deleteHeaders"` // 删除的Header
Expires *HTTPExpireHeaderConfig `yaml:"expires" json:"expires"` // TODO
CORS *HTTPCORSHeaderConfig `yaml:"cors" json:"cors"`
setHeaderNames []string
deleteHeaderMap map[string]bool // header => bool
@@ -36,6 +37,14 @@ func (this *HTTPHeaderPolicy) Init() error {
this.deleteHeaderMap[strings.ToUpper(header)] = true
}
// cors
if this.CORS != nil {
err := this.CORS.Init()
if err != nil {
return err
}
}
return nil
}