NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
http_filter.hpp
浏览该文件的文档.
1#ifndef NEFORCE_NETWORK_HTTP_HTTP_FILTER_HPP__
2#define NEFORCE_NETWORK_HTTP_HTTP_FILTER_HPP__
3
21
30NEFORCE_BEGIN_NAMESPACE__
31NEFORCE_BEGIN_HTTP__
32
37
49class NEFORCE_API http_filter {
50public:
51 virtual ~http_filter() = default;
52
61 virtual void do_filter(http_request& request, http_response& response) = 0;
62
71 virtual bool pre_filter(http_request& request, http_response& response) { return true; }
72
80 virtual void post_filter(http_request& request, http_response& response) {}
81
86 NEFORCE_NODISCARD virtual string name() const { return "http_filter"; }
87};
88
95class NEFORCE_API http_filter_chain {
96private:
98 bool owns_filters_ = true;
99
100public:
101 http_filter_chain() = default;
102
107 explicit http_filter_chain(const bool owns_filters) :
108 owns_filters_(owns_filters) {}
109
110 http_filter_chain(const http_filter_chain&) = delete;
111 http_filter_chain& operator=(const http_filter_chain&) = delete;
112
113 http_filter_chain(http_filter_chain&&) noexcept = default;
114 http_filter_chain& operator=(http_filter_chain&&) noexcept = default;
115
121
127
131 void clear() noexcept;
132
137 NEFORCE_NODISCARD size_t size() const noexcept { return filters_.size(); }
138
143 NEFORCE_NODISCARD bool empty() const noexcept { return filters_.empty(); }
144
152
159
165 void execute_filters(http_request& request, http_response& response);
166};
167
175class NEFORCE_API cors_filter final : public http_filter {
176public:
179 string allowed_headers{"Content-Type, Cookie, Accept, X-Requested-With"};
180 bool allow_credentials = true;
182
183 cors_filter() = default;
184
189 explicit cors_filter(string origins) :
190 allowed_origins(_NEFORCE move(origins)) {}
191
192 bool pre_filter(http_request& request, http_response& response) override;
193 void do_filter(http_request& request, http_response& response) override {}
194 NEFORCE_NODISCARD string name() const override { return "cors_filter"; }
195};
196
203class NEFORCE_API logging_filter final : public http_filter {
204public:
205 bool log_headers = false;
206 bool log_body = false;
208
209 bool pre_filter(http_request& request, http_response& response) override;
210 void post_filter(http_request& request, http_response& response) override;
211 void do_filter(http_request& request, http_response& response) override {}
212 NEFORCE_NODISCARD string name() const override { return "logging_filter"; }
213};
214
221class NEFORCE_API static_file_filter final : public http_filter {
222private:
223 string root_path_;
225 bool enable_cache_ = true;
226 byte_size max_file_size_{10_MB};
227
228public:
233 explicit static_file_filter(string root_path);
234
235 bool pre_filter(http_request& request, http_response& response) override;
236 void do_filter(http_request& request, http_response& response) override {}
237 NEFORCE_NODISCARD string name() const override { return "static_file_filter"; }
238
246 NEFORCE_NODISCARD static bool is_safe_path(const string& path);
247
253 NEFORCE_NODISCARD optional<http_content> get_mime_type(const string& path) const;
254
260 void add_mime_type(const string& extension, http_content content_type);
261};
262
269class NEFORCE_API rate_limit_filter final : public http_filter {
270private:
271 struct rate_limit_info {
272 size_t count = 0;
273 datetime last_reset{datetime::now()};
274 };
276 mutex mutex_;
277
278public:
279 size_t max_requests = 100;
281
287 explicit rate_limit_filter(const size_t max_requests = 100, const seconds window = seconds(60)) :
289 window_seconds(window) {}
290
291 void set_max_requests(const size_t max_request) { max_requests = max_request; }
292 void set_window(const seconds window) { window_seconds = window; }
293
294 bool pre_filter(http_request& request, http_response& response) override;
295 void do_filter(http_request& request, http_response& response) override {}
296 string name() const override { return "rate_limit_filter"; }
297
304};
305
312class NEFORCE_API authentication_filter final : public http_filter {
313private:
314 vector<string> excluded_paths_;
315 function<bool(const http_request&)> auth_validator_;
316
317 bool is_path_excluded(const string& path) const;
318
319public:
320 authentication_filter() = default;
321
326 explicit authentication_filter(function<bool(const http_request&)> validator) :
327 auth_validator_(_NEFORCE move(validator)) {}
328
333 void set_auth_validator(function<bool(const http_request&)> validator) {
334 auth_validator_ = _NEFORCE move(validator);
335 }
336
341 void add_excluded_path(string path) { excluded_paths_.push_back(_NEFORCE move(path)); }
342
346 void clear_excluded_paths() { excluded_paths_.clear(); }
347
348 bool pre_filter(http_request& request, http_response& response) override;
349 void do_filter(http_request& request, http_response& response) override {}
350 NEFORCE_NODISCARD string name() const override { return "authentication_filter"; }
351};
352 // HTTP
354
355NEFORCE_END_HTTP__
356NEFORCE_END_NAMESPACE__
357#endif // NEFORCE_NETWORK_HTTP_HTTP_FILTER_HPP__
字节大小表示和转换工具
bool pre_filter(http_request &request, http_response &response) override
预处理方法
void do_filter(http_request &request, http_response &response) override
核心过滤方法
NEFORCE_NODISCARD string name() const override
获取过滤器名称
void clear_excluded_paths()
清除所有排除路径
void add_excluded_path(string path)
添加排除路径
void set_auth_validator(function< bool(const http_request &)> validator)
设置认证验证器
authentication_filter(function< bool(const http_request &)> validator)
构造函数
字节大小类
CORS跨域过滤器
string allowed_origins
允许的源
bool allow_credentials
是否允许携带凭证
void do_filter(http_request &request, http_response &response) override
核心过滤方法
string allowed_headers
允许的请求头
http_method allowed_methods
允许的方法
cors_filter(string origins)
构造函数
seconds max_age
预检结果缓存时间
bool pre_filter(http_request &request, http_response &response) override
预处理方法
NEFORCE_NODISCARD string name() const override
获取过滤器名称
日期时间类
static NEFORCE_NODISCARD datetime now() noexcept
获取当前本地时间
函数包装器主模板声明
NEFORCE_NODISCARD bool empty() const noexcept
检查过滤器链是否为空
void add_filter(unique_ptr< http_filter > filter)
添加过滤器(转移所有权)
NEFORCE_NODISCARD size_t size() const noexcept
获取过滤器数量
http_filter_chain(const bool owns_filters)
构造函数
void execute_post_filters(http_request &request, http_response &response)
执行所有后过滤器
void add_filter_ref(http_filter *filter)
添加过滤器(不转移所有权)
void execute_filters(http_request &request, http_response &response)
执行所有核心过滤器
void clear() noexcept
清空所有过滤器
bool execute_pre_filters(http_request &request, http_response &response)
执行所有预过滤器
HTTP过滤器基类
virtual void do_filter(http_request &request, http_response &response)=0
核心过滤方法
virtual NEFORCE_NODISCARD string name() const
获取过滤器名称
virtual bool pre_filter(http_request &request, http_response &response)
预处理方法
virtual void post_filter(http_request &request, http_response &response)
后处理方法
日志记录过滤器
bool log_headers
是否记录请求/响应头
byte_size max_body_log_size
最大记录正文大小
bool log_body
是否记录请求/响应体
void do_filter(http_request &request, http_response &response) override
核心过滤方法
bool pre_filter(http_request &request, http_response &response) override
预处理方法
NEFORCE_NODISCARD string name() const override
获取过滤器名称
void post_filter(http_request &request, http_response &response) override
后处理方法
非递归互斥锁
可选值类
文件路径类
size_t max_requests
窗口内最大请求数
bool pre_filter(http_request &request, http_response &response) override
预处理方法
string name() const override
获取过滤器名称
rate_limit_filter(const size_t max_requests=100, const seconds window=seconds(60))
构造函数
seconds window_seconds
时间窗口
void cleanup_old_entries()
清理过期的条目
void do_filter(http_request &request, http_response &response) override
核心过滤方法
NEFORCE_NODISCARD string name() const override
获取过滤器名称
void add_mime_type(const string &extension, http_content content_type)
添加MIME类型映射
static_file_filter(string root_path)
构造函数
static NEFORCE_NODISCARD bool is_safe_path(const string &path)
检查路径是否安全
void do_filter(http_request &request, http_response &response) override
核心过滤方法
bool pre_filter(http_request &request, http_response &response) override
预处理方法
NEFORCE_NODISCARD optional< http_content > get_mime_type(const string &path) const
获取MIME类型
独占智能指针
无序映射容器
动态大小数组容器
持续时间类型
通用函数包装器
constexpr iter_difference_t< Iterator > count(Iterator first, Iterator last, const T &value)
统计范围内等于指定值的元素数量
duration< int64_t > seconds
秒持续时间
http_server_response http_response
HTTP响应类型别名
http_server_request http_request
HTTP请求类型别名
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
HTTP服务器消息结构
互斥锁
可选值类型
HTTP内容类型定义
HTTP方法定义
static const http_method & DEFAULT()
默认方法
独占智能指针
动态大小数组容器