interceptormanager.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "core/interceptormanager.h"
  2. #include "core/config.h"
  3. #include "core/app.h"
  4. #include "core/statemanager.h"
  5. #include "module/http/request.h"
  6. #include "module/http/response.h"
  7. #include "net/http_interceptor.h"
  8. fastweb::interceptor_manager::interceptor_manager(fastweb::app* app):Interface(app)
  9. {
  10. }
  11. fastweb::interceptor_manager::~interceptor_manager()
  12. {
  13. clear();
  14. }
  15. bool fastweb::interceptor_manager::add(const std::string& regex_express, const std::string& filepath)
  16. {
  17. if (interceptor_manager::interceptor.exist(regex_express))
  18. {
  19. LOG_WARN("`" + regex_express + "` interceptor is invalid, duplicate rule, filepath: " + filepath);
  20. return false;
  21. }
  22. interceptor_manager::interceptor.add(regex_express, filepath);
  23. app()->router->interceptor()->add(regex_express, [this](network::http::reqpack* reqpack, const std::string& express_string)->bool {
  24. return this->callback(reqpack, express_string);
  25. });
  26. return true;
  27. }
  28. bool fastweb::interceptor_manager::remove(const std::string& regex_express)
  29. {
  30. interceptor_manager::interceptor.del(regex_express);
  31. return app()->router->interceptor()->remove(regex_express);
  32. }
  33. bool fastweb::interceptor_manager::exist(const std::string& regex_express)
  34. {
  35. return interceptor_manager::interceptor.exist(regex_express);
  36. }
  37. void fastweb::interceptor_manager::clear()
  38. {
  39. interceptor_manager::interceptor.clear();
  40. if(app()->router != nullptr)
  41. app()->router->interceptor()->clear();
  42. }
  43. bool fastweb::interceptor_manager::callback(network::http::reqpack* reqpack, const std::string& express_string)
  44. {
  45. bool ok_continue = false;
  46. auto lua = app()->state->get();
  47. std::string exception_string;
  48. try
  49. {
  50. sol::load_result script = lua->state->load_file(interceptor_manager::interceptor[express_string]);
  51. if (!script.valid()) {
  52. sol::error err = script;
  53. throw ylib::exception(err.what());
  54. }
  55. module::request m_request(reqpack->request());
  56. module::response m_response(reqpack->response());
  57. (*lua->state)["response"] = m_response;
  58. (*lua->state)["request"] = m_request;
  59. sol::protected_function_result result = script();
  60. if (!result.valid()) {
  61. sol::error err = result;
  62. throw ylib::exception(err.what());
  63. }
  64. ok_continue = result.get<bool>();
  65. }
  66. catch (const std::exception& e)
  67. {
  68. exception_string = e.what();
  69. if (app()->config->website.debug)
  70. LOG_ERROR("[interceptor][" + reqpack->request()->filepath() + "]: " + e.what());
  71. }
  72. lua->state->collect_garbage();
  73. app()->state->push(lua);
  74. if (exception_string.empty() == false)
  75. throw ylib::exception(exception_string);
  76. return ok_continue;
  77. }