From 557cc8b995267d96c987ca8b1a515f9c17e92e4d Mon Sep 17 00:00:00 2001 From: UnknownObject Date: Thu, 2 Jul 2026 10:31:30 +0800 Subject: [PATCH] change CORS interface, add try-catch handler --- UNSWebServerCore/CORSConfig.cpp | 50 +++++++++++- UNSWebServerCore/CORSConfig.h | 17 +++- UNSWebServerCore/CORSProcessor.cpp | 2 +- UNSWebServerCore/ProcessorAdapter.h | 118 ++++++++++++++++++++++++---- 4 files changed, 163 insertions(+), 24 deletions(-) diff --git a/UNSWebServerCore/CORSConfig.cpp b/UNSWebServerCore/CORSConfig.cpp index 2283994..55d2a47 100644 --- a/UNSWebServerCore/CORSConfig.cpp +++ b/UNSWebServerCore/CORSConfig.cpp @@ -116,7 +116,27 @@ void CORSConfig::SetAllowCookie(bool 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); } @@ -126,12 +146,36 @@ void CORSConfig::AddValidateHost(const std::string& 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)); } -void CORSConfig::AddValidateHeaders(const std::string& header) +void CORSConfig::AddValidateHeader(const std::string& header) { validate_headers.insert(header); } + +void CORSConfig::AddValidateUrls(const std::set& urls) +{ + for(const auto& url : urls) + validate_urls.insert(url); +} + +void CORSConfig::AddValidateHosts(const std::set& hosts) +{ + for(const auto& host : hosts) + validate_hosts.insert(host); +} + +void CORSConfig::AddValidateMethods(const std::set& methods) +{ + for(const auto& method : methods) + validate_methods.insert(method); +} + +void CORSConfig::AddValidateHeaders(const std::set& headers) +{ + for(const auto& header : headers) + validate_headers.insert(header); +} diff --git a/UNSWebServerCore/CORSConfig.h b/UNSWebServerCore/CORSConfig.h index e691996..afc3009 100644 --- a/UNSWebServerCore/CORSConfig.h +++ b/UNSWebServerCore/CORSConfig.h @@ -27,10 +27,21 @@ public: public: void SetMaxAge(int max_age); 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 AddValidateMethods(const std::string& method); - void AddValidateHeaders(const std::string& header); + void AddValidateMethod(const std::string& method); + void AddValidateHeader(const std::string& header); + + void AddValidateUrls(const std::set& urls); + void AddValidateHosts(const std::set& hosts); + void AddValidateMethods(const std::set& methods); + void AddValidateHeaders(const std::set& headers); }; extern UNSWSC_DLL_EXPORT CORSConfig GlobalCORSConfig; \ No newline at end of file diff --git a/UNSWebServerCore/CORSProcessor.cpp b/UNSWebServerCore/CORSProcessor.cpp index ed95ee4..dc83165 100644 --- a/UNSWebServerCore/CORSProcessor.cpp +++ b/UNSWebServerCore/CORSProcessor.cpp @@ -1,6 +1,6 @@ #include "CORSProcessor.h" -#include "ServerLogger.h" #include "CORSConfig.h" +#include "ServerLogger.h" #include "UNSResponseBuilder.h" // ----- 辅助函数(文件作用域) ----- diff --git a/UNSWebServerCore/ProcessorAdapter.h b/UNSWebServerCore/ProcessorAdapter.h index 02d6bcf..1d03cf7 100644 --- a/UNSWebServerCore/ProcessorAdapter.h +++ b/UNSWebServerCore/ProcessorAdapter.h @@ -3,8 +3,11 @@ #include "IPTable.h" #include #include "FileReceiver.h" +#include "ServerLogger.h" #include "ServerProcessor.h" +#include "SyncFileReceiver.h" #include "HTTPObjectsBridge.h" // 确保能看到 Request::Impl 结构体 +#include "UNSResponseBuilder.h" namespace uns { @@ -13,7 +16,7 @@ namespace uns public: virtual ~IBlockedIpUpdatable() = default; - // 统一的库内更新接口(这里的 YourIPContainerType 请替换为你 BlockedIPs 的实际类型) + // 统一的库内更新接口 virtual void ApplyIpUpdate(IPTablePtr blocked_ips) = 0; }; @@ -32,20 +35,55 @@ namespace uns // 完美的把 webcc 的驱动流,翻译给用户的纯净业务类 webcc::ResponsePtr Handle(webcc::RequestPtr request) final { - auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique(request))); - // 调用用户的业务类 - uns::ResponsePtr uns_res = user_processor->Handle(uns_req); - return uns_res->GetImpl()->webcc_res; + try + { + auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique(request))); + // 调用用户的业务类 + uns::ResponsePtr uns_res = user_processor->Handle(uns_req); + 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 { - return user_processor->Stream(method); + try + { + 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 { - user_processor->UpdateBlockedIPList(blocked_ips); + try + { + 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)"); + } } }; @@ -62,10 +100,22 @@ namespace uns // 完美的把 webcc 的驱动流,翻译给用户的纯净业务类 webcc::ResponsePtr Handle(webcc::RequestPtr request) final { - auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique(request))); - // 调用用户的业务类 - uns::ResponsePtr uns_res = user_reciver->Execute(uns_req); - return uns_res->GetImpl()->webcc_res; + try + { + auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique(request))); + // 调用用户的业务类 + uns::ResponsePtr uns_res = user_reciver->Execute(uns_req); + 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 @@ -76,7 +126,18 @@ namespace uns void ApplyIpUpdate(IPTablePtr blocked_ips) override { - user_reciver->UpdateBlockedIPs(blocked_ips); + try + { + 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)"); + } } }; @@ -93,10 +154,22 @@ namespace uns // 完美的把 webcc 的驱动流,翻译给用户的纯净业务类 webcc::ResponsePtr Handle(webcc::RequestPtr request) final { - auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique(request))); - // 调用用户的业务类 - uns::ResponsePtr uns_res = user_reciver->Execute(uns_req); - return uns_res->GetImpl()->webcc_res; + try + { + auto uns_req = uns::RequestPtr(new uns::Request(std::make_unique(request))); + // 调用用户的业务类 + uns::ResponsePtr uns_res = user_reciver->Execute(uns_req); + 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 @@ -107,7 +180,18 @@ namespace uns void ApplyIpUpdate(IPTablePtr blocked_ips) override { - user_reciver->UpdateBlockedIPs(blocked_ips); + try + { + 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)"); + } } }; } \ No newline at end of file