Distribution_Service/CC_SDK/Include/CCServlet/CORS.h

160 lines
4.3 KiB
C
Raw Permalink Normal View History

2025-11-11 17:46:19 +08:00
#ifndef CC_Web_CORS_H
#define CC_Web_CORS_H
#include <vector>
#include <map>
#include "CCString.h"
#include "CC.h"
#define CC_CORS_Method "GET,POST,PUT,DELETE,OPTIONS"
#define CC_CORS_Header "Content-Type,Authorization,X-Requested-With"
/**
* CTL
*/
namespace CTL {
/**
* CORS类用于管理跨域资源共享(CORS)
*
*
* IP
*/
class CORS
{
public:
CORS() = default;
/**
* CORS对象
* @param cors CORS对象
*/
CORS(const CORS &cors) {
this->Init(cors);
}
/**
* CORS对象
* @param cors CORS对象
*/
void Init(const CORS &cors) {
this->CORSA = cors.CORSA;
this->AllowOrigin = cors.AllowOrigin;
this->IPRestrictions = cors.IPRestrictions;
}
/**
*
* @param Origin
*/
void AddAllowOrigin(const CTL::String& Origin);
/**
* HTTP头到CORS策略中
* @param Key
* @param Value
*/
void AddHeader(const CTL::String& Key,const CTL::String& Value);
/**
* GET, POST等
* @param Value
*/
void SetMethods(const CTL::String& Value);
/**
*
* @param Value
*/
void SetHeaders(const CTL::String& Value);
/**
*
* @return
*/
std::vector<CTL::String> GetAllowOrigin();
/**
* CORS头
* @return CORS头的map
*/
std::map<CTL::String,CTL::String> GetHeader();
/**
* IP限制
* @return IP返回truefalse
*/
bool RestrictIP() const;
/**
* IP
* @param F true表示限制IPfalse表示不限制
*/
void SetIPRestrictions(bool F);
private:
std::map<CTL::String,CTL::String> CORSA;
std::vector<CTL::String> AllowOrigin;
bool IPRestrictions = false;
};
}
// 添加一个允许的源到AllowOrigin列表
inline void CTL::CORS::AddAllowOrigin(const CTL::String& Origin) {
AllowOrigin.push_back(Origin);
}
// 向CORSA map中插入一个键值对
inline void CTL::CORS::AddHeader(const CTL::String &Key, const CTL::String &Value) {
CORSA.insert(std::pair<CTL::String,CTL::String>(Key,Value));
}
// 设置允许的方法插入到CORSA map中
inline void CTL::CORS::SetMethods(const CTL::String &Value) {
CORSA.insert(std::pair<CTL::String,CTL::String>("Access-Control-Allow-Methods",Value));
}
// 设置允许的头插入到CORSA map中
inline void CTL::CORS::SetHeaders(const CTL::String &Value) {
CORSA.insert(std::pair<CTL::String,CTL::String>("Access-Control-Allow-Headers",Value));
}
// 返回AllowOrigin列表
inline std::vector<CTL::String> CTL::CORS::GetAllowOrigin() {
return AllowOrigin;
}
// 构建并返回包含CORS头的map
inline std::map<CTL::String,CTL::String> CTL::CORS::GetHeader() {
2026-03-20 09:51:56 +08:00
std::map<String,String> CORS = CORSA;
String AllowOrigin_Str = "Access-Control-Allow-Origin";
String AllowOrigin_Value = "*";
2025-11-11 17:46:19 +08:00
for (CCVar IP : AllowOrigin) {
if(AllowOrigin_Value.empty()) {
AllowOrigin_Value.append(IP);
}
else {
AllowOrigin_Value.append(",").append(IP);
}
}
2026-03-20 09:51:56 +08:00
if (CORSA.find(AllowOrigin_Str) == CORSA.end()) {
2026-03-24 14:43:26 +08:00
const auto a = std::make_pair(AllowOrigin_Str,AllowOrigin_Value);
CORS.insert(a);
2025-11-11 17:46:19 +08:00
}
return CORS;
}
// 返回IP限制状态
inline bool CTL::CORS::RestrictIP() const {
return IPRestrictions;
}
// 设置IP限制状态
inline void CTL::CORS::SetIPRestrictions(const bool F) {
AllowOrigin.clear();
AllowOrigin.emplace_back("*");
IPRestrictions = F;
}
#endif