diff --git a/src/Http/HttpSession.cpp b/src/Http/HttpSession.cpp index 15b27076..6f407e04 100644 --- a/src/Http/HttpSession.cpp +++ b/src/Http/HttpSession.cpp @@ -84,6 +84,7 @@ HttpSession::HttpSession(const std::shared_ptr &pTh, const Socket::P static onceToken token([]() { g_mapCmdIndex.emplace("GET",&HttpSession::Handle_Req_GET); g_mapCmdIndex.emplace("POST",&HttpSession::Handle_Req_POST); + g_mapCmdIndex.emplace("OPTIONS",&HttpSession::Handle_Req_POST); }, nullptr); } @@ -91,15 +92,18 @@ HttpSession::~HttpSession() { //DebugL; } -void HttpSession::onRecv(const Socket::Buffer::Ptr&pBuf) { +void HttpSession::onRecv(const Socket::Buffer::Ptr &pBuf) { + onRecv(pBuf->data(),pBuf->size()); +} +void HttpSession::onRecv(const char *data,int size){ static uint32_t reqSize = mINI::Instance()[Config::Http::kMaxReqSize].as(); m_ticker.resetTime(); - if (m_strRcvBuf.size() + pBuf->size() >= reqSize) { - WarnL << "接收缓冲区溢出:" << m_strRcvBuf.size() + pBuf->size() << "," << reqSize; + if (m_strRcvBuf.size() + size >= reqSize) { + WarnL << "接收缓冲区溢出:" << m_strRcvBuf.size() + size << "," << reqSize; shutdown(); return; } - m_strRcvBuf.append(pBuf->data(), pBuf->size()); + m_strRcvBuf.append(data, size); size_t index; string onePkt; while ((index = m_strRcvBuf.find("\r\n\r\n")) != std::string::npos) { diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index 70fb9264..aba0a44c 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -31,9 +31,11 @@ public: HttpSession(const std::shared_ptr &pTh, const Socket::Ptr &pSock); virtual ~HttpSession(); - void onRecv(const Socket::Buffer::Ptr &) override; - void onError(const SockException &err) override; - void onManager() override; + virtual void onRecv(const Socket::Buffer::Ptr &) override; + virtual void onError(const SockException &err) override; + virtual void onManager() override; +protected: + void onRecv(const char *data,int size); private: typedef enum { diff --git a/src/Http/HttpsSession.h b/src/Http/HttpsSession.h new file mode 100644 index 00000000..f668e250 --- /dev/null +++ b/src/Http/HttpsSession.h @@ -0,0 +1,54 @@ +/* + * HttpsSession.h + * + * Created on: 2017年4月19日 + * Author: xzl + */ + +#ifndef SRC_HTTP_HTTPSSESSION_H_ +#define SRC_HTTP_HTTPSSESSION_H_ + +#include "HttpSession.h" +#include "Util/SSLBox.h" +#include "Util/TimeTicker.h" +using namespace ZL::Util; + +namespace ZL { +namespace Http { + +class HttpsSession: public HttpSession { +public: + HttpsSession(const std::shared_ptr &pTh, const Socket::Ptr &pSock): + HttpSession(pTh,pSock){ + m_sslBox.setOnEncData([&](const char *data, uint32_t len){ + HttpSession::send(data,len); + }); + m_sslBox.setOnDecData([&](const char *data, uint32_t len){ + HttpSession::onRecv(data,len); + }); + } + virtual ~HttpsSession(){ + //m_sslBox.shutdown(); + } + void onRecv(const Socket::Buffer::Ptr &pBuf) override{ + TimeTicker(); + m_sslBox.onRecv(pBuf->data(), pBuf->size()); + } +private: + virtual int send(const string &buf) override{ + TimeTicker(); + m_sslBox.onSend(buf.data(), buf.size()); + return buf.size(); + } + virtual int send(const char *buf, int size) override{ + TimeTicker(); + m_sslBox.onSend(buf, size); + return size; + } + SSL_Box m_sslBox; +}; + +} /* namespace Http */ +} /* namespace ZL */ + +#endif /* SRC_HTTP_HTTPSSESSION_H_ */ diff --git a/src/config.cpp b/src/config.cpp index 3c7be0ce..a541536b 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -37,6 +37,9 @@ namespace Http { #define HTTP_PORT 80 const char kPort[] = HTTP_FIELD"port"; +#define HTTPS_PORT 443 +extern const char kSSLPort[] = HTTP_FIELD"sslport"; + //http 文件发送缓存大小 #define HTTP_SEND_BUF_SIZE (64 * 1024) const char kSendBufSize[] = HTTP_FIELD"sendBufSize"; @@ -85,6 +88,7 @@ const char kHttpPrefix[] = HTTP_FIELD"httpPrefix"; onceToken token([](){ mINI::Instance()[kPort] = HTTP_PORT; + mINI::Instance()[kSSLPort] = HTTPS_PORT; mINI::Instance()[kSendBufSize] = HTTP_SEND_BUF_SIZE; mINI::Instance()[kMaxReqSize] = HTTP_MAX_REQ_SIZE; mINI::Instance()[kKeepAliveSecond] = HTTP_KEEP_ALIVE_SECOND; diff --git a/src/config.h b/src/config.h index 81ad9bc9..5dc51386 100644 --- a/src/config.h +++ b/src/config.h @@ -63,6 +63,7 @@ extern const char kReplayCount[]; ////////////HTTP配置/////////// namespace Http { extern const char kPort[]; +extern const char kSSLPort[]; //http 文件发送缓存大小 extern const char kSendBufSize[]; //http 最大请求字节数 diff --git a/test.cpp b/test.cpp index 1ab4c402..9acb2ab5 100644 --- a/test.cpp +++ b/test.cpp @@ -18,6 +18,8 @@ #include "Poller/EventPoller.hpp" #include "Thread/WorkThreadPool.h" #include "Http/HttpSession.h" +#include "Http/HttpsSession.h" +#include "Util/SSLBox.h" using namespace std; using namespace ZL::Util; @@ -35,19 +37,23 @@ int main(int argc,char *argv[]){ Logger::Instance().add(std::make_shared("stdout", LTrace)); Logger::Instance().setWriter(std::make_shared()); + SSL_Initor::Instance().loadServerPem((exeDir() + ".HttpServer.pem").data()); TcpServer::Ptr rtspSrv(new TcpServer()); TcpServer::Ptr rtmpSrv(new TcpServer()); TcpServer::Ptr httpSrv(new TcpServer()); + TcpServer::Ptr httpsSrv(new TcpServer()); rtspSrv->start(mINI::Instance()[Config::Rtsp::kPort]); rtmpSrv->start(mINI::Instance()[Config::Rtmp::kPort]); httpSrv->start(mINI::Instance()[Config::Http::kPort]); + httpsSrv->start(mINI::Instance()[Config::Http::kSSLPort]); EventPoller::Instance().runLoop(); rtspSrv.reset(); rtmpSrv.reset(); httpSrv.reset(); + httpsSrv.reset(); static onceToken token(nullptr, []() { UDPServer::Destory(); WorkThreadPool::Destory();