HTTP路由器类
更多...
#include <http_router.hpp>
|
| | http_router () |
| | 构造函数
|
|
void | get (const string &path, http_handler_t handler) |
| | GET请求
|
|
void | post (const string &path, http_handler_t handler) |
| | POST请求
|
|
void | put (const string &path, http_handler_t handler) |
| | PUT请求
|
|
void | del (const string &path, http_handler_t handler) |
| | DELETE请求
|
|
void | head (const string &path, http_handler_t handler) |
| | HEAD请求
|
|
void | options (const string &path, http_handler_t handler) |
| | OPTIONS请求
|
|
void | trace (const string &path, http_handler_t handler) |
| | TRACE请求
|
|
void | connect (const string &path, http_handler_t handler) |
| | CONNECT请求
|
|
void | patch (const string &path, http_handler_t handler) |
| | PATCH请求
|
| void | get_post (const string &path, http_handler_t handler) |
| | 同时注册GET和POST
|
| void | post_delete (const string &path, http_handler_t handler) |
| | 同时注册POST和DELETE
|
| void | all (const string &path, http_handler_t handler) |
| | 注册所有HTTP方法
|
| void | route (const http_method &method, const string &path, const http_handler_t &handler) |
| | 通用路由注册
|
| void | use (unique_ptr< http_filter > middleware) |
| | 添加中间件
|
| http_filter_chain & | middleware_chain () noexcept |
| | 获取中间件链引用
|
| void | set_not_found_handler (http_handler_t handler) |
| | 设置404处理器
|
| void | set_method_not_allowed_handler (http_handler_t handler) |
| | 设置405处理器
|
| void | set_exception_handler (exception_handler_t handler) |
| | 设置异常处理器
|
| http_response | handle_request (http_request &request) |
| | 处理HTTP请求
|
| NEFORCE_NODISCARD bool | has_route (const http_method &method, const string &path) const |
| | 检查是否存在指定路由
|
| NEFORCE_NODISCARD size_t | route_count () const noexcept |
| | 获取路由总数
|
|
void | clear_routes () noexcept |
| | 清空所有路由
|
|
|
bool | case_sensitive = true |
| | 是否大小写敏感
|
|
bool | strict_routing = false |
| | 是否严格匹配尾部斜杠
|
HTTP路由器类
提供HTTP请求路由功能,将请求分发到对应的处理函数。 支持路径参数、正则表达式匹配和中间件链。
主要功能:
- RESTful风格路由注册
- 路径参数提取
- 通配符匹配
- 正则表达式路由支持
- 中间件链支持
- 自定义异常处理器
- 大小写敏感/不敏感路由
使用示例:
res.
body =
"Hello World";
});
});
});
});
res.
body =
"Custom 404 Page";
});
void get(const string &path, http_handler_t handler)
GET请求
void set_not_found_handler(http_handler_t handler)
设置404处理器
http_response handle_request(http_request &request)
处理HTTP请求
void route(const http_method &method, const string &path, const http_handler_t &handler)
通用路由注册
void use(unique_ptr< http_filter > middleware)
添加中间件
http_server_response http_response
HTTP响应类型别名
http_server_request http_request
HTTP请求类型别名
NEFORCE_ALWAYS_INLINE_INLINE thread::id id() noexcept
获取当前线程标识符
NEFORCE_CONSTEXPR20 unique_ptr< T > make_unique(Args &&... args)
创建unique_ptr
NEFORCE_NODISCARD string_view parameter(const string &name) const noexcept
获取参数值
在文件 http_router.hpp 第 77 行定义.
◆ http_router()
| http_router::http_router |
( |
| ) |
|
◆ all()
◆ get_post()
◆ handle_request()
处理HTTP请求
- 参数
-
- 返回
- HTTP响应对象
处理流程:
- 执行预过滤器
- 执行核心过滤器
- 查找匹配的路由处理器
- 执行处理器
- 执行后过滤器
- 返回响应
◆ has_route()
| NEFORCE_NODISCARD bool http_router::has_route |
( |
const http_method & | method, |
|
|
const string & | path ) const |
◆ middleware_chain()
◆ post_delete()
◆ route()
通用路由注册
- 参数
-
| method | HTTP方法 |
| path | 路由路径 |
| handler | 处理器 |
路由模式语法:
- /users/:id - 路径参数,匹配/users/123,提取id=123
- /files/* - 通配符,匹配/files/a/b/c,提取*=a/b/c
- /user/([0-9]+) - 正则表达式,匹配数字ID
引用了 route().
被这些函数引用 route().
◆ route_count()
| NEFORCE_NODISCARD size_t http_router::route_count |
( |
| ) |
const |
|
noexcept |
◆ set_exception_handler()
◆ set_method_not_allowed_handler()
| void http_router::set_method_not_allowed_handler |
( |
http_handler_t | handler | ) |
|
|
inline |
◆ set_not_found_handler()
◆ use()
该类的文档由以下文件生成: