Only set CORS headers if they aren't set already

This allow for middleware to set these to more specific values if desired
This commit is contained in:
Thomas Jackson 2019-04-25 15:12:36 -07:00 committed by Rishabh Kumar
parent f25b8c240d
commit 106542a1fe

View file

@ -34,15 +34,19 @@ func SetCORS(w http.ResponseWriter, o *regexp.Regexp, r *http.Request) {
} }
for k, v := range corsHeaders { for k, v := range corsHeaders {
w.Header().Set(k, v) if _, ok := w.Header()[k]; !ok {
w.Header().Set(k, v)
}
} }
if o.String() == "^(?:.*)$" { if _, ok := w.Header()["Access-Control-Allow-Origin"]; !ok {
w.Header().Set("Access-Control-Allow-Origin", "*") if o.String() == "^(?:.*)$" {
return w.Header().Set("Access-Control-Allow-Origin", "*")
} return
}
if o.MatchString(origin) { if o.MatchString(origin) {
w.Header().Set("Access-Control-Allow-Origin", origin) w.Header().Set("Access-Control-Allow-Origin", origin)
}
} }
} }