NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
http_router类 参考

HTTP路由器类 更多...

#include <http_router.hpp>

Public 类型

using http_handler_t = function<void(http_request&, http_response&)>
 HTTP处理器类型
using exception_handler_t = function<void(http_request&, http_response&, const exception&)>
 异常处理器类型

Public 成员函数

 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_chainmiddleware_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
 清空所有路由

Public 属性

bool case_sensitive = true
 是否大小写敏感
bool strict_routing = false
 是否严格匹配尾部斜杠

详细描述

HTTP路由器类

提供HTTP请求路由功能,将请求分发到对应的处理函数。 支持路径参数、正则表达式匹配和中间件链。

主要功能:

  • RESTful风格路由注册
  • 路径参数提取
  • 通配符匹配
  • 正则表达式路由支持
  • 中间件链支持
  • 自定义异常处理器
  • 大小写敏感/不敏感路由

使用示例:

http_router router;
// 静态路由
router.get("/", [](http_request& req, http_response& res) {
res.body = "Hello World";
});
// 路径参数
router.get("/users/:id", [](http_request& req, http_response& res) {
string id = req.parameter("id");
res.body = "User ID: " + id;
});
// 通配符
router.get("/files/*", [](http_request& req, http_response& res) {
string path = req.parameter("*");
// 处理文件请求
});
// 正则表达式路由
router.route("GET", "/user/([0-9]+)", [](http_request& req, http_response& res) {
string id = req.parameter("0");
});
// 添加中间件
// 自定义404处理器
res.body = "Custom 404 Page";
});
// 处理请求
auto response = router.handle_request(request);
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请求
http_router()
构造函数
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.hpp77 行定义.

构造及析构函数说明

◆ http_router()

http_router::http_router ( )

构造函数

初始化默认的404、405和异常处理器。

引用了 http_router().

被这些函数引用 http_router().

成员函数说明

◆ all()

void http_router::all ( const string & path,
http_handler_t handler )

注册所有HTTP方法

参数
path路由路径
handler处理器

引用了 all().

被这些函数引用 all().

◆ get_post()

void http_router::get_post ( const string & path,
http_handler_t handler )

同时注册GET和POST

参数
path路由路径
handler处理器

引用了 get_post().

被这些函数引用 get_post().

◆ handle_request()

http_response http_router::handle_request ( http_request & request)

处理HTTP请求

参数
requestHTTP请求对象
返回
HTTP响应对象

处理流程:

  1. 执行预过滤器
  2. 执行核心过滤器
  3. 查找匹配的路由处理器
  4. 执行处理器
  5. 执行后过滤器
  6. 返回响应

◆ has_route()

NEFORCE_NODISCARD bool http_router::has_route ( const http_method & method,
const string & path ) const

检查是否存在指定路由

参数
methodHTTP方法
path路由路径
返回
存在返回true

◆ middleware_chain()

http_filter_chain & http_router::middleware_chain ( )
inlinenoexcept

获取中间件链引用

返回
中间件链引用

在文件 http_router.hpp176 行定义.

◆ post_delete()

void http_router::post_delete ( const string & path,
http_handler_t handler )

同时注册POST和DELETE

参数
path路由路径
handler处理器

引用了 post_delete().

被这些函数引用 post_delete().

◆ route()

void http_router::route ( const http_method & method,
const string & path,
const http_handler_t & handler )

通用路由注册

参数
methodHTTP方法
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()

void http_router::set_exception_handler ( exception_handler_t handler)
inline

设置异常处理器

参数
handler处理器

在文件 http_router.hpp196 行定义.

引用了 move().

◆ set_method_not_allowed_handler()

void http_router::set_method_not_allowed_handler ( http_handler_t handler)
inline

设置405处理器

参数
handler处理器

在文件 http_router.hpp188 行定义.

引用了 move().

◆ set_not_found_handler()

void http_router::set_not_found_handler ( http_handler_t handler)
inline

设置404处理器

参数
handler处理器

在文件 http_router.hpp182 行定义.

引用了 move().

◆ use()

void http_router::use ( unique_ptr< http_filter > middleware)
inline

添加中间件

参数
middleware中间件对象(转移所有权)

在文件 http_router.hpp170 行定义.

引用了 move() , 以及 use().

被这些函数引用 use().


该类的文档由以下文件生成: