check keywords before set/testandset

This commit is contained in:
Xiang Li 2013-07-31 10:10:00 -07:00
parent 756b2cc750
commit 612fcf120e
4 changed files with 50 additions and 5 deletions

View File

@ -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
}
}

View File

@ -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
store/keyword_test.go Normal file
View File

@ -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")
}
}

View File

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