NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
http_router.hpp
浏览该文件的文档.
1#ifndef NEFORCE_NETWORK_HTTP_HTTP_ROUTER_HPP__
2#define NEFORCE_NETWORK_HTTP_HTTP_ROUTER_HPP__
3
11
15NEFORCE_BEGIN_NAMESPACE__
16NEFORCE_BEGIN_HTTP__
17
22
78class NEFORCE_API http_router {
79public:
82
83private:
84 struct route_entry {
85 string pattern;
86 http_handler_t handler;
87 optional<regex> regex_pattern;
88 vector<string> param_names;
89 bool is_regex = false;
90 };
91
94 http_filter_chain middleware_chain_;
95
96 http_handler_t not_found_handler_;
97 http_handler_t method_not_allowed_handler_;
98 exception_handler_t exception_handler_;
99
100public:
101 bool case_sensitive = true;
102 bool strict_routing = false;
103
104 // TODO: Content negotiation — auto-select handler based on request Accept header, support produces/consumes qualifiers
105 // TODO: Route grouping / prefix — support route group with shared path prefix (e.g., router.group("/api/v1", ...))
106 // TODO: Route ordering / priority — allow explicit priority for route matching when multiple patterns overlap
107
108private:
109 NEFORCE_NODISCARD http_handler_t find_handler(const http_method& method, const string& path, http_request& request);
110 void resolve_handler(http_request& request, http_response& response);
111
112 void setup_default_handlers();
113
114public:
121
122 ~http_router() = default;
123
124 http_router(const http_router&) = delete;
125 http_router& operator=(const http_router&) = delete;
126 http_router(http_router&&) noexcept = default;
127 http_router& operator=(http_router&&) noexcept = default;
128
129 void get(const string& path, http_handler_t handler);
130 void post(const string& path, http_handler_t handler);
131 void put(const string& path, http_handler_t handler);
132 void del(const string& path, http_handler_t handler);
133 void head(const string& path, http_handler_t handler);
134 void options(const string& path, http_handler_t handler);
135 void trace(const string& path, http_handler_t handler);
136 void connect(const string& path, http_handler_t handler);
137 void patch(const string& path, http_handler_t handler);
138
144 void get_post(const string& path, http_handler_t handler);
145
151 void post_delete(const string& path, http_handler_t handler);
152
158 void all(const string& path, http_handler_t handler);
159
171 void route(const http_method& method, const string& path, const http_handler_t& handler);
172
177 void use(unique_ptr<http_filter> middleware) { middleware_chain_.add_filter(_NEFORCE move(middleware)); }
178
183 http_filter_chain& middleware_chain() noexcept { return middleware_chain_; }
184
189 void set_not_found_handler(http_handler_t handler) { not_found_handler_ = _NEFORCE move(handler); }
190
196 method_not_allowed_handler_ = _NEFORCE move(handler);
197 }
198
203 void set_exception_handler(exception_handler_t handler) { exception_handler_ = _NEFORCE move(handler); }
204
219
236
243 NEFORCE_NODISCARD bool has_route(const http_method& method, const string& path) const;
244
249 NEFORCE_NODISCARD size_t route_count() const noexcept;
250
254 void clear_routes() noexcept {
255 routes_.clear();
256 tries_.clear();
257 }
258};
259 // HTTP
261
262NEFORCE_END_HTTP__
263NEFORCE_END_NAMESPACE__
264#endif // NEFORCE_NETWORK_HTTP_HTTP_ROUTER_HPP__
函数包装器主模板声明
void handle_request_async(http_request request, function< void(http_response)> cb)
异步处理HTTP请求(回调模式)
void post(const string &path, http_handler_t handler)
POST请求
void set_exception_handler(exception_handler_t handler)
设置异常处理器
size_t route_count() const noexcept
获取路由总数
void set_method_not_allowed_handler(http_handler_t handler)
设置405处理器
void head(const string &path, http_handler_t handler)
HEAD请求
void clear_routes() noexcept
清空所有路由
void post_delete(const string &path, http_handler_t handler)
同时注册POST和DELETE
void get(const string &path, http_handler_t handler)
GET请求
bool strict_routing
是否严格匹配尾部斜杠
function< void(http_request &, http_response &, const exception &)> exception_handler_t
异常处理器类型
function< void(http_request &, http_response &)> http_handler_t
HTTP处理器类型
void all(const string &path, http_handler_t handler)
注册所有HTTP方法
void patch(const string &path, http_handler_t handler)
PATCH请求
http_response handle_request(http_request &request)
处理HTTP请求
bool has_route(const http_method &method, const string &path) const
检查是否存在指定路由
void put(const string &path, http_handler_t handler)
PUT请求
void connect(const string &path, http_handler_t handler)
CONNECT请求
void del(const string &path, http_handler_t handler)
DELETE请求
http_filter_chain & middleware_chain() noexcept
获取中间件链引用
void trace(const string &path, http_handler_t handler)
TRACE请求
void set_not_found_handler(http_handler_t handler)
设置404处理器
void route(const http_method &method, const string &path, const http_handler_t &handler)
通用路由注册
bool case_sensitive
是否大小写敏感
void options(const string &path, http_handler_t handler)
OPTIONS请求
void use(unique_ptr< http_filter > middleware)
添加中间件
void get_post(const string &path, http_handler_t handler)
同时注册GET和POST
文件路径类
独占智能指针
动态大小数组容器
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过滤器链实现
压缩前缀树(段式Trie)路由匹配
正则表达式引擎封装