Browse Source

check keywords before set/testandset

Xiang Li 12 years ago
parent
commit
612fcf120e
4 changed files with 50 additions and 5 deletions
  1. 10 4
      client_handlers.go
  2. 3 0
      error.go
  3. 37 0
      store/keyword_test.go
  4. 0 1
      store/keywords.go

+ 10 - 4
client_handlers.go

@@ -35,6 +35,14 @@ func Multiplexer(w http.ResponseWriter, req *http.Request) {
 func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
 	key := req.URL.Path[len("/v1/keys/"):]
 
+	if store.CheckKeyword(key) {
+
+		(*w).WriteHeader(http.StatusBadRequest)
+
+		(*w).Write(newJsonError(400, "Set"))
+		return
+	}
+
 	debug("[recv] POST http://%v/v1/keys/%s", raftServer.Name(), key)
 
 	value := req.FormValue("value")
@@ -57,6 +65,7 @@ func SetHttpHandler(w *http.ResponseWriter, req *http.Request) {
 		(*w).WriteHeader(http.StatusBadRequest)
 
 		(*w).Write(newJsonError(202, "Set"))
+		return
 	}
 
 	if len(prevValue) != 0 {
@@ -227,7 +236,7 @@ func GetHttpHandler(w *http.ResponseWriter, req *http.Request) {
 
 		(*w).WriteHeader(http.StatusInternalServerError)
 		(*w).Write(newJsonError(300, ""))
-		return
+
 	} else {
 		body, ok := body.([]byte)
 		if !ok {
@@ -237,7 +246,6 @@ func GetHttpHandler(w *http.ResponseWriter, req *http.Request) {
 		(*w).WriteHeader(http.StatusOK)
 		(*w).Write(body)
 
-		return
 	}
 
 }
@@ -274,7 +282,6 @@ func WatchHttpHandler(w http.ResponseWriter, req *http.Request) {
 	if body, err := command.Apply(raftServer); err != nil {
 		warn("Unable to do watch command: %v", err)
 		w.WriteHeader(http.StatusInternalServerError)
-		return
 	} else {
 		w.WriteHeader(http.StatusOK)
 
@@ -284,7 +291,6 @@ func WatchHttpHandler(w http.ResponseWriter, req *http.Request) {
 		}
 
 		w.Write(body)
-		return
 	}
 
 }

+ 3 - 0
error.go

@@ -21,6 +21,9 @@ func init() {
 	// raft related errors
 	errors[300] = "Raft Internal Error"
 	errors[301] = "During Leader Election"
+
+	// keyword
+	errors[400] = "The prefix of the given key is a keyword in etcd"
 }
 
 type jsonError struct {

+ 37 - 0
store/keyword_test.go

@@ -0,0 +1,37 @@
+package store
+
+import (
+	"testing"
+)
+
+func TestKeywords(t *testing.T) {
+	keyword := CheckKeyword("machines")
+	if !keyword {
+		t.Fatal("machines should be keyword")
+	}
+
+	keyword = CheckKeyword("/machines")
+
+	if !keyword {
+		t.Fatal("/machines should be keyword")
+	}
+
+	keyword = CheckKeyword("/machines/")
+
+	if !keyword {
+		t.Fatal("/machines/ contains keyword prefix")
+	}
+
+	keyword = CheckKeyword("/machines/node1")
+
+	if !keyword {
+		t.Fatal("/machines/* contains keyword prefix")
+	}
+
+	keyword = CheckKeyword("/nokeyword/machines/node1")
+
+	if keyword {
+		t.Fatal("this does not contain keyword prefix")
+	}
+
+}

+ 0 - 1
store/keywords.go

@@ -1,7 +1,6 @@
 package store
 
 import (
-	"fmt"
 	"path"
 	"strings"
 )