NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
http_filter.hpp
浏览该文件的文档.
1#ifndef NEFORCE_NETWORK_HTTP_HTTP_FILTER_HPP__
2#define NEFORCE_NETWORK_HTTP_HTTP_FILTER_HPP__
3
21
29NEFORCE_BEGIN_NAMESPACE__
30NEFORCE_BEGIN_HTTP__
31
36
48class NEFORCE_API http_filter {
49public:
50 virtual ~http_filter() = default;
51
60 virtual void do_filter(http_request& request, http_response& response) = 0;
61
70 virtual bool pre_filter(http_request& request, http_response& response) { return true; }
71
79 virtual void post_filter(http_request& request, http_response& response) {}
80
85 NEFORCE_NODISCARD virtual string name() const { return "http_filter"; }
86};
87
94class NEFORCE_API http_filter_chain {
95private:
96 struct filter_entry {
98 bool owned = true;
99 };
100 vector<filter_entry> filters_;
101
102public:
103 http_filter_chain() = default;
104 ~http_filter_chain() { clear(); }
105
106 http_filter_chain(const http_filter_chain&) = delete;
107 http_filter_chain& operator=(const http_filter_chain&) = delete;
108
109 http_filter_chain(http_filter_chain&&) noexcept = default;
110 http_filter_chain& operator=(http_filter_chain&&) noexcept = default;
111
117
123
127 void clear() noexcept;
128
133 NEFORCE_NODISCARD size_t size() const noexcept { return filters_.size(); }
134
139 NEFORCE_NODISCARD bool empty() const noexcept { return filters_.empty(); }
140
148
155
164 function<void(bool)> next);
165
174 function<void()> next);
175
181 void execute_filters(http_request& request, http_response& response);
182};
183
191class NEFORCE_API cors_filter final : public http_filter {
192public:
195 string allowed_headers{"Content-Type, Cookie, Accept, X-Requested-With"};
196 bool allow_credentials = true;
198
199 cors_filter() = default;
200
205 explicit cors_filter(string origins) :
206 allowed_origins(_NEFORCE move(origins)) {}
207
208 bool pre_filter(http_request& request, http_response& response) override;
209 void do_filter(http_request& request, http_response& response) override {}
210 NEFORCE_NODISCARD string name() const override { return "cors_filter"; }
211};
212
219class NEFORCE_API logging_filter final : public http_filter {
220public:
221 bool log_headers = false;
222 bool log_body = false;
224
225 bool pre_filter(http_request& request, http_response& response) override;
226 void post_filter(http_request& request, http_response& response) override;
227 void do_filter(http_request& request, http_response& response) override {}
228 NEFORCE_NODISCARD string name() const override { return "logging_filter"; }
229};
230
237class NEFORCE_API static_file_filter final : public http_filter {
238private:
239 string root_path_;
241 bool enable_cache_ = true;
242 bool enable_range_ = true;
243 byte_size max_file_size_{10_MB};
244 string spa_fallback_path_;
245 bool spa_fallback_enabled_ = false;
246 vector<string> spa_exclude_paths_;
247
248public:
253 explicit static_file_filter(string root_path);
254
255 bool pre_filter(http_request& request, http_response& response) override;
256 void do_filter(http_request& request, http_response& response) override {}
257 NEFORCE_NODISCARD string name() const override { return "static_file_filter"; }
258
266 NEFORCE_NODISCARD static bool is_safe_path(const string& path);
267
273 NEFORCE_NODISCARD optional<http_content> get_mime_type(const string& path) const;
274
280 void add_mime_type(const string& extension, http_content content_type);
281
290 void set_spa_fallback(string index_file);
291
296 NEFORCE_NODISCARD const string& spa_fallback() const noexcept { return spa_fallback_path_; }
297
302 NEFORCE_NODISCARD bool is_spa_fallback_enabled() const noexcept { return spa_fallback_enabled_; }
303
311 void add_spa_exclude_path(string path_prefix);
312
313 void set_enable_range(const bool enable) { enable_range_ = enable; }
314 void set_enable_cache(const bool enable) { enable_cache_ = enable; }
315 void set_max_file_size(byte_size size) { max_file_size_ = size; }
316
317 // TODO: Pre-compressed resource support — serve .gz/.br variants when client Accept-Encoding matches, avoiding on-the-fly compression
318 // TODO: Directory listing — optional HTML directory listing for paths without an index file (Apache-style auto-index)
319};
320
327class NEFORCE_API authentication_filter final : public http_filter {
328private:
329 vector<string> excluded_paths_;
330 vector<string> included_paths_;
331 function<bool(const http_request&)> auth_validator_;
332
333 NEFORCE_NODISCARD bool is_path_excluded(const string& path) const;
334 NEFORCE_NODISCARD bool is_path_protected(const string& path) const;
335
336public:
337 authentication_filter() = default;
338
343 explicit authentication_filter(function<bool(const http_request&)> validator) :
344 auth_validator_(_NEFORCE move(validator)) {}
345
350 void set_auth_validator(function<bool(const http_request&)> validator) {
351 auth_validator_ = _NEFORCE move(validator);
352 }
353
360 void add_excluded_path(string path) { excluded_paths_.push_back(_NEFORCE move(path)); }
361
368 void add_included_path(string path) { included_paths_.push_back(_NEFORCE move(path)); }
369
373 void clear_excluded_paths() { excluded_paths_.clear(); }
374
378 void clear_included_paths() { included_paths_.clear(); }
379
380 bool pre_filter(http_request& request, http_response& response) override;
381 void do_filter(http_request& request, http_response& response) override {}
382 NEFORCE_NODISCARD string name() const override { return "authentication_filter"; }
383
384 // TODO: Multi-scheme auth support — support Basic (base64 user:pass), Bearer (token), Digest (RFC 7616) in addition to custom validator
385 // TODO: Auth challenge response — set WWW-Authenticate header with realm and scopes on 401 responses
386};
387
388// TODO: Request validation filter — declarative field validation (required, min/max length, regex pattern, numeric range) with JSON error response
389// TODO: Server-side templating filter — render dynamic HTML using template engines (Jinja2-like, Mustache) with data injection from request context
390 // HTTP
392
393NEFORCE_END_HTTP__
394NEFORCE_END_NAMESPACE__
395#endif // NEFORCE_NETWORK_HTTP_HTTP_FILTER_HPP__
字节大小表示和转换工具
函数包装器主模板声明
void add_included_path(string path)
添加受保护路径(要求认证)
void set_auth_validator(function< bool(const http_request &)> validator)
设置认证验证器
authentication_filter(function< bool(const http_request &)> validator)
构造函数
string name() const override
获取过滤器名称
void clear_excluded_paths()
清除所有排除路径
bool pre_filter(http_request &request, http_response &response) override
预处理方法
void clear_included_paths()
清除所有包含路径
void add_excluded_path(string path)
添加排除路径(不要求认证)
void do_filter(http_request &request, http_response &response) override
核心过滤方法
string name() const override
获取过滤器名称
cors_filter(string origins)
构造函数
string allowed_headers
允许的请求头
bool pre_filter(http_request &request, http_response &response) override
预处理方法
seconds max_age
预检结果缓存时间
bool allow_credentials
是否允许携带凭证
void do_filter(http_request &request, http_response &response) override
核心过滤方法
http_method allowed_methods
允许的方法
bool execute_pre_filters(http_request &request, http_response &response)
执行所有预过滤器
void execute_post_filters_async(http_request &request, http_response &response, http_context &ctx, function< void()> next)
异步执行所有后过滤器(回调链模式)
void execute_filters(http_request &request, http_response &response)
执行所有核心过滤器
size_t size() const noexcept
获取过滤器数量
void add_filter(unique_ptr< http_filter > filter)
添加过滤器(转移所有权)
void add_filter_ref(http_filter *filter)
添加过滤器(不转移所有权)
bool empty() const noexcept
检查过滤器链是否为空
void execute_pre_filters_async(http_request &request, http_response &response, http_context &ctx, function< void(bool)> next)
异步执行所有预过滤器(回调链模式)
void clear() noexcept
清空所有过滤器
void execute_post_filters(http_request &request, http_response &response)
执行所有后过滤器
virtual void post_filter(http_request &request, http_response &response)
后处理方法
virtual string name() const
获取过滤器名称
virtual void do_filter(http_request &request, http_response &response)=0
核心过滤方法
virtual bool pre_filter(http_request &request, http_response &response)
预处理方法
string name() const override
获取过滤器名称
void post_filter(http_request &request, http_response &response) override
后处理方法
bool log_headers
是否记录请求/响应头
bool log_body
是否记录请求/响应体
bool pre_filter(http_request &request, http_response &response) override
预处理方法
void do_filter(http_request &request, http_response &response) override
核心过滤方法
byte_size max_body_log_size
最大记录正文大小
void add_spa_exclude_path(string path_prefix)
添加SPA回退排除路径
bool pre_filter(http_request &request, http_response &response) override
预处理方法
optional< http_content > get_mime_type(const string &path) const
获取MIME类型
static bool is_safe_path(const string &path)
检查路径是否安全
string name() const override
获取过滤器名称
static_file_filter(string root_path)
构造函数
const string & spa_fallback() const noexcept
查询SPA回退文件
void set_spa_fallback(string index_file)
启用SPA回退模式
void add_mime_type(const string &extension, http_content content_type)
添加MIME类型映射
bool is_spa_fallback_enabled() const noexcept
查询SPA回退是否启用
void do_filter(http_request &request, http_response &response) override
核心过滤方法
文件路径类
独占智能指针
动态大小数组容器
持续时间类型
通用函数包装器
duration< int64_t > seconds
秒持续时间
http_server_response http_response
HTTP响应类型别名
http_server_request http_request
HTTP请求类型别名
constexpr Iterator next(Iterator iter, iter_difference_t< Iterator > n=1)
获取迭代器的后一个位置
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
constexpr decltype(auto) size(const Container &cont) noexcept(noexcept(cont.size()))
获取容器的大小
HTTP服务器消息结构
unordered_map< string, any > http_context
请求上下文,跨过滤器/中间件传递任意数据
互斥锁
可选值类型
static const http_method & DEFAULT()
默认方法
独占智能指针