mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
fix(handlers): add Access-Control-Allow-Methods to cors
This is needed to have CORs work properly. Fixes #252
This commit is contained in:
parent
d5f0642524
commit
55f9045561
@ -53,18 +53,21 @@ type errorHandler func(http.ResponseWriter, *http.Request) error
|
|||||||
// provided allowed origins and sets the Access-Control-Allow-Origin header if
|
// provided allowed origins and sets the Access-Control-Allow-Origin header if
|
||||||
// there is a match.
|
// there is a match.
|
||||||
func addCorsHeader(w http.ResponseWriter, r *http.Request) {
|
func addCorsHeader(w http.ResponseWriter, r *http.Request) {
|
||||||
val, ok := corsList["*"]
|
addHeaders := func(origin string) bool {
|
||||||
if val && ok {
|
val, ok := corsList[origin]
|
||||||
w.Header().Add("Access-Control-Allow-Origin", "*")
|
if val == false || ok == false {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
w.Header().Add("Access-Control-Allow-Origin", origin)
|
||||||
|
w.Header().Add("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if addHeaders("*") == true {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
requestOrigin := r.Header.Get("Origin")
|
addHeaders(r.Header.Get("Origin"))
|
||||||
val, ok = corsList[requestOrigin]
|
|
||||||
if val && ok {
|
|
||||||
w.Header().Add("Access-Control-Allow-Origin", requestOrigin)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fn errorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (fn errorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user