upgrade core add header check and incrace upload speed
This commit is contained in:
@@ -2,10 +2,10 @@
|
||||
#include "Global.h"
|
||||
#include "IPTable.h"
|
||||
#include <functional>
|
||||
#include "WebFileInfo.h"
|
||||
#include "HTTPObjects.h"
|
||||
#include "TempFileManager.h"
|
||||
|
||||
using FileProcessorCallback = std::function<void(WebFileInfoVec, const std::string&)>;
|
||||
using FileProcessorCallback = std::function<void(TempFileManager&, const std::string&)>;
|
||||
|
||||
class UNSWSC_DLL_EXPORT FileReceiver
|
||||
{
|
||||
@@ -29,12 +29,19 @@ public:
|
||||
uns::HTTPMethod GetMethod(uns::RequestPtr request);
|
||||
void AppenedBlockedIP(DateTime::Span block_time, std::string ip);
|
||||
bool WriteFile(const std::string& path, const std::string& bytes);
|
||||
void SetFileTimeout(std::chrono::seconds timeout = 0s, std::chrono::seconds max_proc_timeout = 0s) noexcept;
|
||||
|
||||
public:
|
||||
// 请求信息预检,重载以在保存文件之前检查请求体
|
||||
virtual uns::Status PreCheckRequest(uns::RequestPtr request) = 0;
|
||||
// 表单预检,重载以在处理表单前检查表单
|
||||
virtual bool PreCheckForm(uns::FormPartPtr form) = 0;
|
||||
// 路径穿越配置,重载以配置允许的路径穿越类型
|
||||
virtual uns::PathTraversalDefenceLevel PTDefence();
|
||||
// 路径穿越防护,重载以实现路径穿越检查,配置为AutoNormalize或AllowNormal时必须,否则自动退化为DenyAll
|
||||
virtual bool IsPathSafe(const std::string& raw_path);
|
||||
// 请求头预检,重载以实现在接收请求体之前检查请求头,返回false则服务器将强制关闭连接
|
||||
virtual bool IsHeaderValid(uns::RequestPtr request);
|
||||
|
||||
public:
|
||||
// 核心驱动入口:供内部适配器调用的实际执行流
|
||||
|
||||
+26
-1
@@ -2,6 +2,7 @@
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include "Export.h"
|
||||
#include "DateTime.h"
|
||||
|
||||
@@ -35,6 +36,11 @@ constexpr auto G_ERROR_PAGE = R"(
|
||||
|
||||
// inline constexpr std::string_view G_HTTP_STD_WEEK[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
|
||||
|
||||
namespace Json
|
||||
{
|
||||
class Value;
|
||||
}
|
||||
|
||||
namespace uns
|
||||
{
|
||||
enum HTTPMethod
|
||||
@@ -65,6 +71,8 @@ namespace uns
|
||||
inline constexpr std::string_view resh_acma = "Access-Control-Max-Age";
|
||||
};
|
||||
|
||||
inline constexpr auto url_all = R"(/[\s\S]*)";
|
||||
|
||||
using POSTArgs = std::map<std::string, std::string>;
|
||||
|
||||
std::string UNSWSC_DLL_EXPORT EncodeErrorPage(int code);
|
||||
@@ -93,6 +101,23 @@ namespace uns
|
||||
std::string UNSWSC_DLL_EXPORT ToLower(const std::string& s);
|
||||
|
||||
std::string UNSWSC_DLL_EXPORT CalculateFileHashSHA256(const std::string& file);
|
||||
|
||||
Json::Value UNSWSC_DLL_EXPORT SafeJsonDecode(const std::string& str);
|
||||
|
||||
template <typename T, typename CharT = char>
|
||||
std::optional<T> UNSWSC_DLL_EXPORT SafeStoX(const std::basic_string<CharT>& str, std::size_t* pos = nullptr, int base = 10);
|
||||
template <typename T, typename CharT = char>
|
||||
std::optional<T> UNSWSC_DLL_EXPORT SafeStoX(std::basic_string_view<CharT> str, std::size_t* pos = nullptr, int base = 10);
|
||||
template <typename T, typename CharT>
|
||||
inline std::optional<T> SafeStoX(const CharT* str, std::size_t* pos = nullptr, int base = 10)
|
||||
{
|
||||
#if defined(__cpp_char8_t)
|
||||
if constexpr (std::is_same_v<CharT, char8_t>)
|
||||
return SafeStoX<T>(std::basic_string<char>(reinterpret_cast<const char*>(str)), pos, base); // 只有 u8"..." (char8_t) 强制转为 char 版本的 SafeStoX 处理
|
||||
else
|
||||
#endif
|
||||
return SafeStoX<T>(std::basic_string<CharT>(str), pos, base); // 普通 "..." (char) 和 L"..." (wchar_t) 保持各自类型,构造对应的 basic_string
|
||||
}
|
||||
}
|
||||
|
||||
namespace secure
|
||||
@@ -103,7 +128,7 @@ namespace uns
|
||||
* @param requested_path 客户端传入的、解码后的目标子路径
|
||||
* @return true 安全(在沙盒内);false 不安全(企图穿越或路径非法)
|
||||
*/
|
||||
bool IsSafePath(const std::string& safe_path, const std::string& requested_path);
|
||||
bool IsSafePath(const std::string& safe_path, const std::string& requested_path);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <variant>
|
||||
#include "Export.h"
|
||||
#include <functional>
|
||||
#include <filesystem>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
|
||||
@@ -217,6 +218,8 @@ namespace uns
|
||||
else if constexpr (std::is_pointer_v<D>)
|
||||
value = static_cast<const void*>(val);
|
||||
// 7. 标准库容器(关键点:利用 Lambda 闭包在不引入 fmt 的情况下擦除容器类型!)
|
||||
else if constexpr (std::is_same<D, std::filesystem::path>::value)
|
||||
value = ConvertWStringToUtf8(val.generic_wstring());
|
||||
else if constexpr (is_container<D>::value)
|
||||
{
|
||||
value = RangeCapturer{ &val, [] (const void* p, std::vector<LogArg>& out)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Unknown Network Service Web Server Core
|
||||
* Version 1.2.2
|
||||
*
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#pragma once
|
||||
#pragma once
|
||||
#include "Global.h"
|
||||
#include "Export.h"
|
||||
#include "IPTable.h"
|
||||
@@ -23,9 +23,14 @@ public:
|
||||
void AddStreamSettings(std::string method, bool stream);
|
||||
|
||||
public:
|
||||
// 主接口,重载以实现对请求的处理
|
||||
virtual uns::ResponsePtr Processor(uns::RequestPtr request) = 0;
|
||||
// 路径穿越配置,重载以配置允许的路径穿越类型
|
||||
virtual uns::PathTraversalDefenceLevel PTDefence();
|
||||
// 路径穿越防护,重载以实现路径穿越检查,配置为AutoNormalize或AllowNormal时必须,否则自动退化为DenyAll
|
||||
virtual bool IsPathSafe(const std::string& raw_path);
|
||||
// 请求头预检,重载以实现在接收请求体之前检查请求头,返回false则服务器将强制关闭连接
|
||||
virtual bool IsHeaderValid(uns::RequestPtr request);
|
||||
|
||||
public:
|
||||
uns::ResponsePtr Handle(uns::RequestPtr request);
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
#pragma once
|
||||
#pragma once
|
||||
#include "Export.h"
|
||||
#include "Global.h"
|
||||
#include "IPTable.h"
|
||||
#include "WebFileInfo.h"
|
||||
#include "HTTPObjects.h"
|
||||
#include "TempFileManager.h"
|
||||
|
||||
using SFR_FileMap = std::map<std::string, std::string>;
|
||||
|
||||
class UNSWSC_DLL_EXPORT SyncFileReceiver
|
||||
{
|
||||
@@ -25,16 +27,22 @@ public:
|
||||
uns::HTTPMethod GetMethod(uns::RequestPtr request);
|
||||
void AppenedBlockedIP(DateTime::Span block_time, std::string ip);
|
||||
bool WriteFile(const std::string& path, const std::string& bytes);
|
||||
void SetFileTimeout(std::chrono::seconds timeout = 0s, std::chrono::seconds max_proc_timeout = 0s) noexcept;
|
||||
|
||||
public:
|
||||
// 请求信息预检,重载以在保存文件之前检查请求体
|
||||
virtual uns::Status PreCheckRequest(uns::RequestPtr request) = 0;
|
||||
// 表单预检,重载以在处理表单前检查表单
|
||||
virtual bool PreCheckForm(uns::FormPartPtr form) = 0;
|
||||
// 路径穿越配置,重载以配置允许的路径穿越类型
|
||||
virtual uns::PathTraversalDefenceLevel PTDefence();
|
||||
// 路径穿越防护,重载以实现路径穿越检查,配置为AutoNormalize或AllowNormal时必须,否则自动退化为DenyAll
|
||||
virtual bool IsPathSafe(const std::string& raw_path);
|
||||
// 请求头预检,重载以实现在接收请求体之前检查请求头,返回false则服务器将强制关闭连接
|
||||
virtual bool IsHeaderValid(uns::RequestPtr request);
|
||||
|
||||
// 【修改】由原先的 Callback 改为可供子类重写的虚函数
|
||||
// 返回值改为 webcc::ResponsePtr,并且引入 request 参数以便子类调用 AutoCORS 或解析请求头
|
||||
virtual uns::ResponsePtr ProcessFiles(const WebFileInfoVec& file_info, const std::string& tmp_root, uns::RequestPtr request);
|
||||
// 主接口,重载以接收并处理文件
|
||||
virtual uns::ResponsePtr ProcessFiles(TempFileManager& file_info, SFR_FileMap file_map, const std::string& tmp_root, uns::RequestPtr request);
|
||||
|
||||
public:
|
||||
uns::ResponsePtr Execute(uns::RequestPtr request);
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include "Export.h"
|
||||
#include "WebFileInfo.h"
|
||||
|
||||
using std::chrono::operator""s;
|
||||
using std::chrono::operator""min;
|
||||
|
||||
class UNSWSC_DLL_EXPORT TempFileManager
|
||||
{
|
||||
private:
|
||||
class Impl;
|
||||
|
||||
std::unique_ptr<Impl> pimpl;
|
||||
|
||||
public:
|
||||
TempFileManager() noexcept;
|
||||
~TempFileManager() noexcept;
|
||||
|
||||
// 禁止拷贝与移动
|
||||
TempFileManager(const TempFileManager&) = delete;
|
||||
TempFileManager& operator=(const TempFileManager&) = delete;
|
||||
TempFileManager(TempFileManager&&) = delete;
|
||||
TempFileManager& operator=(TempFileManager&&) = delete;
|
||||
|
||||
// 基础业务接口(保持不变)
|
||||
bool SetBaseDirectory(std::string dir) noexcept;
|
||||
bool RegisterFile(const WebFileInfo& info, std::chrono::seconds timeout, std::chrono::seconds max_proc_timeout = std::chrono::seconds(0)) noexcept;
|
||||
bool InvalidateFile(const WebFileInfo& info);
|
||||
//For SFR_FileMap<OriginFileName, StorageFileName>
|
||||
bool InvalidateFile(const std::map<std::string, std::string>& info);
|
||||
bool CopyFileTo(const std::string& storage_name, const std::string& dest_absolute_path) noexcept;
|
||||
bool CutFileTo(const std::string& storage_name, const std::string& dest_absolute_path) noexcept;
|
||||
bool RenameFile(const std::string& old_storage_name, const std::string& new_storage_name) noexcept;
|
||||
bool DeleteFile(const std::string& storage_name) noexcept;
|
||||
bool ActiveFile(const std::string& storage_name) noexcept;
|
||||
bool DeactiveFile(const std::string& storage_name) noexcept;
|
||||
bool SetPermanent(const std::string& storage_name, bool permanent) noexcept;
|
||||
bool FileExists(const std::string& storage_name) const noexcept;
|
||||
bool GetFileInfo(const std::string& storage_name, WebFileInfo& out_info) const noexcept;
|
||||
size_t GetFileCount() const noexcept;
|
||||
// 获取当前所有管理中的文件信息快照(线程安全,绝不抛出异常)
|
||||
std::vector<WebFileInfo> GetAllFileInfos() const noexcept;
|
||||
};
|
||||
@@ -31,6 +31,10 @@ public:
|
||||
std::string GetExtensionName() const;
|
||||
DateTime GetUploadTime() const;
|
||||
size_t GetFileSize() const;
|
||||
|
||||
public:
|
||||
void SetStorageFileName(const std::string& sfn);
|
||||
void SetOriginalFileName(const std::string& ofn);
|
||||
};
|
||||
|
||||
using WebFileInfoVec = std::vector<WebFileInfo>;
|
||||
Reference in New Issue
Block a user