Protect methonds and rmr fix

This commit is contained in:
Denis Zheleztsov 2017-02-17 12:34:43 +03:00
parent f1618cde11
commit dd3c90c94d
2 changed files with 33 additions and 17 deletions

View File

@ -28,6 +28,12 @@ type Get struct {
// LS ... // LS ...
func (zk ZooNode) LS(w http.ResponseWriter, r *http.Request) { func (zk ZooNode) LS(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
e := strings.Join([]string{"Method", r.Method, "not alowed"}, " ")
w.WriteHeader(500)
w.Write([]byte(e))
return
}
vars := mux.Vars(r) vars := mux.Vars(r)
path := vars["path"] path := vars["path"]
@ -82,7 +88,7 @@ func (zk ZooNode) UP(w http.ResponseWriter, r *http.Request) {
} else if r.Method == "POST" { } else if r.Method == "POST" {
go func() { ch <- zk.UpdateNode(path, content) }() go func() { ch <- zk.UpdateNode(path, content) }()
} else { } else {
e := strings.Join([]string{r.Method, "not alowed"}, " ") e := strings.Join([]string{"Method", r.Method, "not alowed"}, " ")
w.WriteHeader(500) w.WriteHeader(500)
w.Write([]byte(e)) w.Write([]byte(e))
return return
@ -97,8 +103,8 @@ func (zk ZooNode) UP(w http.ResponseWriter, r *http.Request) {
// RM ... // RM ...
func (zk ZooNode) RM(w http.ResponseWriter, r *http.Request) { func (zk ZooNode) RM(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" { if r.Method != "DELETE" {
e := strings.Join([]string{r.Method, "not alowed"}, " ") e := strings.Join([]string{"Method", r.Method, "not alowed"}, " ")
w.WriteHeader(500) w.WriteHeader(500)
w.Write([]byte(e)) w.Write([]byte(e))
return return
@ -106,13 +112,33 @@ func (zk ZooNode) RM(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
path := vars["path"] path := vars["path"]
go func() { zk.RMR(path) }() var rmPath string
rmPath = strings.Join([]string{zk.Path, path}, "")
if path == "/" {
e := "Skiping root path"
w.WriteHeader(500)
w.Write([]byte(e))
return
}
if strings.Contains(rmPath, "//") {
rmPath = strings.Replace(rmPath, "//", "/", 1)
}
go func() { zk.RMR(rmPath) }()
w.WriteHeader(200) w.WriteHeader(200)
w.Write([]byte(rmPath))
} }
// GET ... // GET ...
func (zk ZooNode) GET(w http.ResponseWriter, r *http.Request) { func (zk ZooNode) GET(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
e := strings.Join([]string{"Method", r.Method, "not alowed"}, " ")
w.WriteHeader(500)
w.Write([]byte(e))
return
}
vars := mux.Vars(r) vars := mux.Vars(r)
path := vars["path"] path := vars["path"]

View File

@ -95,25 +95,15 @@ func (z ZooNode) GetNode(path string) Get {
//RMR remove Zk node recursive //RMR remove Zk node recursive
func (z ZooNode) RMR(path string) { func (z ZooNode) RMR(path string) {
var rmPath string log.Print("rm: ", path)
rmPath = strings.Join([]string{z.Path, path}, "") c, _, err := z.Conn.Children(path)
if path == "/" {
return
}
if strings.Contains(rmPath, "//") {
rmPath = strings.Replace(rmPath, "//", "/", 1)
}
log.Print("rm: ", rmPath)
c, _, err := z.Conn.Children(rmPath)
if err != nil { if err != nil {
log.Print("[zk ERROR] ", err) log.Print("[zk ERROR] ", err)
} }
log.Print("[WARNING] Trying delete ", path) log.Print("[WARNING] Trying delete ", path)
if len(c) > 0 { if len(c) > 0 {
for _, child := range c { for _, child := range c {
childPath := strings.Join([]string{rmPath, child}, "/") childPath := strings.Join([]string{path, child}, "/")
z.RMR(childPath) z.RMR(childPath)
} }
} }