From dacc5c555efe9b765f8f326a906dd71bc8d4eb95 Mon Sep 17 00:00:00 2001 From: an-tao <20741618@qq.com> Date: Wed, 9 May 2018 16:14:21 +0800 Subject: [PATCH] add Session class;use pimpl in HttpAppFramework class --- CMakeLists.txt | 2 +- examples/static_link_example/main.cc | 3 +- lib/inc/drogon/HttpAppFramework.h | 37 +++------ lib/inc/drogon/Session.h | 70 ++++++++++++++++ lib/src/HttpAppFramework.cc | 118 ++++++++++++++++++++------- lib/src/HttpContext.cc | 4 +- lib/src/HttpRequest.cc | 6 +- 7 files changed, 178 insertions(+), 62 deletions(-) create mode 100755 lib/inc/drogon/Session.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d28367af..9e88e1ff 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,7 @@ if(Boost_FOUND) endif() -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=${CMAKE_CXX_STD_FLAGS} -fpermissive -g -ggdb") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=${CMAKE_CXX_STD_FLAGS} -g -ggdb -Wall") #string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CXX_FLAGS}") diff --git a/examples/static_link_example/main.cc b/examples/static_link_example/main.cc index 45e44c70..d977f862 100755 --- a/examples/static_link_example/main.cc +++ b/examples/static_link_example/main.cc @@ -1,5 +1,6 @@ -#include #include +#include +#include static const char banner[]=" _ \n" " __| |_ __ ___ __ _ ___ _ __ \n" " / _` | '__/ _ \\ / _` |/ _ \\| '_ \\ \n" diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index 1ff1a50c..074d7294 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -6,42 +6,25 @@ #pragma once -#include -#include -#include +//#include +//#include +//#include +//#include #include + + +#include #include -#include namespace drogon { class HttpAppFramework:public trantor::NonCopyable { public: - HttpAppFramework()= delete; HttpAppFramework(const std::string &ip,uint16_t port); void run(); - + ~HttpAppFramework(); private: - - std::string _ip; - uint16_t _port; - void onAsyncRequest(const HttpRequest& req,std::functioncallback); - void readSendFile(const std::string& filePath,const HttpRequest& req, HttpResponse* resp); -#ifdef USE_UUID - //if uuid package found,we can use a uuid string as session id; - //set _sessionTimeout=0 to disable location session control based on cookies; - uint _sessionTimeout=0; -#endif - bool _enableLastModify=true; - std::set _fileTypeSet={"html","jpg"}; - std::string _rootPath; - - - - //tool funcs -#ifdef USE_UUID - std::string getUuid(); - std::string stringToHex(unsigned char* ptr, long long length); -#endif + bool _run; + std::unique_ptr _implPtr; }; } diff --git a/lib/inc/drogon/Session.h b/lib/inc/drogon/Session.h new file mode 100755 index 00000000..b7a6da4d --- /dev/null +++ b/lib/inc/drogon/Session.h @@ -0,0 +1,70 @@ +// +// Copyright 2018, An Tao. All rights reserved. +// +// Use of this source code is governed by a MIT license +// that can be found in the License file. + +#pragma once +#ifdef USE_STD_ANY + +#include +typedef std::any Any; +#define Any_cast std::any_cast + +#elif USE_BOOST + +#include +typedef boost::any Any; +#define Any_cast boost::any_cast + +#else +#error,must use c++17 or boost +#endif + +typedef std::map SessionMap; +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) + { + 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()) + { + return false; + } + 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 diff --git a/lib/src/HttpAppFramework.cc b/lib/src/HttpAppFramework.cc index aa402fe1..6098530e 100755 --- a/lib/src/HttpAppFramework.cc +++ b/lib/src/HttpAppFramework.cc @@ -4,6 +4,10 @@ // that can be found in the License file. #include +#include +#include +#include +#include #include "HttpServer.h" #include #include @@ -11,41 +15,77 @@ #ifdef USE_UUID #include #endif + +namespace drogon +{ + class HttpAppFrameworkImpl + { + public: + HttpAppFrameworkImpl(const std::string &ip,uint16_t port); + void run(); + ~HttpAppFrameworkImpl(){} + private: + std::string _ip; + uint16_t _port; + void onAsyncRequest(const HttpRequest& req,std::functioncallback); + void readSendFile(const std::string& filePath,const HttpRequest& req, HttpResponse* resp); +#ifdef USE_UUID + //if uuid package found,we can use a uuid string as session id; + //set _sessionTimeout=0 to disable location session control based on cookies; + uint _sessionTimeout=0; +#endif + bool _enableLastModify=true; + std::set _fileTypeSet={"html","jpg"}; + std::string _rootPath; + + + + //tool funcs +#ifdef USE_UUID + std::string getUuid(); + std::string stringToHex(unsigned char* ptr, long long length); +#endif + }; +} + using namespace drogon; using namespace std::placeholders; -HttpAppFramework::HttpAppFramework(const std::string &ip,uint16_t port): -_ip(ip), -_port(port) + +HttpAppFrameworkImpl::HttpAppFrameworkImpl(const std::string &ip,uint16_t port): + _ip(ip), + _port(port) { } -void HttpAppFramework::run() + +void HttpAppFrameworkImpl::run() { trantor::EventLoop loop; HttpServer httpServer(&loop,InetAddress(_ip,_port),""); - httpServer.setHttpAsyncCallback(std::bind(&HttpAppFramework::onAsyncRequest,this,_1,_2)); + httpServer.setHttpAsyncCallback(std::bind(&HttpAppFrameworkImpl::onAsyncRequest,this,_1,_2)); httpServer.start(); loop.loop(); } -void HttpAppFramework::onAsyncRequest(const HttpRequest& req,std::functioncallback) + +void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequest& req,std::functioncallback) { LOG_TRACE << "Headers " << req.methodString() << " " << req.path(); #if 1 - const std::map& headers = req.headers(); - for (std::map::const_iterator it = headers.begin(); - it != headers.end(); - ++it) { - LOG_TRACE << it->first << ": " << it->second; - } + const std::map& headers = req.headers(); + for (std::map::const_iterator it = headers.begin(); + it != headers.end(); + ++it) { + LOG_TRACE << it->first << ": " << it->second; + } - LOG_TRACE<<"cookies:"; - auto cookies = req.cookies(); - for(auto it=cookies.begin();it!=cookies.end();++it) - { - LOG_TRACE<first<<"="<second; - } + LOG_TRACE<<"cookies:"; + auto cookies = req.cookies(); + for(auto it=cookies.begin();it!=cookies.end();++it) + { + LOG_TRACE<first<<"="<second; + } #endif @@ -154,20 +194,20 @@ void HttpAppFramework::onAsyncRequest(const HttpRequest& req,std::functionsetBody(str); delete contents; } - #ifdef USE_UUID -std::string HttpAppFramework::getUuid() +std::string HttpAppFrameworkImpl::getUuid() { uuid_t uu; uuid_generate(uu); return stringToHex(uu, 16); } -std::string HttpAppFramework::stringToHex(unsigned char* ptr, long long length) +std::string HttpAppFrameworkImpl::stringToHex(unsigned char* ptr, long long length) { std::string idString; for (long long i = 0; i < length; i++) @@ -258,3 +297,26 @@ std::string HttpAppFramework::stringToHex(unsigned char* ptr, long long length) return idString; } #endif + + + +HttpAppFramework::HttpAppFramework(const std::string &ip,uint16_t port): + _implPtr(new HttpAppFrameworkImpl(ip,port)) +{ + +} + + + +void HttpAppFramework::run() +{ + _implPtr->run(); +} + +HttpAppFramework::~HttpAppFramework() +{ + +} + + + diff --git a/lib/src/HttpContext.cc b/lib/src/HttpContext.cc index 89034ff9..39702159 100755 --- a/lib/src/HttpContext.cc +++ b/lib/src/HttpContext.cc @@ -361,7 +361,7 @@ bool HttpContext::parseResponse(MsgBuffer *buf) const char *crlf = buf->findCRLF(); if (crlf) { - if (response_.current_chunk_length_ == crlf - buf->peek()) + if (response_.current_chunk_length_ == (size_t)(crlf - buf->peek())) { //current chunk end crlf response_.body_ += std::string(buf->peek(), response_.current_chunk_length_); @@ -369,7 +369,7 @@ bool HttpContext::parseResponse(MsgBuffer *buf) response_.current_chunk_length_ = 0; res_state_ = HttpResponseParseState::kExpectChunkLen; } - else if (response_.current_chunk_length_ > crlf - buf->peek()) + else if (response_.current_chunk_length_ > (size_t)(crlf - buf->peek())) { //current chunk body crlf response_.body_ += std::string(buf->peek(), crlf - buf->peek() + 1); diff --git a/lib/src/HttpRequest.cc b/lib/src/HttpRequest.cc index 4ac0c6b8..a55b3a8b 100755 --- a/lib/src/HttpRequest.cc +++ b/lib/src/HttpRequest.cc @@ -19,13 +19,13 @@ #include static int urldecode(const char* encd,char* decd) { - int j,i; + int j; char *cd =(char*) encd; char p[2]; - unsigned int num; + j=0; - for( i = 0; i < strlen(cd); i++ ) + for( size_t i = 0; i < strlen(cd); i++ ) { memset( p,0,2); if( cd[i] != '%' )