From e19eaf7289fdb47dc7b29a01048f894e878577b7 Mon Sep 17 00:00:00 2001 From: yjogol Date: Thu, 8 Sep 2011 11:58:35 +0000 Subject: [PATCH] nhttpd add helper for json,xml outputs git-svn-id: file:///home/bas/coolstream_public_svn/THIRDPARTY/applications/neutrino-beta@1674 e54a6e83-5905-42d5-8d5c-058d10e6a962 --- src/nhttpd/yhttpd_core/helper.cpp | 49 +++++++++++ src/nhttpd/yhttpd_core/helper.h | 9 ++ src/nhttpd/yhttpd_core/yhook.cpp | 137 ++++++++++++++++++++++++++++++ src/nhttpd/yhttpd_core/yhook.h | 27 +++++- 4 files changed, 220 insertions(+), 2 deletions(-) diff --git a/src/nhttpd/yhttpd_core/helper.cpp b/src/nhttpd/yhttpd_core/helper.cpp index f7fa7deb4..454756e27 100644 --- a/src/nhttpd/yhttpd_core/helper.cpp +++ b/src/nhttpd/yhttpd_core/helper.cpp @@ -8,6 +8,8 @@ #include // calloc and free prototypes. #include // str* and memset prototypes. #include +#include +#include // yhttpd #include "yconfig.h" @@ -261,3 +263,50 @@ bool write_to_file(std::string filename, std::string content) { } else return false; } + +//----------------------------------------------------------------------------- +// JSON: create pair string "<_key>". "<_value>" +// Handle wrong quotes +//----------------------------------------------------------------------------- +std::string json_out_quote_convert(std::string _str) { + replace(_str, "\"", "\'"); + return _str; +} +//----------------------------------------------------------------------------- +// JSON: create pair string "<_key>". "<_value>" +// Handle wrong quotes +//----------------------------------------------------------------------------- +std::string json_out_pair(std::string _key, std::string _value) { + replace(_key, "\"", ""); + replace(_value, "\"", "\'"); + return "\"" + _key + "\": " + "\"" + _value + "\""; +} +//----------------------------------------------------------------------------- +// JSON: create success return string +//----------------------------------------------------------------------------- +std::string json_out_success(std::string _result) { + return "{\"success\": \"true\", \"data\":{" + _result + "}}"; +} +//----------------------------------------------------------------------------- +// JSON: create success return string +//----------------------------------------------------------------------------- +std::string json_out_error(std::string _error) { + return "{\"success\": \"false\", \"error\":{\"text\": \"" + _error + "\"}}"; +} +//----------------------------------------------------------------------------- +// JSON: convert string to JSON-String +//----------------------------------------------------------------------------- +std::string json_convert_string(std::string s) { + std::stringstream ss; + for (size_t i = 0; i < s.length(); ++i) { + if (unsigned(s[i]) < '\x20' || s[i] == '\\' || s[i] == '"') { + ss << "\\u" << std::setfill('0') << std::setw(4) << std::hex + << unsigned(s[i]); + } + else { + ss << s[i]; + } + } + return ss.str(); +} + diff --git a/src/nhttpd/yhttpd_core/helper.h b/src/nhttpd/yhttpd_core/helper.h index 30f44a210..a2b87c850 100644 --- a/src/nhttpd/yhttpd_core/helper.h +++ b/src/nhttpd/yhttpd_core/helper.h @@ -40,4 +40,13 @@ bool nocase_compare (char c1, char c2); std::string timeString(time_t time); bool write_to_file(std::string filename, std::string content); +//----------------------------------------------------------------------------- +// JSON Helpers +//----------------------------------------------------------------------------- +std::string json_out_quote_convert(std::string _str); +std::string json_out_pair(std::string _key, std::string _value); +std::string json_out_success(std::string _result); +std::string json_out_error(std::string _error); +std::string json_convert_string(std::string s); + #endif /* __yhttpd_helper_h__ */ diff --git a/src/nhttpd/yhttpd_core/yhook.cpp b/src/nhttpd/yhttpd_core/yhook.cpp index 2913eaf04..43aad87d1 100644 --- a/src/nhttpd/yhttpd_core/yhook.cpp +++ b/src/nhttpd/yhttpd_core/yhook.cpp @@ -50,6 +50,7 @@ THandleStatus CyhookHandler::Hooks_SendResponse() { //----------------------------------------------------------------------------- THandleStatus CyhookHandler::Hooks_PrepareResponse() { log_level_printf(4, "PrepareResponse Hook-List Start\n"); + outType = checkOutput(); THandleStatus _status = HANDLED_NONE; THookList::iterator i = HookList.begin(); for (; i != HookList.end(); i++) { @@ -363,3 +364,139 @@ void CyhookHandler::printf(const char *fmt, ...) { va_end(arglist); Write(outbuf); } +//----------------------------------------------------------------------------- +TOutType CyhookHandler::checkOutput() { + // get outType + outType = plain; // plain + if (ParamList["format"] == "json" || !(ParamList["json"].empty()) ) + outType = json; + else if (ParamList["format"] == "xml" || !(ParamList["xml"].empty()) ) + outType = xml; + return outType; +} +//----------------------------------------------------------------------------- +TOutType CyhookHandler::outStart() { + // get outType + outType = plain; // plain + if (ParamList["format"] == "json") + outType = json; + else if (ParamList["format"] == "xml" || !(ParamList["xml"].empty()) ) + outType = xml; + // set response header + if (outType == xml) + SetHeader(HTTP_OK, "text/xml; charset=UTF-8"); + else + SetHeader(HTTP_OK, "text/plain; charset=UTF-8"); + + return outType; +} + +//----------------------------------------------------------------------------- +std::string CyhookHandler::outIndent() { + return ""; +} +//----------------------------------------------------------------------------- +std::string CyhookHandler::outPair(std::string _key, std::string _content, bool _next) { + std::string result = ""; + switch (outType) { + case xml: + result = outIndent() + "<" + _key + ">" + _content + ""; + result += "\n"; + break; + case json: + result = outIndent() + "\"" + _key + "\": \"" + _content + "\""; + if(_next) + result += ","; + result += "\n"; + break; + default: + result = _content; + break; + } + return result; +} + +//----------------------------------------------------------------------------- +std::string CyhookHandler::outArray(std::string _key, std::string _content) { + std::string result = ""; + switch (outType) { + case xml: + //TODO: xml check and DESC check + result = outIndent() + "<" + _key + ">\n" + _content + ""; + break; + case json: + //TODO: json check + result = outIndent() + "\"" + _key + "\": [" + _content + "]"; + break; + default: + result = _content; + break; + } + return result + "\n"; +} + +//----------------------------------------------------------------------------- +std::string CyhookHandler::outArrayItem(std::string _key, std::string _content, bool _next) { + std::string result = ""; + switch (outType) { + case xml: + //TODO: xml check and DESC check + result = outIndent() + "<" + _key + ">\n" + _content + ""; + break; + case json: + //TODO: json check + result = outIndent() + "{" + _content + "}"; + if(_next) + result += ","; + break; + default: + result = _content; + break; + } + return result + "\n"; +} +//----------------------------------------------------------------------------- +std::string CyhookHandler::outCollection(std::string _key, std::string _content) { + std::string result = ""; + switch (outType) { + case xml: + //TODO: xml check and DESC check + result = outIndent() + "<" + _key + ">\n" + _content + ""; + break; + case json: + //TODO: json check + result = outIndent() + "\"" + _key + "\": {" + _content + "}"; + break; + default: + result = _content; + break; + } + return result + "\n"; +} + +//----------------------------------------------------------------------------- +std::string CyhookHandler::outValue(std::string _content) { + std::string result = ""; + switch (outType) { + case xml: + result = ""; + break; + case json: +// result = json_convert_string(_content); + result = _content; + break; + + default: + result = _content; + break; + } + return result; +} + +std::string CyhookHandler::outNext() { + if(outType == json) + return ","; + else + return ""; +} + diff --git a/src/nhttpd/yhttpd_core/yhook.h b/src/nhttpd/yhttpd_core/yhook.h index eb55dec20..3c0306811 100644 --- a/src/nhttpd/yhttpd_core/yhook.h +++ b/src/nhttpd/yhttpd_core/yhook.h @@ -81,6 +81,17 @@ typedef enum } THandleStatus; typedef std::list THookList; +//----------------------------------------------------------------------------- +// Output Tyoe. +//----------------------------------------------------------------------------- +typedef enum +{ + plain = 0, + html, + xml, + json +} TOutType; + //----------------------------------------------------------------------------- // An abstract Hook-Class. Custom Hook must be inherited. //----------------------------------------------------------------------------- @@ -176,11 +187,23 @@ 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) {(ParamList["response"]=="json") ? Write("{\"success\": \"true\"}") : Write("ok");} + void SendError(void) {(ParamList["response"]=="json") ? Write("{\"success\": \"false\"}") : Write("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;} + + int _outIndent; + TOutType outType; // Outputtpe = plain (default)|xml|json + TOutType outStart(); + TOutType checkOutput(); + std::string outIndent(); + std::string outPair(std::string _key, std::string _content, bool _next); + std::string outArray(std::string _key, std::string _content); + std::string outArrayItem(std::string _key, std::string _content, bool _next); + std::string outCollection(std::string _key,std::string _content); + std::string outValue(std::string _content); + std::string outNext(); friend class CyParser; };