mirror of
https://github.com/hacdias/webdav.git
synced 2026-09-24 04:01:54 +08: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 {
|
func readConfig(flags *pflag.FlagSet) *webdav.Config {
|
||||||
cfg := &webdav.Config{
|
cfg := &webdav.Config{
|
||||||
User: &webdav.User{
|
User: &webdav.User{
|
||||||
@@ -130,6 +158,10 @@ func readConfig(flags *pflag.FlagSet) *webdav.Config {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
Auth: getOptB(flags, "auth"),
|
Auth: getOptB(flags, "auth"),
|
||||||
|
Cors: webdav.CorsCfg{
|
||||||
|
Enabled: false,
|
||||||
|
AllowedHosts: []string{},
|
||||||
|
},
|
||||||
Users: map[string]*webdav.User{},
|
Users: map[string]*webdav.User{},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,6 +175,11 @@ func readConfig(flags *pflag.FlagSet) *webdav.Config {
|
|||||||
parseUsers(users, cfg)
|
parseUsers(users, cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rawCors := v.Get("cors")
|
||||||
|
if cors, ok := rawCors.([]interface{}); ok {
|
||||||
|
parseCors(cors, cfg)
|
||||||
|
}
|
||||||
|
|
||||||
if len(cfg.Users) != 0 && !cfg.Auth {
|
if len(cfg.Users) != 0 && !cfg.Auth {
|
||||||
log.Print("Users will be ignored due to auth=false")
|
log.Print("Users will be ignored due to auth=false")
|
||||||
}
|
}
|
||||||
|
|||||||
Regular → Executable
Regular → Executable
+9
@@ -14,3 +14,12 @@ func checkPassword(saved, input string) bool {
|
|||||||
|
|
||||||
return saved == input
|
return saved == input
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isAllowedHost(allowedHosts []string, origin string) bool {
|
||||||
|
for _, host := range allowedHosts {
|
||||||
|
if host == origin {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
Regular → Executable
+28
@@ -7,15 +7,43 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Config is the configuration of a WebDAV instance.
|
// Config is the configuration of a WebDAV instance.
|
||||||
|
|
||||||
|
type CorsCfg struct {
|
||||||
|
Enabled bool
|
||||||
|
AllowedHosts []string
|
||||||
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
*User
|
*User
|
||||||
Auth bool
|
Auth bool
|
||||||
|
Cors CorsCfg
|
||||||
Users map[string]*User
|
Users map[string]*User
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
|
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
|
||||||
func (c *Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (c *Config) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
u := c.User
|
u := c.User
|
||||||
|
requestOrigin := r.Header.Get("Origin")
|
||||||
|
|
||||||
|
// add cors headers before any operation so even on 401 unauthorized cors will working only when Origin header is present so request came from browser
|
||||||
|
if c.Cors.Enabled && requestOrigin != "" {
|
||||||
|
|
||||||
|
headers := w.Header()
|
||||||
|
|
||||||
|
if(len(c.Cors.AllowedHosts) == 1 && c.Cors.AllowedHosts[0] == "*") {
|
||||||
|
headers.Set("Access-Control-Allow-Methods", "*")
|
||||||
|
headers.Set("Access-Control-Allow-Headers", "*")
|
||||||
|
headers.Set("Access-Control-Allow-Origin", "*")
|
||||||
|
} else if(isAllowedHost(c.Cors.AllowedHosts, requestOrigin)) {
|
||||||
|
headers.Set("Access-Control-Allow-Origin", requestOrigin)
|
||||||
|
headers.Set("Access-Control-Allow-Headers", "*")
|
||||||
|
headers.Set("Access-Control-Allow-Methods", "*")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Method == "OPTIONS" && c.Cors.Enabled && requestOrigin != "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if c.Auth {
|
if c.Auth {
|
||||||
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
||||||
|
|||||||
Reference in New Issue
Block a user