1#ifndef NEFORCE_NETWORK_HTTP_HTTP_FILTER_HPP__
2#define NEFORCE_NETWORK_HTTP_HTTP_FILTER_HPP__
29NEFORCE_BEGIN_NAMESPACE__
85 NEFORCE_NODISCARD
virtual string name()
const {
return "http_filter"; }
94class NEFORCE_API http_filter_chain {
103 http_filter_chain() =
default;
104 ~http_filter_chain() {
clear(); }
106 http_filter_chain(
const http_filter_chain&) =
delete;
107 http_filter_chain& operator=(
const http_filter_chain&) =
delete;
109 http_filter_chain(http_filter_chain&&)
noexcept =
default;
110 http_filter_chain& operator=(http_filter_chain&&)
noexcept =
default;
133 NEFORCE_NODISCARD
size_t size() const noexcept {
return filters_.size(); }
139 NEFORCE_NODISCARD
bool empty() const noexcept {
return filters_.empty(); }
210 NEFORCE_NODISCARD
string name()
const override {
return "cors_filter"; }
228 NEFORCE_NODISCARD
string name()
const override {
return "logging_filter"; }
241 bool enable_cache_ =
true;
242 bool enable_range_ =
true;
244 string spa_fallback_path_;
245 bool spa_fallback_enabled_ =
false;
257 NEFORCE_NODISCARD
string name()
const override {
return "static_file_filter"; }
296 NEFORCE_NODISCARD
const string&
spa_fallback() const noexcept {
return spa_fallback_path_; }
313 void set_enable_range(
const bool enable) { enable_range_ = enable; }
314 void set_enable_cache(
const bool enable) { enable_cache_ = enable; }
327class NEFORCE_API authentication_filter final :
public http_filter {
333 NEFORCE_NODISCARD
bool is_path_excluded(
const string&
path)
const;
334 NEFORCE_NODISCARD
bool is_path_protected(
const string&
path)
const;
337 authentication_filter() =
default;
344 auth_validator_(_NEFORCE
move(validator)) {}
351 auth_validator_ = _NEFORCE
move(validator);
382 NEFORCE_NODISCARD
string name()
const override {
return "authentication_filter"; }
394NEFORCE_END_NAMESPACE__
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
预处理方法
string allowed_origins
允许的源
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 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()))
获取容器的大小
unordered_map< string, any > http_context
请求上下文,跨过滤器/中间件传递任意数据
static const http_method & DEFAULT()
默认方法