diff --git a/include/net/http_cdn.h b/include/net/http_cdn.h index 3315b74..bcf64ef 100644 --- a/include/net/http_cdn.h +++ b/include/net/http_cdn.h @@ -47,6 +47,7 @@ namespace ylib cdn(); ~cdn(); bool start(const cdn_config& config); + void close(); std::string host(); virtual bool run() override; const cdn_config& config() { return m_config; } diff --git a/include/net/http_interceptor.h b/include/net/http_interceptor.h index 24e33c6..dce1da2 100644 --- a/include/net/http_interceptor.h +++ b/include/net/http_interceptor.h @@ -29,6 +29,7 @@ namespace ylib ~interceptor(); size_t add(const std::string& regex_express, std::function callback); bool trigger(const std::string& url, network::http::reqpack* rp); + void clear(); private: ylib::nolock_array m_array; }; diff --git a/include/net/http_router.h b/include/net/http_router.h index 39131c3..84d94db 100644 --- a/include/net/http_router.h +++ b/include/net/http_router.h @@ -109,14 +109,11 @@ namespace ylib std::string path, network::http::method method ); -#if HTTP_LUA_ENGINE == 1 - void subscribe( - const std::string& lua_filepath, - std::string path, - network::http::method method, - std::function init_state = nullptr - ); -#endif + + /// + /// 清理所有订阅 + /// + void clear_subscribe(); /****************************************************************** * function:其它 * desc:未订阅请求触发该回调 diff --git a/include/net/http_website.h b/include/net/http_website.h index 2f6cebe..f4d1f2e 100644 --- a/include/net/http_website.h +++ b/include/net/http_website.h @@ -49,17 +49,17 @@ namespace ylib ylib::nolock_array* proxy(); private: // 文件缓存 - network::http::cache* m_cache; + network::http::cache* m_cache = nullptr; // SESSION缓存 - ylib::local_storage* m_session_local_storage; + ylib::local_storage* m_session_local_storage = nullptr; // router路由 服务 - network::http::router* m_router; + network::http::router* m_router = nullptr; // CDN服务 - network::http::cdn* m_cdn; + network::http::cdn* m_cdn = nullptr; // HOST std::vector m_hosts; // HTTPS - bool m_https; + bool m_https = false; // 反向代理 ylib::nolock_array m_proxy; // 配置 diff --git a/include/util/array.hpp b/include/util/array.hpp index f0c8540..25c8cd1 100644 --- a/include/util/array.hpp +++ b/include/util/array.hpp @@ -45,7 +45,7 @@ namespace ylib } return m_array[index]; } - + inline size_t size() { return m_count; } T* m_array; size_t m_count; }; diff --git a/include/util/localstorage.h b/include/util/localstorage.h index be3dab9..4770411 100644 --- a/include/util/localstorage.h +++ b/include/util/localstorage.h @@ -3,6 +3,7 @@ #include #include "util/json.h" #include "util/counter.hpp" +#include "base/error.h" namespace leveldb { class DB; @@ -12,7 +13,7 @@ namespace ylib /// /// 本地存储(LevelDB) /// - class local_storage + class local_storage:public ylib::error_base { public: local_storage(); diff --git a/src/net/http_cdn.cpp b/src/net/http_cdn.cpp index 1dcb369..7bd1999 100644 --- a/src/net/http_cdn.cpp +++ b/src/net/http_cdn.cpp @@ -104,6 +104,12 @@ bool network::http::cdn::start(const cdn_config& config){ ::ithread::start(); return true; } +void ylib::network::http::cdn::close() +{ + ::ithread::stop(); + ::ithread::wait(); + m_nodes.clear(); +} std::string network::http::cdn::host(){ int32 score = 0; std::string result_host; diff --git a/src/net/http_center.cpp b/src/net/http_center.cpp index ee0a380..bad5782 100644 --- a/src/net/http_center.cpp +++ b/src/net/http_center.cpp @@ -13,6 +13,7 @@ ylib::network::http::center::center() ylib::network::http::center::~center() { + close(); } bool ylib::network::http::center::create(const start_config& config) { @@ -80,7 +81,16 @@ void ylib::network::http::center::close() delete m_server[i]; } } + for (size_t i = 0; i < m_website.size(); i++) + { + if (m_website[i] != nullptr) + { + m_website[i]->close(); + delete m_website[i]; + } + } m_server.clear(); + m_website.clear(); } ylib::network::http::server* ylib::network::http::center::server(ushort port) diff --git a/src/net/http_interceptor.cpp b/src/net/http_interceptor.cpp index d627463..812418d 100644 --- a/src/net/http_interceptor.cpp +++ b/src/net/http_interceptor.cpp @@ -11,7 +11,7 @@ network::http::interceptor::interceptor() } network::http::interceptor::~interceptor() { - + clear(); } size_t network::http::interceptor::add(const std::string& express, std::function callback) { @@ -40,4 +40,10 @@ bool network::http::interceptor::trigger(const std::string& url, network::http:: } return true; } +void ylib::network::http::interceptor::clear() +{ + for (size_t i = 0; i < m_array.size(); i++) + delete m_array.get(i); + m_array.free(); +} #endif diff --git a/src/net/http_router.cpp b/src/net/http_router.cpp index f8cbcb3..b8a1600 100644 --- a/src/net/http_router.cpp +++ b/src/net/http_router.cpp @@ -67,7 +67,8 @@ void network::http::router::close() delete this->m_threadpool; this->m_threadpool = nullptr; } - + m_interceptor->clear(); + clear_subscribe(); } network::http::interceptor* network::http::router::interceptor(){ m_interceptor->center(center()); @@ -105,21 +106,12 @@ void network::http::router::subscribe( svie->controller_function = function; m_subscribe.append(svie); } -#if HTTP_LUA_ENGINE == 1 -void ylib::network::http::router::subscribe(const std::string& lua_filepath, std::string path, network::http::method method, std::function init_state) +void ylib::network::http::router::clear_subscribe() { -#if HTTP_ROUTER_PRINT == 1 - ylib::log->info("[subscribe][ctl][lua] express:" + path + " method:" + method_to_string(method), "router"); -#endif - network::http::subscribe_info* svie = new network::http::subscribe_info; - svie->controller = false; - svie->method = method; - svie->express = std::regex(path.c_str()); - svie->lua_filepath = lua_filepath; - svie->lua_init_state = init_state; - m_subscribe.append(svie); + for (size_t i = 0; i < m_subscribe.size(); i++) + delete m_subscribe.get(i); + m_subscribe.free(); } -#endif void network::http::router::other(std::function callback) { this->m_callback_other = callback; @@ -229,12 +221,7 @@ void ylib::network::http::router::__thread_callback(reqpack* rp) if (m_callback_other != nullptr) { m_callback_other(rp->request(), rp->response()); }else{ - std::string filepath = rp->request()->filepath(); - if(filepath == "/") - filepath = "/index.html"; - if(rp->response()->send_file(filepath) == false){ - rp->response()->send((std::string)"404 Not found",404,"Not Found"); - } + rp->response()->send((std::string)"Services that have not been processed",500,"Service Unavailable"); } } } diff --git a/src/net/http_website.cpp b/src/net/http_website.cpp index fe569fa..e606bcc 100644 --- a/src/net/http_website.cpp +++ b/src/net/http_website.cpp @@ -159,6 +159,10 @@ bool ylib::network::http::website::start(const website_config& config) void ylib::network::http::website::close() { + m_router->close(); + m_cache->stop(); + m_cdn->close(); + m_session_local_storage->close(); } network::http::router* network::http::website::router() { diff --git a/src/util/localstorage.cpp b/src/util/localstorage.cpp index 013a24e..ffbfc7a 100644 --- a/src/util/localstorage.cpp +++ b/src/util/localstorage.cpp @@ -18,7 +18,12 @@ bool ylib::local_storage::open(const std::string& dirpath) // 打开数据库 leveldb::Status status = leveldb::DB::Open(options, dirpath, &m_db); - return status.ok(); + if (status.ok() == false) + { + m_lastErrorDesc = status.ToString(); + return false; + } + return true; } void ylib::local_storage::close()