154 lines
6.3 KiB
C++
154 lines
6.3 KiB
C++
#ifndef CCWeb_Servlet_H
|
|
#define CCWeb_Servlet_H
|
|
#pragma once
|
|
|
|
#include <CCThreadPool.h>
|
|
#include "CCResponse.h"
|
|
#include "WebSocket/CCWebSocket.h"
|
|
|
|
// 定义 WebServlet 类格式的回调函数宏,用于简化成员函数回调的编写
|
|
#define CC_ServletFunClass(Fun) [this](auto && PH1, auto && PH2) { Fun(std::forward<decltype(PH1)>(PH1), std::forward<decltype(PH2)>(PH2)); }
|
|
// 定义 WebServlet 普通函数格式的回调函数宏,用于简化普通函数回调的编写
|
|
#define CC_ServletFun(Fun) Fun
|
|
|
|
namespace CTL {
|
|
struct StaticResource {
|
|
// 资源类型
|
|
String Type;
|
|
// 资源内容
|
|
CCVector<char> Content;
|
|
// 资源名称
|
|
String Name;
|
|
// 资源路径
|
|
String Path;
|
|
};
|
|
// WebServlet 类,用于处理网络请求和管理服务器状态
|
|
class WebServlet{
|
|
public:
|
|
// 使用 std::function 定义请求处理函数类型
|
|
using RequestFunc = std::function<void(Request& ,Response&)>;
|
|
private:
|
|
// 网络服务器对象
|
|
Network server;
|
|
// 服务器运行标志
|
|
bool ServerFlag = true;
|
|
// Epoll 对象,用于处理 I/O 多路复用
|
|
Epoll ep{};
|
|
// Epoll 事件数组,用于存储 epoll_wait 返回的事件
|
|
CC_EP_EV events[MAX_EVENTS]{};
|
|
// 线程池对象,用于处理并发请求
|
|
ThreadPool M_S_POOl{};
|
|
// 线程池大小配置
|
|
size_t M_Pool_Size = 128,M_Pool_Max = 1024;
|
|
// 线程池中线程的保活时间
|
|
int M_Pool_KeepAliveTime = 1000;
|
|
// 静态资源的 URL 映射
|
|
CCList<CTL::String> StaticPath;
|
|
// 静态资源内容的映射
|
|
CCMap<CTL::String,StaticResource> StaticResources;
|
|
// Web 信号处理函数
|
|
RequestFunc WebSignalFun;
|
|
// 请求处理函数映射
|
|
CCMap<std::string,RequestFunc> RequestFun;
|
|
// CORS 头对象指针,用于处理跨域请求
|
|
CORS* CORS_Header = nullptr;
|
|
// SSL 上下文对象指针,用于 HTTPS 连接
|
|
SSL_CTX* ssl_ctx = nullptr;
|
|
// WebSocket 对象,用于处理 WebSocket 连接
|
|
WebSocket m_WebSocket;
|
|
// WebSocket 连接标志
|
|
bool WebSocketFlag = false;
|
|
// 请求超时时间 单位/毫秒
|
|
unsigned long TimingTime = 1000;
|
|
// 设置请求头缓冲区大小
|
|
unsigned int ReadHBuff = 4096;
|
|
IPVX IPvx = IPV4;
|
|
public:
|
|
// 构造函数
|
|
WebServlet();
|
|
// 析构函数
|
|
~WebServlet();
|
|
// 初始化服务器
|
|
bool Init(CCAddress address,int Port,bool ChokeUp = true);
|
|
// 初始化服务器
|
|
bool Init(const String& address,int Port,bool ChokeUp = true);
|
|
// 设置 WebSocket 配置
|
|
bool SetWebSocket(const WebSocket& ws);
|
|
// 初始化 SSL 配置
|
|
bool InitSSL();
|
|
// 加载 SSL 证书
|
|
bool LoadSSLCertificate(const CTL::String& CertPath) const;
|
|
// 加载 SSL 私钥
|
|
bool LoadSSLKey(const CTL::String &KeyPath, const CTL::String &Passwd) const;
|
|
// 加载 SSL 客户端验证证书
|
|
bool LoadSSLVerify(const CTL::String& CA_Path) const;
|
|
// 运行服务器,支持 WebSocket
|
|
int Running(const WebSocket& ws,bool InitStaticFlag = true);
|
|
// 运行服务器
|
|
int Running(bool InitStaticFlag = true);
|
|
// 停止服务器
|
|
void Stop();
|
|
// 添加静态资源路径 请填写相对路径
|
|
bool AddStaticPath(const CTL::String& Path);
|
|
// 设置 WebServlet 请求处理函数
|
|
void SetWebServlet(const CTL::String& UrlPath,const RequestFunc& RFunc);
|
|
// 设置 WebServlet 普通请求处理函数
|
|
void SetWebServletFun(RequestFunc RFunc);
|
|
// 设置 CORS 配置
|
|
void SetWebCORS(const CORS& cors);
|
|
// 获取 WebSocket 对象
|
|
WebSocket& GetWebSocket();
|
|
// 设置请求超时时间
|
|
void SetTimeout(unsigned long MS);
|
|
// 设置接收数据头缓冲区大小
|
|
void SetReadHeaderBufferSize(unsigned int Size);
|
|
// 设置线程池参数
|
|
void SetThreadPool(size_t Size,size_t Max,int KeepAliveTime);
|
|
// 设置IP类型 必须在Init之前设置
|
|
void SetIPType(IPVX IPvx);
|
|
// 初始化静态资源
|
|
void InitStatic();
|
|
// 设置最大文件上传数量
|
|
static void SetMaxConcurrentFileUploads(int Num);
|
|
private:
|
|
// 打印消息
|
|
void MessagePrint(const CTL::String& str);
|
|
// 检查客户端权限
|
|
bool Permission(CTL::Socket& client) const;
|
|
// 接受客户端连接
|
|
void AcceptingClient();
|
|
// 处理客户端请求
|
|
void ProcessingClientRequests(CTL::Socket& client,CC_EP_EV &ev);
|
|
// 处理数据类型
|
|
int ProcessingDataType(CTL::Socket& client,CC_EP_EV &ev,Request& request,SSL* ssl = nullptr);
|
|
// 获取请求数据
|
|
CTL::String GetRequest(CTL::Socket& client,SSL* ssl = nullptr);
|
|
// 处理请求
|
|
void HandleRequest(CTL::Socket& client,Request& request,SSL* ssl = nullptr);
|
|
// 处理路径
|
|
CTL::String ProcessPath(const CTL::String& path);
|
|
// 初始化目录静态资源
|
|
void DirInitStatic(const CTL::String& Path);
|
|
// 检查路径是否包含映射后缀
|
|
bool hasMapSuffix(const std::string& path);
|
|
// 移除路径中的映射后缀
|
|
std::string removeMapSuffix(const std::string& path);
|
|
// 获取 Application/form-data 类型的数据
|
|
CTL::String GetApp_Form_Data(CTL::Socket& socket,Request& HTTPRequests,SSL* ssl = nullptr);
|
|
// 获取 Multipart/form-data 类型的数据信息
|
|
CCMap<CTL::String,CTL::String> GetMultipart_Form_DataInfo(CTL::Socket& socket,CTL::String& RecvMessage,SSL* ssl = nullptr);
|
|
// 创建新的 SSL 对象
|
|
SSL* NewSSL(const CTL::Socket& client) const;
|
|
void ErrorHandling(CTL::Socket& client,Request& request,SSL* ssl = nullptr);
|
|
String FormatRequest(const String& str);
|
|
};
|
|
// 实现 Running 成员函数,初始化 WebSocket 并设置 WebSocketFlag 标志,然后调用 Running() 启动服务器
|
|
inline int WebServlet::Running(const WebSocket& ws,bool InitStaticFlag) {
|
|
m_WebSocket.Init(ws,this->ssl_ctx);
|
|
WebSocketFlag = true;
|
|
return Running(InitStaticFlag);
|
|
}
|
|
}
|
|
|
|
#endif
|