Skip to content

Commit

Permalink
improve config/vh/bind config
Browse files Browse the repository at this point in the history
  • Loading branch information
keengo99 committed Dec 10, 2024
1 parent 8fcdbbb commit 5bc2c2b
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 36 deletions.
1 change: 1 addition & 0 deletions include/KAccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class KAccess final : public kconfig::KConfigListen
friend class KTable;
static std::map<KString, KAcl*> acl_factorys[2];
static std::map<KString, KMark*> mark_factorys[2];
static void remove_all_factorys();
static bool addAclModel(u_short type, KAcl* acl, bool replace = false);
static bool addMarkModel(u_short type, KMark* acl, bool replace = false);
static void build_action_attribute(KXmlAttribute& attribute, const KUrlValue& uv);
Expand Down
1 change: 1 addition & 0 deletions include/KConfigTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace kconfig {

}
virtual KConfigEventFlag config_flag() const = 0;
/* return false event will pass to parent node */
virtual bool on_config_event(KConfigTree* tree, KConfigEvent* ev) = 0;
};

Expand Down
12 changes: 12 additions & 0 deletions include/KVirtualHostContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ class KVirtualHostContainer : public KAtomCountable
auto lock = get_locker();
return root.find(domain);
}
int get_bind_count() {
auto lock = get_locker();
int result = count;
return result;
}
int get_total_bind_count() {
auto lock = get_locker();
int result = total_count;
return result;
}
void bind_vh(KVirtualHost* vh, bool high = false);
void unbind_vh(KVirtualHost* vh);
KLocker get_locker() {
Expand All @@ -161,6 +171,8 @@ class KVirtualHostContainer : public KAtomCountable
protected:
~KVirtualHostContainer();
private:
int count = 0;
int total_count = 0;
KDomainMap root;
KMutex lock;
};
Expand Down
9 changes: 5 additions & 4 deletions include/KVirtualHostManage.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@

class KGTempleteVirtualHost;

