新增Https支持

This commit is contained in:
xzl 2017-04-19 17:47:07 +08:00
parent 52e25fc08c
commit 6ff60dd56e
6 changed files with 78 additions and 7 deletions

View File

@ -84,6 +84,7 @@ HttpSession::HttpSession(const std::shared_ptr<ThreadPool> &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<uint32_t>();
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) {

View File

@ -31,9 +31,11 @@ public:
HttpSession(const std::shared_ptr<ThreadPool> &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
{

54
src/Http/HttpsSession.h Normal file
View File

@ -0,0 +1,54 @@
/*
* HttpsSession.h
*
* Created on: 2017419
* 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<ThreadPool> &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_ */

View File

@ -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;

View File

@ -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 最大请求字节数

View File

@ -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<ConsoleChannel>("stdout", LTrace));
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
SSL_Initor::Instance().loadServerPem((exeDir() + ".HttpServer.pem").data());
TcpServer<RtspSession>::Ptr rtspSrv(new TcpServer<RtspSession>());
TcpServer<RtmpSession>::Ptr rtmpSrv(new TcpServer<RtmpSession>());
TcpServer<HttpSession>::Ptr httpSrv(new TcpServer<HttpSession>());
TcpServer<HttpsSession>::Ptr httpsSrv(new TcpServer<HttpsSession>());
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();