mirror of
https://github.com/tuxbox-fork-migrations/recycled-ni-neutrino.git
synced 2025-08-29 16:31:05 +02:00
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
Origin commit data
------------------
Branch: ni/coolstream
Commit: e19eaf7289
Author: yjogol <yjogol2@online.de>
Date: 2011-09-08 (Thu, 08 Sep 2011)
------------------
This commit was generated by Migit
This commit is contained in:
@@ -8,6 +8,8 @@
|
|||||||
#include <cstdlib> // calloc and free prototypes.
|
#include <cstdlib> // calloc and free prototypes.
|
||||||
#include <cstring> // str* and memset prototypes.
|
#include <cstring> // str* and memset prototypes.
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
// yhttpd
|
// yhttpd
|
||||||
#include "yconfig.h"
|
#include "yconfig.h"
|
||||||
@@ -261,3 +263,50 @@ bool write_to_file(std::string filename, std::string content) {
|
|||||||
} else
|
} else
|
||||||
return false;
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -40,4 +40,13 @@ bool nocase_compare (char c1, char c2);
|
|||||||
std::string timeString(time_t time);
|
std::string timeString(time_t time);
|
||||||
bool write_to_file(std::string filename, std::string content);
|
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__ */
|
#endif /* __yhttpd_helper_h__ */
|
||||||
|
@@ -50,6 +50,7 @@ THandleStatus CyhookHandler::Hooks_SendResponse() {
|
|||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
THandleStatus CyhookHandler::Hooks_PrepareResponse() {
|
THandleStatus CyhookHandler::Hooks_PrepareResponse() {
|
||||||
log_level_printf(4, "PrepareResponse Hook-List Start\n");
|
log_level_printf(4, "PrepareResponse Hook-List Start\n");
|
||||||
|
outType = checkOutput();
|
||||||
THandleStatus _status = HANDLED_NONE;
|
THandleStatus _status = HANDLED_NONE;
|
||||||
THookList::iterator i = HookList.begin();
|
THookList::iterator i = HookList.begin();
|
||||||
for (; i != HookList.end(); i++) {
|
for (; i != HookList.end(); i++) {
|
||||||
@@ -363,3 +364,139 @@ void CyhookHandler::printf(const char *fmt, ...) {
|
|||||||
va_end(arglist);
|
va_end(arglist);
|
||||||
Write(outbuf);
|
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 + "</" + _key + ">";
|
||||||
|
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 + "</" + _key + ">";
|
||||||
|
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 + "</" + _key + ">";
|
||||||
|
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 + "</" + _key + ">";
|
||||||
|
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 = "<![CDATA[" + _content + "]]>";
|
||||||
|
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 "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -81,6 +81,17 @@ typedef enum
|
|||||||
} THandleStatus;
|
} THandleStatus;
|
||||||
typedef std::list<Cyhook *> THookList;
|
typedef std::list<Cyhook *> THookList;
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// Output Tyoe.
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
plain = 0,
|
||||||
|
html,
|
||||||
|
xml,
|
||||||
|
json
|
||||||
|
} TOutType;
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// An abstract Hook-Class. Custom Hook must be inherited.
|
// An abstract Hook-Class. Custom Hook must be inherited.
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
@@ -176,11 +187,23 @@ public:
|
|||||||
void WriteLn(char const *text) {WriteLn(std::string(text));}
|
void WriteLn(char const *text) {WriteLn(std::string(text));}
|
||||||
void SendHTMLHeader(const std::string& Titel);
|
void SendHTMLHeader(const std::string& Titel);
|
||||||
void SendHTMLFooter(void);
|
void SendHTMLFooter(void);
|
||||||
void SendOk(void) {(ParamList["response"]=="json") ? Write("{\"success\": true}") : Write("ok");}
|
void SendOk(void) {(ParamList["response"]=="json") ? Write("{\"success\": \"true\"}") : Write("ok");}
|
||||||
void SendError(void) {(ParamList["response"]=="json") ? Write("{\"success\": false}") : Write("error");}
|
void SendError(void) {(ParamList["response"]=="json") ? Write("{\"success\": \"false\"}") : Write("error");}
|
||||||
void SendFile(const std::string& url) {NewURL = url; status = HANDLED_SENDFILE;}
|
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 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;}
|
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;
|
friend class CyParser;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user