Browse Source

Merge pull request #253 from philips/add-Access-Control-Allow-Methods

fix(handlers): add Access-Control-Allow-Methods to cors
Ben Johnson 12 years ago
parent
commit
9ed2776639
1 changed files with 11 additions and 8 deletions
  1. 11 8
      etcd_handlers.go

+ 11 - 8
etcd_handlers.go

@@ -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["*"]
-	if val && ok {
-		w.Header().Add("Access-Control-Allow-Origin", "*")
-		return
+	addHeaders := func(origin string) bool {
+		val, ok := corsList[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
 	}
 	}
 
 
-	requestOrigin := r.Header.Get("Origin")
-	val, ok = corsList[requestOrigin]
-	if val && ok {
-		w.Header().Add("Access-Control-Allow-Origin", requestOrigin)
+	if addHeaders("*") == true {
 		return
 		return
 	}
 	}
+
+	addHeaders(r.Header.Get("Origin"))
 }
 }
 
 
 func (fn errorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 func (fn errorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {