From 72b2a60b53b043d7220c113c162669b9e24953dc Mon Sep 17 00:00:00 2001 From: an-tao <20741618@qq.com> Date: Fri, 11 May 2018 16:57:16 +0800 Subject: [PATCH] add Session in HttpRequest --- lib/inc/drogon/HttpRequest.h | 2 + lib/inc/drogon/HttpResponse.h | 2 +- lib/inc/drogon/HttpSimpleController.h | 12 ++-- lib/inc/drogon/Session.h | 93 +++++++++++++++------------ lib/src/HttpAppFramework.cc | 2 + lib/src/HttpRequestImpl.h | 12 ++++ 6 files changed, 74 insertions(+), 49 deletions(-) diff --git a/lib/inc/drogon/HttpRequest.h b/lib/inc/drogon/HttpRequest.h index 1f5a8494..4a28648b 100755 --- a/lib/inc/drogon/HttpRequest.h +++ b/lib/inc/drogon/HttpRequest.h @@ -5,6 +5,7 @@ #pragma once +#include #include #include @@ -28,6 +29,7 @@ namespace drogon virtual const std::string& query() const=0; virtual const std::string& path() const=0; virtual Version getVersion() const=0; + virtual SessionPtr session() const=0; virtual ~HttpRequest(){} }; } \ No newline at end of file diff --git a/lib/inc/drogon/HttpResponse.h b/lib/inc/drogon/HttpResponse.h index 9253e4ac..60259924 100755 --- a/lib/inc/drogon/HttpResponse.h +++ b/lib/inc/drogon/HttpResponse.h @@ -237,7 +237,7 @@ namespace drogon std::string body_; size_t left_body_length_; size_t current_chunk_length_; - uint8_t contentType_; + uint8_t contentType_=CT_TEXT_HTML; //trantor::Date receiveTime_; void setContentType(const std::string& contentType) diff --git a/lib/inc/drogon/HttpSimpleController.h b/lib/inc/drogon/HttpSimpleController.h index 49847578..f09043d5 100755 --- a/lib/inc/drogon/HttpSimpleController.h +++ b/lib/inc/drogon/HttpSimpleController.h @@ -11,8 +11,8 @@ #include #include #include -#define PATH_LIST_BEGIN private:\ -virtual std::vector paths() override \ +#define PATH_LIST_BEGIN public:\ +static std::vector paths() \ {\ std::vector vet; @@ -31,15 +31,15 @@ namespace drogon virtual ~HttpSimpleController(){} private: - virtual std::vector paths()=0; + //virtual std::vector paths()=0; class pathRegister { public: pathRegister(){ std::cout<<"pathRegister!!"<::classTypeName()<*>(new T); - auto vPaths=ctl->paths(); - delete ctl; +// auto ctl= static_cast*>(new T); + auto vPaths=T::paths(); + //delete ctl; for(auto path:vPaths) { HttpAppFramework::instance().registerHttpSimpleController(path,HttpSimpleController::classTypeName()); diff --git a/lib/inc/drogon/Session.h b/lib/inc/drogon/Session.h index b7a6da4d..f26c476c 100755 --- a/lib/inc/drogon/Session.h +++ b/lib/inc/drogon/Session.h @@ -5,6 +5,11 @@ // that can be found in the License file. #pragma once + +#include +#include +#include +#include #ifdef USE_STD_ANY #include @@ -22,49 +27,53 @@ typedef boost::any Any; #endif typedef std::map SessionMap; -class Session +namespace drogon { -public: - template T get(const std::string &key) const{ - std::lock_guard lck(mutex_); - auto it=sessionMap_.find(key); - if(it!=sessionMap_.end()) + class Session + { + public: + template T get(const std::string &key) const{ + std::lock_guard lck(mutex_); + auto it=sessionMap_.find(key); + if(it!=sessionMap_.end()) + { + return Any_cast(it->second); + } + T tmp; + return tmp; + }; + Any &operator[](const std::string &key){ + std::lock_guard lck(mutex_); + return sessionMap_[key]; + }; + void insert(const std::string& key,const Any &obj) { - return Any_cast(it->second); - } - T tmp; - return tmp; - }; - Any &operator[](const std::string &key){ - std::lock_guard lck(mutex_); - return sessionMap_[key]; - }; - void insert(const std::string& key,const Any &obj) - { - std::lock_guard lck(mutex_); - sessionMap_[key]=obj; - }; - void erase(const std::string& key) - { - std::lock_guard lck(mutex_); - sessionMap_.erase(key); - } - bool find(const std::string& key) - { - std::lock_guard lck(mutex_); - if(sessionMap_.find(key) == sessionMap_.end()) + std::lock_guard lck(mutex_); + sessionMap_[key]=obj; + }; + void erase(const std::string& key) { - return false; + std::lock_guard lck(mutex_); + sessionMap_.erase(key); } - return true; - } - void clear() - { - std::lock_guard lck(mutex_); - sessionMap_.clear(); - } -protected: - SessionMap sessionMap_; - int timeoutInterval_; - mutable std::mutex mutex_; -}; \ No newline at end of file + bool find(const std::string& key) + { + std::lock_guard lck(mutex_); + if(sessionMap_.find(key) == sessionMap_.end()) + { + return false; + } + return true; + } + void clear() + { + std::lock_guard lck(mutex_); + sessionMap_.clear(); + } + protected: + SessionMap sessionMap_; + int timeoutInterval_; + mutable std::mutex mutex_; + }; + typedef std::shared_ptr SessionPtr; +} diff --git a/lib/src/HttpAppFramework.cc b/lib/src/HttpAppFramework.cc index e4430519..426fb04b 100755 --- a/lib/src/HttpAppFramework.cc +++ b/lib/src/HttpAppFramework.cc @@ -2,6 +2,7 @@ // // Use of this source code is governed by a MIT license // that can be found in the License file. +#include "HttpRequestImpl.h" #include #include @@ -178,6 +179,7 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequest& req,std::function filters=(*_filterMap)[req.path().c_str()]; for(std::string filterClassName:filters) diff --git a/lib/src/HttpRequestImpl.h b/lib/src/HttpRequestImpl.h index 243764f7..07bc4530 100755 --- a/lib/src/HttpRequestImpl.h +++ b/lib/src/HttpRequestImpl.h @@ -280,6 +280,16 @@ namespace drogon } void appendToBuffer(MsgBuffer* output) const; + + virtual SessionPtr session() const override + { + return _sessionPtr; + } + + void setSession(const SessionPtr &session) + { + _sessionPtr=session; + } private: Method method_; Version version_; @@ -290,6 +300,8 @@ namespace drogon std::map headers_; std::map cookies_; std::map premeter_; + + SessionPtr _sessionPtr; protected: std::string content_; size_t contentLen;