From dede2bc1eb47ade9fed36fdbb48f81545059abd7 Mon Sep 17 00:00:00 2001 From: vanhofen Date: Sat, 20 Feb 2016 23:06:46 +0100 Subject: [PATCH] yhook: send valide error/ok responses; allow error message Origin commit data ------------------ Commit: https://github.com/neutrino-images/ni-neutrino/commit/57cc01978b859b375e406fd3717a499388477b98 Author: vanhofen Date: 2016-02-20 (Sat, 20 Feb 2016) Origin message was: ------------------ - yhook: send valide error/ok responses; allow error message --- src/nhttpd/yhttpd_core/yhook.cpp | 41 ++++++++++++++++++++++++++++++++ src/nhttpd/yhttpd_core/yhook.h | 4 ++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/nhttpd/yhttpd_core/yhook.cpp b/src/nhttpd/yhttpd_core/yhook.cpp index c159416d5..c9fd94a08 100644 --- a/src/nhttpd/yhttpd_core/yhook.cpp +++ b/src/nhttpd/yhttpd_core/yhook.cpp @@ -538,3 +538,44 @@ std::string CyhookHandler::outNext() { return ""; } +//----------------------------------------------------------------------------- +void CyhookHandler::SendOk() { + std::string result = ""; + switch (outType) { + case xml: + result = "true"; + break; + case json: + result = "{\"success\": \"true\"}"; + break; + default: + result = "ok"; + break; + } + Write(result); +} +//----------------------------------------------------------------------------- +void CyhookHandler::SendError(std::string error) { + std::string result = ""; + switch (outType) { + case xml: + if (error.empty()) + result = "false"; + else + result = "false" + error + ""; + break; + case json: + if (error.empty()) + result = "{\"success\": \"false\"}"; + else + result = "{\"success\": \"false\", \"error\":{\"text\": \"" + error + "\"}}"; + break; + default: + if (error.empty()) + result = "error"; + else + result = "error=" + error; + break; + } + Write(result); +} diff --git a/src/nhttpd/yhttpd_core/yhook.h b/src/nhttpd/yhttpd_core/yhook.h index 781d8dc9f..03147a8da 100644 --- a/src/nhttpd/yhttpd_core/yhook.h +++ b/src/nhttpd/yhttpd_core/yhook.h @@ -192,8 +192,8 @@ public: void WriteLn(char const *text) {WriteLn(std::string(text));} void SendHTMLHeader(const std::string& Titel); void SendHTMLFooter(void); - void SendOk(void) {(ParamList["response"]=="json") ? Write("{\"success\": \"true\"}") : Write("ok");} - void SendError(void) {(ParamList["response"]=="json") ? Write("{\"success\": \"false\"}") : Write("error");} + void SendOk(void); + void SendError(std::string error = ""); void SendFile(const std::string& url) {NewURL = url; status = HANDLED_SENDFILE;} void SendRedirect(const std::string& url) {httpStatus=HTTP_MOVED_TEMPORARILY; NewURL = url; status = HANDLED_REDIRECTION;} void SendRewrite(const std::string& url) {NewURL = url; status = HANDLED_REWRITE;}