From 4f2390dbe27213c1cd8dd356b9f575014e870877 Mon Sep 17 00:00:00 2001 From: sylar-yin <564628276@qq.com> Date: Sat, 20 Jul 2019 07:32:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ServletCreator=E5=92=8C?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: sylar-yin <564628276@qq.com> --- sylar/http/http.h | 1 + sylar/http/servlet.cc | 35 ++++++++++++++++++++++++------- sylar/http/servlet.h | 49 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 75 insertions(+), 10 deletions(-) diff --git a/sylar/http/http.h b/sylar/http/http.h index 9a02794..b65691b 100644 --- a/sylar/http/http.h +++ b/sylar/http/http.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include diff --git a/sylar/http/servlet.cc b/sylar/http/servlet.cc index 9154697..2a4be18 100644 --- a/sylar/http/servlet.cc +++ b/sylar/http/servlet.cc @@ -34,13 +34,31 @@ int32_t ServletDispatch::handle(sylar::http::HttpRequest::ptr request void ServletDispatch::addServlet(const std::string& uri, Servlet::ptr slt) { RWMutexType::WriteLock lock(m_mutex); - m_datas[uri] = slt; + m_datas[uri] = std::make_shared(slt); +} + +void ServletDispatch::addServletCreator(const std::string& uri, IServletCreator::ptr creator) { + RWMutexType::WriteLock lock(m_mutex); + m_datas[uri] = creator; +} + +void ServletDispatch::addGlobServletCreator(const std::string& uri, IServletCreator::ptr creator) { + RWMutexType::WriteLock lock(m_mutex); + for(auto it = m_globs.begin(); + it != m_globs.end(); ++it) { + if(it->first == uri) { + m_globs.erase(it); + break; + } + } + m_globs.push_back(std::make_pair(uri, creator)); } void ServletDispatch::addServlet(const std::string& uri ,FunctionServlet::callback cb) { RWMutexType::WriteLock lock(m_mutex); - m_datas[uri].reset(new FunctionServlet(cb)); + m_datas[uri] = std::make_shared( + std::make_shared(cb)); } void ServletDispatch::addGlobServlet(const std::string& uri @@ -53,12 +71,13 @@ void ServletDispatch::addGlobServlet(const std::string& uri break; } } - m_globs.push_back(std::make_pair(uri, slt)); + m_globs.push_back(std::make_pair(uri + , std::make_shared(slt))); } void ServletDispatch::addGlobServlet(const std::string& uri ,FunctionServlet::callback cb) { - return addGlobServlet(uri, FunctionServlet::ptr(new FunctionServlet(cb))); + return addGlobServlet(uri, std::make_shared(cb)); } void ServletDispatch::delServlet(const std::string& uri) { @@ -80,7 +99,7 @@ void ServletDispatch::delGlobServlet(const std::string& uri) { Servlet::ptr ServletDispatch::getServlet(const std::string& uri) { RWMutexType::ReadLock lock(m_mutex); auto it = m_datas.find(uri); - return it == m_datas.end() ? nullptr : it->second; + return it == m_datas.end() ? nullptr : it->second->get(); } Servlet::ptr ServletDispatch::getGlobServlet(const std::string& uri) { @@ -88,7 +107,7 @@ Servlet::ptr ServletDispatch::getGlobServlet(const std::string& uri) { for(auto it = m_globs.begin(); it != m_globs.end(); ++it) { if(it->first == uri) { - return it->second; + return it->second->get(); } } return nullptr; @@ -98,12 +117,12 @@ Servlet::ptr ServletDispatch::getMatchedServlet(const std::string& uri) { RWMutexType::ReadLock lock(m_mutex); auto mit = m_datas.find(uri); if(mit != m_datas.end()) { - return mit->second; + return mit->second->get(); } for(auto it = m_globs.begin(); it != m_globs.end(); ++it) { if(!fnmatch(it->first.c_str(), uri.c_str(), 0)) { - return it->second; + return it->second->get(); } } return m_default; diff --git a/sylar/http/servlet.h b/sylar/http/servlet.h index 184c62b..a8913e4 100644 --- a/sylar/http/servlet.h +++ b/sylar/http/servlet.h @@ -87,6 +87,38 @@ class FunctionServlet : public Servlet { callback m_cb; }; +class IServletCreator { +public: + typedef std::shared_ptr ptr; + virtual ~IServletCreator() {} + virtual Servlet::ptr get() const = 0; +}; + +class HoldServletCreator : public IServletCreator { +public: + typedef std::shared_ptr ptr; + HoldServletCreator(Servlet::ptr slt) + :m_servlet(slt) { + } + + Servlet::ptr get() const override { + return m_servlet; + } +private: + Servlet::ptr m_servlet; +}; + +template +class ServletCreator : public IServletCreator { +public: + typedef std::shared_ptr ptr; + ServletCreator() { + } + Servlet::ptr get() const override { + return Servlet::ptr(new T); + } +}; + /** * @brief Servlet分发器 */ @@ -133,6 +165,19 @@ class ServletDispatch : public Servlet { */ void addGlobServlet(const std::string& uri, FunctionServlet::callback cb); + void addServletCreator(const std::string& uri, IServletCreator::ptr creator); + void addGlobServletCreator(const std::string& uri, IServletCreator::ptr creator); + + template + void addServletCreator(const std::string& uri) { + addServletCreator(uri, std::make_shared >()); + } + + template + void addGlobServletCreator(const std::string& uri) { + addGlobServletCreator(uri, std::make_shared >()); + } + /** * @brief 删除servlet * @param[in] uri uri @@ -182,10 +227,10 @@ class ServletDispatch : public Servlet { RWMutexType m_mutex; /// 精准匹配servlet MAP /// uri(/sylar/xxx) -> servlet - std::unordered_map m_datas; + std::unordered_map m_datas; /// 模糊匹配servlet 数组 /// uri(/sylar/*) -> servlet - std::vector > m_globs; + std::vector > m_globs; /// 默认servlet,所有路径都没匹配到时使用 Servlet::ptr m_default; };