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,9 +34,12 @@ func SetCORS(w http.ResponseWriter, o *regexp.Regexp, r *http.Request) {
} }
for k, v := range corsHeaders { for k, v := range corsHeaders {
if _, ok := w.Header()[k]; !ok {
w.Header().Set(k, v) w.Header().Set(k, v)
} }
}
if _, ok := w.Header()["Access-Control-Allow-Origin"]; !ok {
if o.String() == "^(?:.*)$" { if o.String() == "^(?:.*)$" {
w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Origin", "*")
return return
@ -46,3 +49,4 @@ func SetCORS(w http.ResponseWriter, o *regexp.Regexp, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", origin) w.Header().Set("Access-Control-Allow-Origin", origin)
} }
} }
}