From dd3c90c94d0245832acd07298060200604a7c1df Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Fri, 17 Feb 2017 12:34:43 +0300 Subject: [PATCH] Protect methonds and rmr fix --- rest/api.go | 34 ++++++++++++++++++++++++++++++---- rest/zoo.go | 16 +++------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/rest/api.go b/rest/api.go index b5316fa..b552032 100644 --- a/rest/api.go +++ b/rest/api.go @@ -28,6 +28,12 @@ type Get struct { // LS ... 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) path := vars["path"] @@ -82,7 +88,7 @@ func (zk ZooNode) UP(w http.ResponseWriter, r *http.Request) { } else if r.Method == "POST" { go func() { ch <- zk.UpdateNode(path, content) }() } else { - e := strings.Join([]string{r.Method, "not alowed"}, " ") + e := strings.Join([]string{"Method", r.Method, "not alowed"}, " ") w.WriteHeader(500) w.Write([]byte(e)) return @@ -97,8 +103,8 @@ func (zk ZooNode) UP(w http.ResponseWriter, r *http.Request) { // RM ... func (zk ZooNode) RM(w http.ResponseWriter, r *http.Request) { - if r.Method != "POST" { - e := strings.Join([]string{r.Method, "not alowed"}, " ") + if r.Method != "DELETE" { + e := strings.Join([]string{"Method", r.Method, "not alowed"}, " ") w.WriteHeader(500) w.Write([]byte(e)) return @@ -106,13 +112,33 @@ func (zk ZooNode) RM(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) 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.Write([]byte(rmPath)) } // GET ... 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) path := vars["path"] diff --git a/rest/zoo.go b/rest/zoo.go index de6a134..cf8429d 100644 --- a/rest/zoo.go +++ b/rest/zoo.go @@ -95,25 +95,15 @@ func (z ZooNode) GetNode(path string) Get { //RMR remove Zk node recursive func (z ZooNode) RMR(path string) { - var rmPath string - rmPath = strings.Join([]string{z.Path, path}, "") - if path == "/" { - return - } - - if strings.Contains(rmPath, "//") { - rmPath = strings.Replace(rmPath, "//", "/", 1) - } - - log.Print("rm: ", rmPath) - c, _, err := z.Conn.Children(rmPath) + log.Print("rm: ", path) + c, _, err := z.Conn.Children(path) if err != nil { log.Print("[zk ERROR] ", err) } log.Print("[WARNING] Trying delete ", path) if len(c) > 0 { for _, child := range c { - childPath := strings.Join([]string{rmPath, child}, "/") + childPath := strings.Join([]string{path, child}, "/") z.RMR(childPath) } }