From 8dab1ce8a99db8710d2f0e5fd37a5bd14f7c00a8 Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Tue, 6 Nov 2018 08:23:15 +0300 Subject: [PATCH] Parse POST --- main.go | 2 ++ node/api.go | 33 +++++++++++++++++++++++---------- node/auth.go | 6 +++--- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 2613cb8..89997a7 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,8 @@ func init() { flag.StringVar(&add, "add", "", "User to add") flag.StringVar(&email, "email", "", "User email address") flag.Parse() + + log.SetLevel(log.DebugLevel) } // main ... diff --git a/node/api.go b/node/api.go index e3024db..5e2fcde 100644 --- a/node/api.go +++ b/node/api.go @@ -2,12 +2,11 @@ package node import ( "fmt" + "io/ioutil" "net/http" "strings" "time" - "encoding/json" - log "github.com/Sirupsen/logrus" "github.com/gorilla/mux" ) @@ -143,26 +142,40 @@ func (es ESConf) UPointHandler(w http.ResponseWriter, r *http.Request) { var req PointRequest LogRequest(r) - err := json.NewDecoder(r.Body).Decode(&req) - if err != nil { - w.WriteHeader(400) - w.Write([]byte(fmt.Sprintf("error: %s", err.Error()))) + // Log request + content, _ := ioutil.ReadAll(r.Body) + log.Debugf("Point request is: ", string(content)) + + // Get plain POST variables + if err := r.ParseForm(); err != nil { + log.Error("Fail to parse POST args: ", err.Error()) + } + pauth := r.Form.Get("pauth") + tmsg := r.Form.Get("tmsg") + + req.Pauth = pauth + req.Tmsg = tmsg + + log.Debugf("pauth: %s\ntmsg: %s", pauth, tmsg) + + if pauth == "" { + w.WriteHeader(403) + w.Write([]byte("error: authstring cannot be empty")) return } - // Authorization check user, ok := es.checkAuth(req) if !ok { w.WriteHeader(403) - w.Write([]byte("Permission denied")) + w.Write([]byte("error: permission denied")) return } // Proccess point message - err = es.PointMessage(req, user) - if err != nil { + if err := es.PointMessage(req, user); err != nil { log.Error(err.Error()) w.WriteHeader(500) + w.Write([]byte(fmt.Sprintf("error: %s", err.Error()))) return } diff --git a/node/auth.go b/node/auth.go index ee04fab..910f040 100644 --- a/node/auth.go +++ b/node/auth.go @@ -54,7 +54,7 @@ func (es ESConf) checkAuth(r PointRequest) (User, bool) { req, err := http.NewRequest("POST", reqURL, strings.NewReader(query)) if err != nil { - log.Error(err) + log.Error("Can't prepare a request: ", err) return User{}, false } req.Header.Add("Content-Type", "application/json") @@ -62,7 +62,7 @@ func (es ESConf) checkAuth(r PointRequest) (User, bool) { client := &http.Client{} resp, err := client.Do(req) if err != nil { - log.Error(err) + log.Error("Can't make a request: ", err) return User{}, false } defer resp.Body.Close() @@ -70,7 +70,7 @@ func (es ESConf) checkAuth(r PointRequest) (User, bool) { var esr MaxIdAggregation err = json.NewDecoder(resp.Body).Decode(&esr) if err != nil { - log.Error(err) + log.Error("Can't decode a response from ES: ", err) return User{}, false }