Skip to content

Commit

Permalink
add kgl::serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
keengo99 committed Dec 7, 2024
1 parent fc593f3 commit 8c74dbf
Show file tree
Hide file tree
Showing 11 changed files with 486 additions and 52 deletions.
5 changes: 4 additions & 1 deletion include/KBaseVirtualHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
#include "KConfigTree.h"
#include "KPathHandler.h"
#include "KSharedObj.h"
#include "serializable.h"
#ifdef ENABLE_BLACK_LIST
#include "KIpList.h"
#endif
void on_vh_event(void* data, kconfig::KConfigTree* tree, kconfig::KConfigEvent* ev);
class KApiPipeStream;
class KVirtualHost;
class KHttpFilterManage;
#if 0
class KDataAttribute
{
public:
Expand All @@ -32,12 +34,13 @@ class KDataAttribute
virtual KDataAttribute* add(const KString& name) = 0;
virtual void build(KWStream& s, int format) = 0;
};
#endif
class KVirtualHostEvent
{
public:
virtual ~KVirtualHostEvent() {
};
virtual KDataAttribute* data() = 0;
virtual kgl::serializable* data() = 0;
virtual void setStatus(const char* errMsg) = 0;
virtual void redirect(const char* event) = 0;
virtual void buildVh(KVirtualHost* vh) = 0;
Expand Down
124 changes: 124 additions & 0 deletions include/serializable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#ifndef SERIALIZABLE_H
#define SERIALIZABLE_H
#include "KStream.h"
#include "KStringBuf.h"
#include "KXmlDocument.h"
#include <vector>
#include <map>
namespace kgl {
using string = KString;
using wstream = KWStream;
enum class data_type {
STR,
OBJ,
INT,
STR_ARRAY,
OBJ_ARRAY,
INT_ARRAY
};
enum class format {
xml_attribute,
xml_text,
json
};
class data_value;
class serializable {
public:
~serializable();
serializable() {

}
serializable(serializable&& a) noexcept {
data.swap(a.data);
}
serializable(const serializable& a) = delete;

bool add(const string& name, const string& value);
bool add(const string& name, int64_t v);
serializable* add(const string& name);

std::vector<string>* add_string_array(const string& name);
std::vector<int64_t>* add_int_array(const string& name);
serializable* add_obj_array(const string& name);
serializable& operator = (serializable&& a) noexcept;
serializable& operator = (const serializable& a);

bool get(const string& name, string& value) const;
const string& get(const string& name) const;
const string* getx(const string& name) const;
const int64_t get_int(const string& name) const;
void build(wstream& s, format fmt);
private:
std::map<string, data_value*> data;
};

class data_value {
public:
data_value(const string& value) {
type = data_type::STR;
this->str = new string(value);
}
data_value(int64_t v) :i{ v } {
type = data_type::INT;
}
data_value(data_type type);
data_value* add_ref() {
katom_inc((void*)&refs);
return this;
}
void release() {
if (katom_dec((void*)&refs) == 0) {
delete this;
}
}
data_type change_to_array() {
switch (type) {
case data_type::STR:
{
auto v = str;
strs = new std::vector<string>{ *v };
delete v;
type = data_type::STR_ARRAY;
break;
}
case data_type::INT:
{
auto v = i;
ints = new std::vector<int64_t>{ v };
type = data_type::INT_ARRAY;
break;
}
case data_type::OBJ:
{
auto v = obj;
objs = new std::vector< serializable>();
objs->emplace_back(std::move(*v));
delete v;
type = data_type::OBJ_ARRAY;
break;
}
default:
break;
}
return type;
}
void build(const string& name, wstream& s, format fmt);
union {
std::vector<string>* strs; //STR_ARRAY
std::vector<serializable>* objs; //OBJ_ARRAY
std::vector<int64_t>* ints; //INT_ARRAY
string* str; //STR
serializable* obj; //OBJ
int64_t i; //INT
};
data_type get_type() const {
return type;
}
private:
data_type type;
volatile int32_t refs = 1;
~data_value();
};

}
#endif
20 changes: 12 additions & 8 deletions module/whm/KWhmService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,36 @@ bool KWhmService::service(KServiceProvider *provider) {
provider->sendStatus(STATUS_OK, "OK");
const char *callName = uv->getx("whm_call");
const char* v = uv->getx("format");
int format = OUTPUT_FORMAT_XML;
if (v && strcasecmp(v, "json") == 0) {
format = OUTPUT_FORMAT_JSON;
kgl::format fmt = kgl::format::xml_text;
if (v) {
if (strcasecmp(v, "json") == 0) {
fmt = kgl::format::json;
} else if (strcasecmp(v, "xml-attribute") == 0) {
fmt = kgl::format::xml_attribute;
}
}
KWStream *out = provider->getOutputStream();
if (format == OUTPUT_FORMAT_XML) {
if (fmt!= kgl::format::json) {
provider->sendUnknowHeader("Content-Type", "text/xml");
*out << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
} else {
provider->sendUnknowHeader("Content-Type", "application/json");
}
if(callName){
if (format == OUTPUT_FORMAT_XML) {
if (fmt != kgl::format::json) {
*out << "<" << callName << " whm_version=\"1.0\">";
} else {
*out << "{\"call\":\"" << callName << "\",";
}
int ret = package->process(callName,&context);
context.flush(ret, format);
if (format == OUTPUT_FORMAT_XML) {
context.flush(ret, fmt);
if (fmt != kgl::format::json) {
*out << "</" << callName << ">\n";
} else {
*out << "}";
}
}else{
if (format == OUTPUT_FORMAT_XML) {
if (fmt != kgl::format::json) {
*out << "<result whm_version=\"1.0\">whm_call cann't be empty</result>";
} else {
*out << "{\"status\":\"404\",\"result\":\"whm_call cann't be empty\"}";
Expand Down
15 changes: 8 additions & 7 deletions module/whm/WhmContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ bool WhmContext::add(const char* name, INT64 value) {
void WhmContext::add(const char* name, KString& value) {
add(name, value.c_str());
}
#if 0
WhmDataValue::WhmDataValue() {
type = WhmDataType::OBJ;
encode = false;
Expand Down Expand Up @@ -152,9 +153,9 @@ void WhmDataValue::build(const KString& name, KWStream& s, int format) {
}
}
}

#endif
bool WhmContext::add(const char* name, const char* value, bool encode) {
return data()->add(name, value, encode);
return data()->add(name, value);
}
bool WhmContext::add(const KString& name, const KString& value) {
return data()->add(name, value);
Expand Down Expand Up @@ -190,26 +191,26 @@ bool WhmContext::buildVh() {
}
return true;
}
bool WhmContext::flush(int status, int format) {
bool WhmContext::flush(int status, kgl::format fmt) {
KWStream* out = getOutputStream();
if (status > 0) {
if (format == OUTPUT_FORMAT_XML) {
if (fmt != kgl::format::json) {
*out << "<result status=\"" << status;
if (statusMsg.size() > 0) {
*out << " " << statusMsg;
}
*out << "\">\n";
} else if (format == OUTPUT_FORMAT_JSON) {
} else {
*out << "\"status\":\"" << status;
if (statusMsg.size() > 0) {
*out << " " << statusMsg;
}
*out << "\",\"result\":{\n";
}
}
dv.build(*out, format);
dv.build(*out, fmt);
if (status > 0) {
if (format == OUTPUT_FORMAT_XML) {
if (fmt != kgl::format::json) {
*out << "</result>\n";
} else {
*out << "}";
Expand Down
18 changes: 13 additions & 5 deletions module/whm/WhmContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "KStream.h"
#include "whm.h"
#include "KExtendProgram.h"
#include "serializable.h"
#if 0
#define OUTPUT_FORMAT_XML 0
#define OUTPUT_FORMAT_JSON 1
class WhmDataAttribute;
Expand Down Expand Up @@ -47,6 +49,11 @@ struct WhmDataValue {
};
class WhmDataAttribute : public KDataAttribute {
public:
~WhmDataAttribute() {
for (auto it = data.begin(); it != data.end(); ++it) {
delete (*it).second;
}
}
bool add(const KString& name, const KString& value, bool encode = false) override {
auto it = data.find(name);
if (it == data.end()) {
Expand All @@ -58,7 +65,7 @@ class WhmDataAttribute : public KDataAttribute {
return false;
}
(*it).second->strs->push_back(value);
return false;
return true;
}
bool add(const KString& name, int64_t value) override {
auto it = data.find(name);
Expand Down Expand Up @@ -109,6 +116,7 @@ class WhmDataAttribute : public KDataAttribute {
}
std::map<KString, WhmDataValue*> data;
};
#endif
class WhmContext : public KVirtualHostEvent, public KDynamicString {
public:
WhmContext(KServiceProvider* provider);
Expand Down Expand Up @@ -166,11 +174,11 @@ class WhmContext : public KVirtualHostEvent, public KDynamicString {
this->statusMsg += statusMsg;
}
}
bool flush(int status, int format = OUTPUT_FORMAT_XML);
KDataAttribute* data() override {
bool flush(int status, kgl::format fmt = kgl::format::xml_text);
kgl::serializable* data() override {
return &dv;
}
KDataAttribute* add(const KString& name) {
kgl::serializable* add(const KString& name) {
return data()->add(name);
}
void add(const char* name, KString& value);
Expand All @@ -194,7 +202,7 @@ class WhmContext : public KVirtualHostEvent, public KDynamicString {
}
char* save(char* p);
private:
WhmDataAttribute dv;
kgl::serializable dv;
std::list<char*> memorys;
KServiceProvider* provider;
KUrlValue urlValue;
Expand Down
27 changes: 11 additions & 16 deletions module/whm/WhmShell.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
#include "WhmShell.h"
#include "WhmShellSession.h"

KTHREAD_FUNCTION whmShellAsyncThread(void *param)
{
WhmShellContext *sc = (WhmShellContext *)param;
WhmShell *shell = sc->shell;
KTHREAD_FUNCTION whmShellAsyncThread(void* param) {
WhmShellContext* sc = (WhmShellContext*)param;
WhmShell* shell = sc->shell;
assert(shell);
shell->asyncRun(sc);
KTHREAD_RETURN;
}
WhmShell::WhmShell()
{
WhmShell::WhmShell() {
process = last = NULL;
curProcess = NULL;
head = end = NULL;
index = 0;
merge_context = NULL;
merge_context_running = false;
}
WhmShell::~WhmShell()
{
WhmShell::~WhmShell() {
while (process) {
last = process->next;
delete process;
Expand All @@ -29,12 +26,11 @@ WhmShell::~WhmShell()
delete curProcess;
}
}
void WhmShell::flush()
{
void WhmShell::flush() {
lock.Lock();
while (head && kgl_current_sec - head->closedTime > 30) {
context.erase(head->session);
WhmShellContext *sc = head;
WhmShellContext* sc = head;
head = head->next;
if (head) {
head->prev = NULL;
Expand All @@ -45,12 +41,11 @@ void WhmShell::flush()
}
lock.Unlock();
}
void WhmShell::readStdin(WhmShellContext *sc,WhmContext *context)
{
void WhmShell::readStdin(WhmShellContext* sc, WhmContext* context) {
//查找标准输入数据
auto it = context->getUrlValue()->attribute.find("-");
if (it!=context->getUrlValue()->attribute.end()) {
sc->in_buffer.write_all((*it).second.c_str(),(int)(*it).second.size());
kgl::string stdin_str;
if (context->getUrlValue()->get("-", stdin_str)) {
sc->in_buffer.write_all(stdin_str.c_str(), stdin_str.size());
}
}
void WhmShell::initContext(WhmShellContext *sc,WhmContext *context)
Expand Down
Loading

0 comments on commit 8c74dbf

Please sign in to comment.