change CORS interface, add try-catch handler

This commit is contained in:
UnknownObject
2026-07-02 10:31:30 +08:00
parent d7ce0fe00a
commit 557cc8b995
4 changed files with 163 additions and 24 deletions
+47 -3
View File
@@ -116,7 +116,27 @@ void CORSConfig::SetAllowCookie(bool allow)
allow_cookie = allow; allow_cookie = allow;
} }
void CORSConfig::AddValidateUrls(const std::string& url) void CORSConfig::ClearValidateUrls()
{
validate_urls.clear();
}
void CORSConfig::ClearValidateHosts()
{
validate_hosts.clear();
}
void CORSConfig::ClearValidateMethods()
{
validate_methods.clear();
}
void CORSConfig::ClearValidateHeaders()
{
validate_headers.clear();
}
void CORSConfig::AddValidateUrl(const std::string& url)
{ {
validate_urls.insert(url); validate_urls.insert(url);
} }
@@ -126,12 +146,36 @@ void CORSConfig::AddValidateHost(const std::string& host)
validate_hosts.insert(host); validate_hosts.insert(host);
} }
void CORSConfig::AddValidateMethods(const std::string& method) void CORSConfig::AddValidateMethod(const std::string& method)
{ {
validate_methods.insert(uns::tools::ToUpper(method)); validate_methods.insert(uns::tools::ToUpper(method));
} }
void CORSConfig::AddValidateHeaders(const std::string& header) void CORSConfig::AddValidateHeader(const std::string& header)
{ {
validate_headers.insert(header); validate_headers.insert(header);
} }
void CORSConfig::AddValidateUrls(const std::set<std::string>& urls)
{
for(const auto& url : urls)
validate_urls.insert(url);
}
void CORSConfig::AddValidateHosts(const std::set<std::string>& hosts)
{
for(const auto& host : hosts)
validate_hosts.insert(host);
}
void CORSConfig::AddValidateMethods(const std::set<std::string>& methods)
{
for(const auto& method : methods)
validate_methods.insert(method);
}
void CORSConfig::AddValidateHeaders(const std::set<std::string>& headers)
{
for(const auto& header : headers)
validate_headers.insert(header);
}
+14 -3
View File
@@ -27,10 +27,21 @@ public:
public: public:
void SetMaxAge(int max_age); void SetMaxAge(int max_age);
void SetAllowCookie(bool allow); void SetAllowCookie(bool allow);
void AddValidateUrls(const std::string& url);
void ClearValidateUrls();
void ClearValidateHosts();
void ClearValidateMethods();
void ClearValidateHeaders();
void AddValidateUrl(const std::string& url);
void AddValidateHost(const std::string& host); void AddValidateHost(const std::string& host);
void AddValidateMethods(const std::string& method); void AddValidateMethod(const std::string& method);
void AddValidateHeaders(const std::string& header); void AddValidateHeader(const std::string& header);
void AddValidateUrls(const std::set<std::string>& urls);
void AddValidateHosts(const std::set<std::string>& hosts);
void AddValidateMethods(const std::set<std::string>& methods);
void AddValidateHeaders(const std::set<std::string>& headers);
}; };
extern UNSWSC_DLL_EXPORT CORSConfig GlobalCORSConfig; extern UNSWSC_DLL_EXPORT CORSConfig GlobalCORSConfig;
+1 -1
View File
@@ -1,6 +1,6 @@
#include "CORSProcessor.h" #include "CORSProcessor.h"
#include "ServerLogger.h"
#include "CORSConfig.h" #include "CORSConfig.h"
#include "ServerLogger.h"
#include "UNSResponseBuilder.h" #include "UNSResponseBuilder.h"
// ----- 辅助函数(文件作用域) ----- // ----- 辅助函数(文件作用域) -----
+85 -1
View File
@@ -3,8 +3,11 @@
#include "IPTable.h" #include "IPTable.h"
#include <webcc/view.h> #include <webcc/view.h>
#include "FileReceiver.h" #include "FileReceiver.h"
#include "ServerLogger.h"
#include "ServerProcessor.h" #include "ServerProcessor.h"
#include "SyncFileReceiver.h"
#include "HTTPObjectsBridge.h" // 确保能看到 Request::Impl 结构体 #include "HTTPObjectsBridge.h" // 确保能看到 Request::Impl 结构体
#include "UNSResponseBuilder.h"
namespace uns namespace uns
{ {
@@ -13,7 +16,7 @@ namespace uns
public: public:
virtual ~IBlockedIpUpdatable() = default; virtual ~IBlockedIpUpdatable() = default;
// 统一的库内更新接口(这里的 YourIPContainerType 请替换为你 BlockedIPs 的实际类型) // 统一的库内更新接口
virtual void ApplyIpUpdate(IPTablePtr blocked_ips) = 0; virtual void ApplyIpUpdate(IPTablePtr blocked_ips) = 0;
}; };
@@ -31,22 +34,57 @@ namespace uns
// 完美的把 webcc 的驱动流,翻译给用户的纯净业务类 // 完美的把 webcc 的驱动流,翻译给用户的纯净业务类
webcc::ResponsePtr Handle(webcc::RequestPtr request) final webcc::ResponsePtr Handle(webcc::RequestPtr request) final
{
try
{ {
auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique<uns::Request::Impl>(request))); auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique<uns::Request::Impl>(request)));
// 调用用户的业务类 // 调用用户的业务类
uns::ResponsePtr uns_res = user_processor->Handle(uns_req); uns::ResponsePtr uns_res = user_processor->Handle(uns_req);
return uns_res->GetImpl()->webcc_res; return uns_res->GetImpl()->webcc_res;
} }
catch (const std::exception& e)
{
SCLOGF_ERROR("Unhandled Exception in ServerProcessorAdapter(Handle): {}", e.what());
}
catch (...)
{
SCLOGF_FATAL("Unhandled Unknown Exception in ServerProcessorAdapter(Handle)");
}
return uns::ResponseBuilder().InternalServerError()()->GetImpl()->webcc_res; //Default 500
}
bool Stream(const std::string& method) final bool Stream(const std::string& method) final
{
try
{ {
return user_processor->Stream(method); return user_processor->Stream(method);
} }
catch (const std::exception& e)
{
SCLOGF_ERROR("Unhandled Exception in ServerProcessorAdapter(Stream): {}", e.what());
}
catch (...)
{
SCLOGF_FATAL("Unhandled Unknown Exception in ServerProcessorAdapter(Stream)");
}
return false; //Default false
}
void ApplyIpUpdate(IPTablePtr blocked_ips) override void ApplyIpUpdate(IPTablePtr blocked_ips) override
{
try
{ {
user_processor->UpdateBlockedIPList(blocked_ips); user_processor->UpdateBlockedIPList(blocked_ips);
} }
catch (const std::exception& e)
{
SCLOGF_ERROR("Unhandled Exception in ServerProcessorAdapter(ApplyIpUpdate): {}", e.what());
}
catch (...)
{
SCLOGF_FATAL("Unhandled Unknown Exception in ServerProcessorAdapter(ApplyIpUpdate)");
}
}
}; };
class FileReceiverAdapter : public webcc::View, public IBlockedIpUpdatable class FileReceiverAdapter : public webcc::View, public IBlockedIpUpdatable
@@ -61,12 +99,24 @@ namespace uns
// 完美的把 webcc 的驱动流,翻译给用户的纯净业务类 // 完美的把 webcc 的驱动流,翻译给用户的纯净业务类
webcc::ResponsePtr Handle(webcc::RequestPtr request) final webcc::ResponsePtr Handle(webcc::RequestPtr request) final
{
try
{ {
auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique<uns::Request::Impl>(request))); auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique<uns::Request::Impl>(request)));
// 调用用户的业务类 // 调用用户的业务类
uns::ResponsePtr uns_res = user_reciver->Execute(uns_req); uns::ResponsePtr uns_res = user_reciver->Execute(uns_req);
return uns_res->GetImpl()->webcc_res; return uns_res->GetImpl()->webcc_res;
} }
catch (const std::exception& e)
{
SCLOGF_ERROR("Unhandled Exception in FileReceiverAdapter(Handle): {}", e.what());
}
catch (...)
{
SCLOGF_FATAL("Unhandled Unknown Exception in FileReceiverAdapter(Handle)");
}
return uns::ResponseBuilder().InternalServerError()()->GetImpl()->webcc_res; //Default 500
}
bool Stream(const std::string& method) final bool Stream(const std::string& method) final
{ {
@@ -75,9 +125,20 @@ namespace uns
} }
void ApplyIpUpdate(IPTablePtr blocked_ips) override void ApplyIpUpdate(IPTablePtr blocked_ips) override
{
try
{ {
user_reciver->UpdateBlockedIPs(blocked_ips); user_reciver->UpdateBlockedIPs(blocked_ips);
} }
catch (const std::exception& e)
{
SCLOGF_ERROR("Unhandled Exception in FileReceiverAdapter(ApplyIpUpdate): {}", e.what());
}
catch (...)
{
SCLOGF_FATAL("Unhandled Unknown Exception in FileReceiverAdapter(ApplyIpUpdate)");
}
}
}; };
class SyncFileReceiverAdapter : public webcc::View, public IBlockedIpUpdatable class SyncFileReceiverAdapter : public webcc::View, public IBlockedIpUpdatable
@@ -92,12 +153,24 @@ namespace uns
// 完美的把 webcc 的驱动流,翻译给用户的纯净业务类 // 完美的把 webcc 的驱动流,翻译给用户的纯净业务类
webcc::ResponsePtr Handle(webcc::RequestPtr request) final webcc::ResponsePtr Handle(webcc::RequestPtr request) final
{
try
{ {
auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique<uns::Request::Impl>(request))); auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique<uns::Request::Impl>(request)));
// 调用用户的业务类 // 调用用户的业务类
uns::ResponsePtr uns_res = user_reciver->Execute(uns_req); uns::ResponsePtr uns_res = user_reciver->Execute(uns_req);
return uns_res->GetImpl()->webcc_res; return uns_res->GetImpl()->webcc_res;
} }
catch (const std::exception& e)
{
SCLOGF_ERROR("Unhandled Exception in SyncFileReceiverAdapter(Handle): {}", e.what());
}
catch (...)
{
SCLOGF_FATAL("Unhandled Unknown Exception in SyncFileReceiverAdapter(Handle)");
}
return uns::ResponseBuilder().InternalServerError()()->GetImpl()->webcc_res; //Default 500
}
bool Stream(const std::string& method) final bool Stream(const std::string& method) final
{ {
@@ -106,8 +179,19 @@ namespace uns
} }
void ApplyIpUpdate(IPTablePtr blocked_ips) override void ApplyIpUpdate(IPTablePtr blocked_ips) override
{
try
{ {
user_reciver->UpdateBlockedIPs(blocked_ips); user_reciver->UpdateBlockedIPs(blocked_ips);
} }
catch (const std::exception& e)
{
SCLOGF_ERROR("Unhandled Exception in SyncFileReceiverAdapter(ApplyIpUpdate): {}", e.what());
}
catch (...)
{
SCLOGF_FATAL("Unhandled Unknown Exception in SyncFileReceiverAdapter(ApplyIpUpdate)");
}
}
}; };
} }