http_interceptor.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #pragma once
  2. #include "define.h"
  3. #if USE_NET_HTTP_WEBSITE
  4. #include <regex>
  5. #include <functional>
  6. #include "net/http_interface.h"
  7. #include "util/array.hpp"
  8. #include <shared_mutex>
  9. namespace ylib
  10. {
  11. namespace network
  12. {
  13. namespace http
  14. {
  15. class reqpack;
  16. struct interceptor_info {
  17. std::regex regex;
  18. std::string pattern;
  19. std::string extra;
  20. std::function<bool(network::http::reqpack* rp, const std::string& pattern,const std::string& extra)> callback;
  21. };
  22. /******************************************************
  23. * class:拦截器
  24. ******************************************************/
  25. class interceptor :public ylib::error_base, public network::http::interface_
  26. {
  27. public:
  28. interceptor();
  29. ~interceptor();
  30. bool add(const std::string& pattern,const std::string& extra,std::function<bool(network::http::reqpack* rp, const std::string& pattern, const std::string& extra)> callback);
  31. bool remove(const std::string& pattern);
  32. bool exist(const std::string& pattern);
  33. bool trigger(const std::string& url, network::http::reqpack* rp);
  34. void clear();
  35. private:
  36. std::vector<interceptor_info> m_list;
  37. std::shared_mutex m_rw_mutex;
  38. };
  39. }
  40. }
  41. }
  42. #endif