diff --git a/examples/simple_example/main.cc b/examples/simple_example/main.cc index cdde48f9..2b565f43 100755 --- a/examples/simple_example/main.cc +++ b/examples/simple_example/main.cc @@ -84,6 +84,10 @@ int main() drogon::HttpAppFramework::instance().addListener("0.0.0.0",12345); drogon::HttpAppFramework::instance().addListener("0.0.0.0",8080); + //https + drogon::HttpAppFramework::instance().setSSLFiles("server.pem","server.pem"); + drogon::HttpAppFramework::instance().addListener("0.0.0.0",4430,true); + drogon::HttpAppFramework::instance().addListener("0.0.0.0",4431,true); drogon::HttpAppFramework::instance().setThreadNum(4); trantor::Logger::setLogLevel(trantor::Logger::TRACE); //class function diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index 0ed1e1fe..f065bea6 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -51,7 +51,9 @@ namespace drogon public: static HttpAppFramework &instance(); virtual void setThreadNum(size_t threadNum)=0; - virtual void addListener(const std::string &ip,uint16_t port)=0; + virtual void setSSLFiles(const std::string &certPath, + const std::string &keyPath)=0; + virtual void addListener(const std::string &ip,uint16_t port,bool useSSL=false)=0; virtual void run()=0; virtual ~HttpAppFramework(); virtual void registerHttpSimpleController(const std::string &pathName, diff --git a/lib/src/HttpAppFramework.cc b/lib/src/HttpAppFramework.cc index bd8d811a..fd6515fb 100755 --- a/lib/src/HttpAppFramework.cc +++ b/lib/src/HttpAppFramework.cc @@ -32,6 +32,8 @@ #include #include #include +#include +#include namespace drogon { @@ -39,8 +41,10 @@ namespace drogon class HttpAppFrameworkImpl:public HttpAppFramework { public: - virtual void addListener(const std::string &ip,uint16_t port) override; + virtual void addListener(const std::string &ip,uint16_t port,bool useSSL=false) override; virtual void setThreadNum(size_t threadNum) override; + virtual void setSSLFiles(const std::string &certPath, + const std::string &keyPath) override; virtual void run() override ; virtual void registerHttpSimpleController(const std::string &pathName,const std::string &crtlName,const std::vector &filters= std::vector())override ; @@ -55,7 +59,7 @@ namespace drogon virtual void enableDynamicSharedLibLoading(const std::vector &libPaths) override; ~HttpAppFrameworkImpl(){} private: - std::vector> _listeners; + std::vector> _listeners; void onAsyncRequest(const HttpRequest& req,const std::function & callback); void readSendFile(const std::string& filePath,const HttpRequest& req, HttpResponse* resp); void addApiPath(const std::string &path, @@ -116,6 +120,9 @@ namespace drogon std::unique_ptr_sharedLibManagerPtr; trantor::EventLoop _loop; + + std::string _sslCertPath; + std::string _sslKeyPath; }; } @@ -248,10 +255,21 @@ void HttpAppFrameworkImpl::setThreadNum(size_t threadNum) assert(threadNum>=1); _threadNum=threadNum; } -void HttpAppFrameworkImpl::addListener(const std::string &ip, uint16_t port) +void HttpAppFrameworkImpl::setSSLFiles(const std::string &certPath, + const std::string &keyPath) +{ + _sslCertPath=certPath; + _sslKeyPath=keyPath; +} +void HttpAppFrameworkImpl::addListener(const std::string &ip, uint16_t port,bool useSSL) { assert(!_running); - _listeners.push_back(std::make_pair(ip,port)); + +#ifndef USE_OPENSSL + assert(!useSSL); +#endif + + _listeners.push_back(std::make_tuple(ip,port,useSSL)); } void HttpAppFrameworkImpl::run() @@ -264,16 +282,47 @@ void HttpAppFrameworkImpl::run() for(auto listener:_listeners) { LOG_DEBUG<<"thread num="<<_threadNum; +#ifdef __linux__ for(size_t i=0;i<_threadNum;i++) { auto loopThreadPtr=std::make_shared(); loopThreadPtr->run(); loopThreads.push_back(loopThreadPtr); - auto serverPtr=std::make_shared(loopThreadPtr->getLoop(),InetAddress(listener.first,listener.second),"drogon"); + auto serverPtr=std::make_shared(loopThreadPtr->getLoop(), + InetAddress(std::get<0>(listener),std::get<1>(listener)),"drogon"); + if(std::get<2>(listener)) + { + //enable ssl; +#ifdef USE_OPENSSL + assert(!_sslCertPath.empty()); + assert(!_sslKeyPath.empty()); + serverPtr->enableSSL(_sslCertPath,_sslKeyPath); +#endif + } serverPtr->setHttpAsyncCallback(std::bind(&HttpAppFrameworkImpl::onAsyncRequest,this,_1,_2)); serverPtr->start(); servers.push_back(serverPtr); } +#else + auto loopThreadPtr=std::make_shared(); + loopThreadPtr->run(); + loopThreads.push_back(loopThreadPtr); + auto serverPtr=std::make_shared(loopThreadPtr->getLoop(), + InetAddress(std::get<0>(listener),std::get<1>(listener)),"drogon"); + if(std::get<2>(listener)) + { + //enable ssl; +#ifdef USE_OPENSSL + assert(!_sslCertPath.empty()); + assert(!_sslKeyPath.empty()); + serverPtr->enableSSL(_sslCertPath,_sslKeyPath); +#endif + } + serverPtr->setIoLoopNum(_threadNum); + serverPtr->setHttpAsyncCallback(std::bind(&HttpAppFrameworkImpl::onAsyncRequest,this,_1,_2)); + serverPtr->start(); + servers.push_back(serverPtr); +#endif } int interval,limit; if(_sessionTimeout==0) diff --git a/lib/src/HttpServer.h b/lib/src/HttpServer.h index 769349b4..0eed2200 100755 --- a/lib/src/HttpServer.h +++ b/lib/src/HttpServer.h @@ -53,6 +53,12 @@ namespace drogon void start(); +#ifdef USE_OPENSSL + void enableSSL(const std::string &certPath,const std::string &keyPath){ + server_.enableSSL(certPath,keyPath); + } +#endif + private: void onConnection(const TcpConnectionPtr& conn); void onMessage(const TcpConnectionPtr&, diff --git a/trantor b/trantor index 6c41c674..c6c79cf6 160000 --- a/trantor +++ b/trantor @@ -1 +1 @@ -Subproject commit 6c41c674048b8552d369dfa494f17872ae58d049 +Subproject commit c6c79cf681610533416245cae477f737666c977f