mirror of
https://github.com/hacdias/webdav.git
synced 2024-04-21 12:32:06 +00:00
feat: add support for custom CORS headers
This commit is contained in:
@@ -118,6 +118,34 @@ func parseUsers(raw []interface{}, c *webdav.Config) {
|
||||
}
|
||||
}
|
||||
|
||||
func parseCors(raw []interface{}, c *webdav.Config) {
|
||||
hosts := []string{}
|
||||
|
||||
for _, v := range raw {
|
||||
|
||||
if cfg, ok := v.(map[interface{}]interface{}); ok {
|
||||
|
||||
cors := webdav.CorsCfg{
|
||||
Enabled: cfg["enabled"].(bool),
|
||||
AllowedHosts: []string{},
|
||||
}
|
||||
|
||||
if allowed_hosts, ok := cfg["allowed_hosts"]; ok {
|
||||
for _, host := range strings.Split(allowed_hosts.(string), ",") {
|
||||
hosts = append(hosts, host)
|
||||
}
|
||||
}
|
||||
|
||||
if len(hosts) == 0 {
|
||||
hosts = append(hosts, "*")
|
||||
}
|
||||
|
||||
cors.AllowedHosts = hosts
|
||||
c.Cors = cors
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readConfig(flags *pflag.FlagSet) *webdav.Config {
|
||||
cfg := &webdav.Config{
|
||||
User: &webdav.User{
|
||||
@@ -130,6 +158,10 @@ func readConfig(flags *pflag.FlagSet) *webdav.Config {
|
||||
},
|
||||
},
|
||||
Auth: getOptB(flags, "auth"),
|
||||
Cors: webdav.CorsCfg{
|
||||
Enabled: false,
|
||||
AllowedHosts: []string{},
|
||||
},
|
||||
Users: map[string]*webdav.User{},
|
||||
}
|
||||
|
||||
@@ -143,6 +175,11 @@ func readConfig(flags *pflag.FlagSet) *webdav.Config {
|
||||
parseUsers(users, cfg)
|
||||
}
|
||||
|
||||
rawCors := v.Get("cors")
|
||||
if cors, ok := rawCors.([]interface{}); ok {
|
||||
parseCors(cors, cfg)
|
||||
}
|
||||
|
||||
if len(cfg.Users) != 0 && !cfg.Auth {
|
||||
log.Print("Users will be ignored due to auth=false")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user