|
|
@@ -1,110 +1,61 @@
|
|
|
-#include "subscribemanager.h"
|
|
|
+#include "core/subscribemanager.h"
|
|
|
#include "core/config.h"
|
|
|
#include "core/app.h"
|
|
|
#include "core/statemanager.h"
|
|
|
#include "module/http/request.h"
|
|
|
#include "module/http/response.h"
|
|
|
-fastweb::subscribe_manager::subscribe_manager(fastweb::app* ptr):Interface(ptr)
|
|
|
+#include "net/http_subscribe.h"
|
|
|
+fastweb::subscribe_manager::subscribe_manager(fastweb::app* app) :Interface(app)
|
|
|
{
|
|
|
}
|
|
|
fastweb::subscribe_manager::~subscribe_manager()
|
|
|
{
|
|
|
clear();
|
|
|
}
|
|
|
-void fastweb::subscribe_manager::load(network::http::router* router)
|
|
|
+void fastweb::subscribe_manager::start()
|
|
|
{
|
|
|
- clear();
|
|
|
- m_router = router;
|
|
|
- router->other([&](network::http::request* request, network::http::response* response) {
|
|
|
- this->other(request,response);
|
|
|
+
|
|
|
+ app()->router->other([&](network::http::request* request, network::http::response* response) {
|
|
|
+ this->other(request, response);
|
|
|
+ });
|
|
|
+}
|
|
|
+bool fastweb::subscribe_manager::add(const std::string& pattern, const std::string& filepath)
|
|
|
+{
|
|
|
+ app()->router->subscribe()->add(pattern, filepath, [&](network::http::request* request, network::http::response* response, const std::string& pattern, const std::string& filepath)->bool {
|
|
|
+ return this->callback(request, response, pattern, filepath);
|
|
|
});
|
|
|
+ return true;
|
|
|
}
|
|
|
-
|
|
|
-void fastweb::subscribe_manager::clear()
|
|
|
+bool fastweb::subscribe_manager::remove(const std::string& pattern)
|
|
|
{
|
|
|
- if (m_router != nullptr)
|
|
|
- {
|
|
|
- m_router->clear_subscribe();
|
|
|
- }
|
|
|
- for (size_t i = 0; i < m_subextra.size(); i++)
|
|
|
- delete m_subextra[i];
|
|
|
- m_subextra.clear();
|
|
|
- m_router = nullptr;
|
|
|
+ return app()->router->subscribe()->remove(pattern);
|
|
|
}
|
|
|
-void fastweb::subscribe_manager::other(network::http::request* request, network::http::response* response)
|
|
|
+bool fastweb::subscribe_manager::exist(const std::string& pattern)
|
|
|
{
|
|
|
- auto send_404 = [&](network::http::response* response) {
|
|
|
- std::string default_404 = app()->config->website.dir + "\\" + app()->config->website.default_404;
|
|
|
- if (app()->config->website.default_404 == "" || ylib::file::exist(default_404) == false)
|
|
|
- {
|
|
|
- response->send((std::string)"404 Not Found", 404, "Not Found");
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- response->send_file(default_404, -1, 404, "Not Found");
|
|
|
- }
|
|
|
- };
|
|
|
- auto send_file = [&](network::http::response* response, std::string filepath)
|
|
|
- {
|
|
|
- if (ylib::file::exist(filepath))
|
|
|
- {
|
|
|
- if (ylib::file::ext(filepath) == "lua")
|
|
|
- {
|
|
|
- exec(filepath,request,response);
|
|
|
- return;
|
|
|
- }
|
|
|
- response->send_file(filepath);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- send_404(response);
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- std::string filepath;
|
|
|
- if (strutils::right(request->filepath(),1) == "/")
|
|
|
- {
|
|
|
- for (size_t i = 0; i < app()->config->website.default_index.size(); i++)
|
|
|
- {
|
|
|
- filepath = app()->config->website.dir + request->filepath() + app()->config->website.default_index[i];
|
|
|
- if (ylib::file::exist(filepath))
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- filepath = app()->config->website.dir + request->filepath();
|
|
|
- send_file(response, filepath);
|
|
|
+ return app()->router->subscribe()->exist(pattern);
|
|
|
}
|
|
|
-
|
|
|
-void fastweb::subscribe_manager::exec(const std::string& filepath, network::http::request* request, network::http::response* response)
|
|
|
+void fastweb::subscribe_manager::clear()
|
|
|
{
|
|
|
+ if(app()->router != nullptr)
|
|
|
+ app()->router->subscribe()->clear();
|
|
|
+}
|
|
|
|
|
|
+bool fastweb::subscribe_manager::callback(network::http::request* request, network::http::response* response, const std::string& pattern, const std::string& filepath)
|
|
|
+{
|
|
|
+ bool ok_continue = false;
|
|
|
auto lua = app()->state->get();
|
|
|
std::string exception_string;
|
|
|
try
|
|
|
{
|
|
|
- //auto lbResult = lua->state->load_file(filepath);
|
|
|
- //if (lbResult.valid() == false)
|
|
|
- //{
|
|
|
- // sol::error err = lbResult;
|
|
|
- // throw ylib::exception("Failed to load script, " + std::string(err.what()));
|
|
|
- //}
|
|
|
- //module::request m_request(request);
|
|
|
- //module::response m_response(response);
|
|
|
-
|
|
|
- //(*lua->state)["response"] = &m_response;
|
|
|
- //(*lua->state)["request"] = &m_request;
|
|
|
- //lbResult();
|
|
|
-
|
|
|
- sol::load_result script = lua->state->load_file(filepath);
|
|
|
+ sol::load_result script = lua->state->load_file(app()->config->website.dir + filepath);
|
|
|
if (!script.valid()) {
|
|
|
sol::error err = script;
|
|
|
throw ylib::exception(err.what());
|
|
|
}
|
|
|
module::request m_request(request);
|
|
|
module::response m_response(response);
|
|
|
- (*lua->state)["response"] = &m_response;
|
|
|
- (*lua->state)["request"] = &m_request;
|
|
|
+ (*lua->state)["response"] = m_response;
|
|
|
+ (*lua->state)["request"] = m_request;
|
|
|
|
|
|
sol::protected_function_result result = script();
|
|
|
if (!result.valid()) {
|
|
|
@@ -116,12 +67,72 @@ void fastweb::subscribe_manager::exec(const std::string& filepath, network::http
|
|
|
{
|
|
|
exception_string = e.what();
|
|
|
if (app()->config->website.debug)
|
|
|
- LOG_ERROR("[subscribe_service][" + request->filepath() + "]: " + e.what());
|
|
|
+ LOG_ERROR("[subscribe][" + request->filepath() + "]: " + e.what());
|
|
|
}
|
|
|
- // 清理
|
|
|
lua->state->collect_garbage();
|
|
|
app()->state->push(lua);
|
|
|
|
|
|
if (exception_string.empty() == false)
|
|
|
throw ylib::exception(exception_string);
|
|
|
+
|
|
|
+ return ok_continue;
|
|
|
+}
|
|
|
+
|
|
|
+void fastweb::subscribe_manager::other(network::http::request* request, network::http::response* response)
|
|
|
+{
|
|
|
+ std::string filepath = request->filepath();
|
|
|
+ if (filepath.empty())
|
|
|
+ filepath = "/";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // 搜寻默认页面
|
|
|
+ if (filepath[filepath.length()-1] == '/')
|
|
|
+ {
|
|
|
+ std::string default_filepath;
|
|
|
+ for (size_t i = 0; i < app()->config->website.default_index.size(); i++)
|
|
|
+ {
|
|
|
+ default_filepath = app()->config->website.dir + filepath + app()->config->website.default_index[i];
|
|
|
+ if (ylib::file::exist(default_filepath))
|
|
|
+ {
|
|
|
+ filepath += app()->config->website.default_index[i];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ std::string ext = ylib::file::ext(filepath);
|
|
|
+ // 判断LUA
|
|
|
+ if (ext == "lua")
|
|
|
+ {
|
|
|
+ if (app()->config->website.direct_url_mapping)
|
|
|
+ {
|
|
|
+ callback(request,response,filepath,filepath);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ send_404(response);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送静态文件
|
|
|
+ std::string absolute_path = app()->config->website.dir + filepath;
|
|
|
+ if (ylib::file::exist(absolute_path))
|
|
|
+ response->send_file(absolute_path);
|
|
|
+ else
|
|
|
+ send_404(response);
|
|
|
+}
|
|
|
+
|
|
|
+void fastweb::subscribe_manager::send_404(network::http::response* response)
|
|
|
+{
|
|
|
+ std::string default_404 = app()->config->website.dir + "\\" + app()->config->website.default_404;
|
|
|
+ if (app()->config->website.default_404 == "" || ylib::file::exist(default_404) == false)
|
|
|
+ {
|
|
|
+ response->send((std::string)"404 Not Found", 404, "Not Found");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ response->send_file(default_404, -1, 404, "Not Found");
|
|
|
+ }
|
|
|
}
|