NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
http_router.hpp
浏览该文件的文档.
1#ifndef NEFORCE_NETWORK_HTTP_HTTP_ROUTER_HPP__
2#define NEFORCE_NETWORK_HTTP_HTTP_ROUTER_HPP__
3
11
14NEFORCE_BEGIN_NAMESPACE__
15NEFORCE_BEGIN_HTTP__
16
21
77class NEFORCE_API http_router {
78public:
81
82private:
83 struct route_entry {
84 string pattern;
85 http_handler_t handler;
86 optional<regex> regex_pattern;
87 vector<string> param_names;
88 bool is_regex = false;
89 };
90
92 http_filter_chain middleware_chain_;
93
94 http_handler_t not_found_handler_;
95 http_handler_t method_not_allowed_handler_;
96 exception_handler_t exception_handler_;
97
98public:
99 bool case_sensitive = true;
100 bool strict_routing = false;
101
102private:
103 route_entry* find_handler(const http_method& method, const string& path, http_request& request);
104
105 void setup_default_handlers();
106
107public:
114
115 ~http_router() = default;
116
117 http_router(const http_router&) = delete;
118 http_router& operator=(const http_router&) = delete;
119 http_router(http_router&&) noexcept = default;
120 http_router& operator=(http_router&&) noexcept = default;
121
122 void get(const string& path, http_handler_t handler);
123 void post(const string& path, http_handler_t handler);
124 void put(const string& path, http_handler_t handler);
125 void del(const string& path, http_handler_t handler);
126 void head(const string& path, http_handler_t handler);
127 void options(const string& path, http_handler_t handler);
128 void trace(const string& path, http_handler_t handler);
129 void connect(const string& path, http_handler_t handler);
130 void patch(const string& path, http_handler_t handler);
131
137 void get_post(const string& path, http_handler_t handler);
138
144 void post_delete(const string& path, http_handler_t handler);
145
151 void all(const string& path, http_handler_t handler);
152
164 void route(const http_method& method, const string& path, const http_handler_t& handler);
165
170 void use(unique_ptr<http_filter> middleware) { middleware_chain_.add_filter(_NEFORCE move(middleware)); }
171
176 http_filter_chain& middleware_chain() noexcept { return middleware_chain_; }
177
182 void set_not_found_handler(http_handler_t handler) { not_found_handler_ = _NEFORCE move(handler); }
183
189 method_not_allowed_handler_ = _NEFORCE move(handler);
190 }
191
196 void set_exception_handler(exception_handler_t handler) { exception_handler_ = _NEFORCE move(handler); }
197
212
219 NEFORCE_NODISCARD bool has_route(const http_method& method, const string& path) const;
220
225 NEFORCE_NODISCARD size_t route_count() const noexcept;
226
230 void clear_routes() noexcept { routes_.clear(); }
231};
232 // HTTP
234
235NEFORCE_END_HTTP__
236NEFORCE_END_NAMESPACE__
237#endif // NEFORCE_NETWORK_HTTP_HTTP_ROUTER_HPP__
函数包装器主模板声明
HTTP过滤器基类
void post_delete(const string &path, http_handler_t handler)
同时注册POST和DELETE
bool strict_routing
是否严格匹配尾部斜杠
NEFORCE_NODISCARD size_t route_count() const noexcept
获取路由总数
void del(const string &path, http_handler_t handler)
DELETE请求
void get(const string &path, http_handler_t handler)
GET请求
void options(const string &path, http_handler_t handler)
OPTIONS请求
void trace(const string &path, http_handler_t handler)
TRACE请求
void set_not_found_handler(http_handler_t handler)
设置404处理器
void post(const string &path, http_handler_t handler)
POST请求
NEFORCE_NODISCARD bool has_route(const http_method &method, const string &path) const
检查是否存在指定路由
function< void(http_request &, http_response &)> http_handler_t
HTTP处理器类型
function< void(http_request &, http_response &, const exception &)> exception_handler_t
异常处理器类型
void set_exception_handler(exception_handler_t handler)
设置异常处理器
void head(const string &path, http_handler_t handler)
HEAD请求
http_response handle_request(http_request &request)
处理HTTP请求
bool case_sensitive
是否大小写敏感
void connect(const string &path, http_handler_t handler)
CONNECT请求
void clear_routes() noexcept
清空所有路由
void get_post(const string &path, http_handler_t handler)
同时注册GET和POST
void set_method_not_allowed_handler(http_handler_t handler)
设置405处理器
void put(const string &path, http_handler_t handler)
PUT请求
void all(const string &path, http_handler_t handler)
注册所有HTTP方法
http_router()
构造函数
void route(const http_method &method, const string &path, const http_handler_t &handler)
通用路由注册
void patch(const string &path, http_handler_t handler)
PATCH请求
http_filter_chain & middleware_chain() noexcept
获取中间件链引用
void use(unique_ptr< http_filter > middleware)
添加中间件
可选值类
文件路径类
独占智能指针
无序映射容器
动态大小数组容器
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方法定义