diff --git a/include/net/http_interceptor.h b/include/net/http_interceptor.h index dce1da2..abd59b5 100644 --- a/include/net/http_interceptor.h +++ b/include/net/http_interceptor.h @@ -5,6 +5,7 @@ #include #include "net/http_interface.h" #include "util/array.hpp" +#include namespace ylib { @@ -27,11 +28,15 @@ namespace ylib public: interceptor(); ~interceptor(); - size_t add(const std::string& regex_express, std::function callback); + bool add(const std::string& regex_express, std::function callback); + bool remove(const std::string& regex_express); + bool exist(const std::string& regex_express); bool trigger(const std::string& url, network::http::reqpack* rp); void clear(); private: - ylib::nolock_array m_array; + std::vector m_list; + + std::shared_mutex m_rw_mutex; }; } } diff --git a/src/net/http_interceptor.cpp b/src/net/http_interceptor.cpp index 812418d..7735d3d 100644 --- a/src/net/http_interceptor.cpp +++ b/src/net/http_interceptor.cpp @@ -13,23 +13,57 @@ network::http::interceptor::~interceptor() { clear(); } -size_t network::http::interceptor::add(const std::string& express, std::function callback) +bool network::http::interceptor::add(const std::string& express, std::function callback) { - network::http::interceptor_info *info = new network::http::interceptor_info; - info->express = std::regex(express.c_str()); - info->callback = callback; - info->express_string = express; - return m_array.append(info); + std::unique_lock lock(m_rw_mutex); + // 判断是否存在 + for (size_t i = 0; i < m_list.size(); i++) + { + if (m_list.at(i).express_string == express) + return false; + } + network::http::interceptor_info info; + info.express = std::regex(express.c_str()); + info.callback = callback; + info.express_string = express; + m_list.push_back(info); + return true; +} +bool ylib::network::http::interceptor::remove(const std::string& regex_express) +{ + std::unique_lock lock(m_rw_mutex); + for(auto iter = m_list.begin();iter != m_list.end();) + { + if (iter->express_string == regex_express) + { + m_list.erase(iter); + return true; + } + else + iter++; + } + return false; +} +bool ylib::network::http::interceptor::exist(const std::string& regex_express) +{ + std::shared_lock lock(m_rw_mutex); + for (size_t i = 0; i < m_list.size(); i++) + { + if (m_list.at(i).express_string == regex_express) + return true; + } + return false; } bool network::http::interceptor::trigger(const std::string& url, network::http::reqpack* rp) { - if(m_array.m_count == 0) + std::shared_lock lock(m_rw_mutex); + + if(m_list.size() == 0) return true; - for(size_t i=0;iexpress)){ - bool result = info->callback(rp,info->express_string); + if(std::regex_match(url.c_str(),m_list[i].express)) { + bool result = m_list[i].callback(rp, m_list[i].express_string); if(result == false){ #if HTTP_INTERCEPTOR_PRINT == 1 ylib::log->warn("["+rp->exec_msec()+" ms] false url:"+url+"\t"+" express:"+info->express_string+" ip:"+rp->request()->remote_ipaddress(true),"interceptor"); @@ -42,8 +76,7 @@ bool network::http::interceptor::trigger(const std::string& url, network::http:: } void ylib::network::http::interceptor::clear() { - for (size_t i = 0; i < m_array.size(); i++) - delete m_array.get(i); - m_array.free(); + std::unique_lock lock(m_rw_mutex); + m_list.clear(); } #endif