class KVirtualHostManage : public kconfig::KConfigListen
class KVirtualHostManage final : public kconfig::KConfigListen
{
public:
KVirtualHostManage();
virtual ~KVirtualHostManage();
~KVirtualHostManage();
void getMenuHtml(KWStream& s, KVirtualHost* vh, KStringBuf& url);
void getHtml(KWStream& s, const KString& vh_name, int id, KUrlValue& attribute);
bool vh_base_action(KUrlValue& attribute, KString& err_msg);
Expand Down Expand Up @@ -52,7 +51,7 @@ class KVirtualHostManage : public kconfig::KConfigListen
bool updateVirtualHost(kconfig::KConfigTree* ct, KVirtualHost* vh);
bool updateVirtualHost(kconfig::KConfigTree* ct, KVirtualHost* vh, KVirtualHost* ov);
//void updateVirtualHost(KVirtualHost* vh, std::list<KSubVirtualHost*>& hosts);
void updateVirtualHost(KVirtualHost* vh, std::list<KString>& binds);
//void updateVirtualHost(KVirtualHost* vh, std::list<KString>& binds);
/*
* 增加虚拟主机
*/
Expand Down Expand Up @@ -82,7 +81,9 @@ class KVirtualHostManage : public kconfig::KConfigListen
static KFiberLocker locker() {
return KFiberLocker(lock);
}
static KVirtualHostManage* get_instance();
private:
KVirtualHostManage();
void getVhIndex(KWStream& s, KVirtualHost* vh, int id);
bool internalAddVirtualHost(kconfig::KConfigTree* ct, KVirtualHost* vh, KVirtualHost* ov);
bool internalRemoveVirtualHost(kconfig::KConfigTree* ct, KVirtualHost* vh);
Expand Down
2 changes: 1 addition & 1 deletion kasync
Submodule kasync updated from 8ce115 to 7ccbfa
12 changes: 12 additions & 0 deletions src/KAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ void KAccess::clear() {
bool KAccess::isGlobal() {
return global_flag;
}
void KAccess::remove_all_factorys() {
for (int i = 0; i < 2; ++i) {
for (auto it = acl_factorys[i].begin(); it != acl_factorys[i].end(); ++it) {
(*it).second->release();
}
acl_factorys[i].clear();
for (auto it = mark_factorys[i].begin(); it != mark_factorys[i].end(); ++it) {
(*it).second->release();
}
mark_factorys[i].clear();
}
}
bool KAccess::addAclModel(u_short type, KAcl* m, bool replace) {
if (type > 1) {
m->add_ref();
Expand Down
2 changes: 1 addition & 1 deletion src/KConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ KConfig::~KConfig() {
}
KGlobalConfig::KGlobalConfig() {
gam = new KAcserverManager;
gvm = new KVirtualHostManage;
gvm = KVirtualHostManage::get_instance();
sysHost = new KVirtualHost("_SYS");
dem = NULL;
select_count = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/KDsoExtend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,18 @@ KDsoExtend::KDsoExtend(const char* name) {
memset(&version, 0, sizeof(version));
}
KDsoExtend::~KDsoExtend() {
dso.unload();
xfree(name);
if (filename) {
xfree(filename);
}
if (orign_filename) {
xfree(orign_filename);
}
for (auto it = upstream.begin(); it != upstream.end(); ++it) {
(*it).second->release();
}
upstream.clear();
}
bool KDsoExtend::RegisterUpstream(kgl_upstream* us) {
//global upstream always open after cache handle.
Expand Down
9 changes: 7 additions & 2 deletions src/KDynamicListen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ static iterator_ret listen_html_iterator(void *data, void *argv)
KListen *listen = (KListen *)data;
KWStream *s = (KWStream*)argv;
kserver *server = listen->server;
KVirtualHostContainer* vhd = (KVirtualHostContainer*)kserver_get_opaque(server);
if (KBIT_TEST(listen->server->flags, KGL_SERVER_START)) {
*s << "<tr>";
bool unix_socket = false;
Expand Down Expand Up @@ -573,8 +574,12 @@ static iterator_ret listen_html_iterator(void *data, void *argv)
}
*s << "</td><td>" << (int)listen->key->global;
*s << "</td><td>" << (int)listen->key->dynamic;
*s << "</td><td>" << katom_get((void *)&server->refs);
*s << "</td>";
*s << "</td><td>" << katom_get((void *)&server->refs) << "</td>";
*s << "<td>" << vhd->get_bind_count()
#if 0
<< " " << vhd->get_total_bind_count()
#endif
<< "</td>";
*s << "</tr>";
}
return iterator_continue;
Expand Down
2 changes: 1 addition & 1 deletion src/KHttpManage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ bool KHttpManage::config() {
s << "</td><td valign=top>";
s << "\n" << klang["success_listen"] << ":<table border=1>";
s << "<tr><td>" << LANG_IP << "</td><td>" << LANG_PORT;
s << "</td><td>" << klang["listen_type"] << "</td><td>" << klang["protocol"] << "</td><td>flags</td><td>global</td><td>dynamic</td><td>" << LANG_REFS << "</td></tr>";
s << "</td><td>" << klang["listen_type"] << "</td><td>" << klang["protocol"] << "</td><td>flags</td><td>global</td><td>dynamic</td><td>" << LANG_REFS << "</td><td>bind</td></tr>";
conf.gvm->GetListenHtml(s);
s << "</table>";
s << "</tr></table>";
Expand Down
60 changes: 48 additions & 12 deletions src/KVirtualHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,9 @@ bool KVirtualHost::setSSLInfo(const KString& certfile, const KString& keyfile, c
kgl_release_ssl_ctx(ssl_ctx);
ssl_ctx = nullptr;
}
if (cert_file.empty()) {
return false;
}
ssl_ctx = refs_ssl_ctx();
if (ssl_ctx) {
kgl_release_ssl_ctx(ssl_ctx);
Expand All @@ -631,7 +634,7 @@ bool KVirtualHost::parse_xml(const khttpd::KXmlNodeBody* body, KVirtualHost* ov)
envs.clear();
parseEnv(attr("envs"));
setDocRoot(attr["doc_root"]);
browse = (attr["browse"] == "on" || attr["browse"]=="1");
browse = (attr["browse"] == "on" || attr["browse"] == "1");

inherit = (attr["inherit"] == "on" || attr["inherit"] == "1");
setAccess(attr["access"]);
Expand Down Expand Up @@ -698,19 +701,41 @@ bool KVirtualHost::parse_xml(const khttpd::KXmlNodeBody* body, KVirtualHost* ov)
}
//parse host
auto host_xml = kconfig::find_child(body, _KS("host"));
if (!host_xml) {
return true;
}
for (uint32_t index = 0;; ++index) {
auto body = host_xml->get_body(index);
if (!body) {
break;
if (host_xml) {
for (uint32_t index = 0;; ++index) {
auto body = host_xml->get_body(index);
if (!body) {
break;
}
auto svh = parse_host(body);
if (!svh) {
continue;
}
hosts.push_back(svh);
}
auto svh = parse_host(body);
if (!svh) {
continue;
}
//parse bind
auto bind_xml = kconfig::find_child(body, _KS("bind"));
if (bind_xml) {
for (uint32_t index = 0;; ++index) {
auto body = bind_xml->get_body(index);
if (!body) {
break;
}
auto bind = body->get_text();
if (!bind) {
continue;
}
if (bind[0] == '!') {
binds.push_back(bind.substr(1));
} else if (bind.find(':') != KString::npos) {
binds.push_back(bind);
} else {
KStringBuf s;
s << "*:" << bind;
binds.push_back(s.c_str());
}
}
hosts.push_back(svh);
}
return true;
}
Expand Down Expand Up @@ -748,6 +773,7 @@ bool KVirtualHost::on_config_event(kconfig::KConfigTree* tree, kconfig::KConfigE
//[[fallthrough]]
case kconfig::EvSubDir | kconfig::EvUpdate:
{
#if 0
if (xml->is_tag(_KS("host"))) {
/* notice parent xml tag event. */
return false;
Expand Down Expand Up @@ -775,19 +801,29 @@ bool KVirtualHost::on_config_event(kconfig::KConfigTree* tree, kconfig::KConfigE
}
conf.gvm->updateVirtualHost(this, binds);
return true;

/* notice parent xml tag event. */
//return false;
}
#endif
}
break;
case kconfig::EvSubDir | kconfig::EvRemove:
#if 0
if (xml->is_tag(_KS("host"))) {
/* notice parent xml tag event. */
return false;
}
if (xml->is_tag(_KS("bind"))) {
//*
std::list<KString> binds;
conf.gvm->updateVirtualHost(this, binds);
return true;
//*/
/* notice parent xml tag event. */
//return false;
}
#endif
break;
}
return false;
Expand Down
9 changes: 7 additions & 2 deletions src/KVirtualHostContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,18 @@ bool convert(const char* domain, bool& wide, domain_t buf, int buf_size) {
void KVirtualHostContainer::bind_vh(KVirtualHost* vh, bool high) {
auto lock = get_locker();
for (auto it2 = vh->hosts.begin(); it2 != vh->hosts.end(); ++it2) {
root.add((*it2)->bind_host, (*it2)->wide, high ? kgl_bind_high : kgl_bind_low, (*it2));
if (root.add((*it2)->bind_host, (*it2)->wide, high ? kgl_bind_high : kgl_bind_low, (*it2))) {
++count;
++total_count;
}
}
}
void KVirtualHostContainer::unbind_vh(KVirtualHost* vh) {
auto lock = get_locker();
for (auto it2 = vh->hosts.begin(); it2 != vh->hosts.end(); ++it2) {
root.del((*it2)->bind_host, (*it2)->wide, (*it2));
if (kgl_del_failed != root.del((*it2)->bind_host, (*it2)->wide, (*it2))) {
--count;
}
}
}
bool KDomainMap::bind(const char* domain, void* vh, kgl_bind_level level) {
Expand Down
27 changes: 14 additions & 13 deletions src/KVirtualHostManage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ void free_ssl_certifycate(void* arg) {
KSslCertificate* cert = (KSslCertificate*)arg;
delete cert;
}
static KVirtualHostManage* vhm = nullptr;
KVirtualHostManage* KVirtualHostManage::get_instance() {
if (!vhm) {
vhm = new KVirtualHostManage();
}
return vhm;
}
bool KVirtualHostManage::on_config_event(kconfig::KConfigTree* tree, kconfig::KConfigEvent* ev) {
auto xml = ev->get_xml();
switch (ev->type) {
Expand Down Expand Up @@ -121,9 +128,7 @@ KVirtualHostManage::KVirtualHostManage() {
}
KVirtualHostManage::~KVirtualHostManage() {
for (auto it4 = avh.begin(); it4 != avh.end(); it4++) {
if (this == conf.gvm) {
InternalUnBindVirtualHost((*it4).second);
}
InternalUnBindVirtualHost((*it4).second);
(*it4).second->release();
}
avh.clear();
Expand Down Expand Up @@ -279,15 +284,15 @@ bool KVirtualHostManage::updateVirtualHost(kconfig::KConfigTree* ct, KVirtualHos
#ifdef ENABLE_VH_RUN_AS
ov->need_kill_process = vh->caculateNeedKillProcess(ov);
#endif
if (ov && this == conf.gvm) {
if (ov) {
internalRemoveVirtualHost(ct, ov);
}
}
if (vh->name.empty()) {
getAutoName(vh->name);
}
bool result = internalAddVirtualHost(ct, vh, ov);
if (ov && this == conf.gvm) {
if (ov) {
InternalUnBindVirtualHost(ov);
}
return result;
Expand Down Expand Up @@ -316,9 +321,7 @@ bool KVirtualHostManage::removeVirtualHost(kconfig::KConfigTree* ct, KVirtualHos
vh->need_kill_process = 1;
#endif
bool result = internalRemoveVirtualHost(ct, vh);
if (this == conf.gvm) {
InternalUnBindVirtualHost(vh);
}
InternalUnBindVirtualHost(vh);
return result;
}
bool KVirtualHostManage::internalAddVirtualHost(kconfig::KConfigTree* ct, KVirtualHost* vh, KVirtualHost* ov) {
Expand All @@ -330,9 +333,7 @@ bool KVirtualHostManage::internalAddVirtualHost(kconfig::KConfigTree* ct, KVirtu
klog(KLOG_ERR, "Cann't add VirtualHost [%s] name duplicate.\n", vh->name.c_str());
return false;
}
if (this == conf.gvm) {
InternalBindVirtualHost(vh);
}
InternalBindVirtualHost(vh);
avh.insert(pair<KString, KVirtualHost*>(vh->name, vh));
vh->add_ref();
return true;
Expand All @@ -353,7 +354,6 @@ bool KVirtualHostManage::internalRemoveVirtualHost(kconfig::KConfigTree* ct, KVi
return false;
}
avh.erase(it);
kassert(this == conf.gvm);
vh->release();
return true;
}
Expand Down Expand Up @@ -1084,7 +1084,7 @@ void KVirtualHostManage::updateVirtualHost(KVirtualHost* vh, std::list<KSubVirtu
}
InternalBindVirtualHost(vh);
}
*/
void KVirtualHostManage::updateVirtualHost(KVirtualHost* vh, std::list<KString>& binds) {
auto vm_locker = locker();
InternalUnBindVirtualHost(vh);
Expand All @@ -1094,6 +1094,7 @@ void KVirtualHostManage::updateVirtualHost(KVirtualHost* vh, std::list<KString>&
}
InternalBindVirtualHost(vh);
}
*/
void KVirtualHostManage::InternalBindVirtualHost(KVirtualHost* vh) {
#ifdef ENABLE_SVH_SSL
if (ssl_config) {
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ int clean_memory_leak_fiber(void* arg, int argc) {
conf.sysHost->release();
packageManage.clean();
shutdown_http_server();
KAccess::remove_all_factorys();
#ifndef HTTP_PROXY
delete conf.gvm;
delete conf.gam;
Expand Down

0 comments on commit 5bc2c2b

Please sign in to comment.