| 1 |
HTTP 服务端类 |
| 2 |
|
| 3 |
```c++ |
| 4 |
|
| 5 |
// HTTP服务类 |
| 6 |
class HttpServer { |
| 7 |
|
| 8 |
// 注册HTTP业务类 |
| 9 |
void registerHttpService(HttpService* service); |
| 10 |
|
| 11 |
// 设置监听主机 |
| 12 |
void setHost(const char* host = "0.0.0.0"); |
| 13 |
// 设置监听端口 |
| 14 |
void setPort(int port = 0, int ssl_port = 0); |
| 15 |
// 设置监听文件描述符 |
| 16 |
void setListenFD(int fd = -1, int ssl_fd = -1); |
| 17 |
|
| 18 |
// 设置IO进程数 (仅`linux`下有效) |
| 19 |
void setProcessNum(int num); |
| 20 |
// 设置IO线程数 |
| 21 |
void setThreadNum(int num); |
| 22 |
|
| 23 |
// 设置SSL/TLS |
| 24 |
int setSslCtx(hssl_ctx_t ssl_ctx); |
| 25 |
// 新建SSL/TLS |
| 26 |
int newSslCtx(hssl_ctx_opt_t* opt); |
| 27 |
|
| 28 |
// hooks |
| 29 |
// 事件循环开始时执行的回调函数 |
| 30 |
std::function<void()> onWorkerStart; |
| 31 |
// 事件循环结束时执行的回调函数 |
| 32 |
std::function<void()> onWorkerStop; |
| 33 |
|
| 34 |
// 占用当前线程运行 |
| 35 |
int run(bool wait = true); |
| 36 |
|
| 37 |
// 不占用当前线程运行 |
| 38 |
int start(); |
| 39 |
|
| 40 |
// 停止服务 |
| 41 |
int stop(); |
| 42 |
|
| 43 |
}; |
| 44 |
|
| 45 |
// HTTP业务类 |
| 46 |
class HttpService { |
| 47 |
|
| 48 |
// 添加静态资源映射 |
| 49 |
void Static(const char* path, const char* dir); |
| 50 |
|
| 51 |
// 允许跨域访问 |
| 52 |
void AllowCORS(); |
| 53 |
|
| 54 |
// 添加可信代理 (代理白名单) |
| 55 |
void AddTrustProxy(const char* host); |
| 56 |
|
| 57 |
// 添加不可信代理 (代理黑名单) |
| 58 |
void AddNoProxy(const char* host); |
| 59 |
|
| 60 |
// 开启正向转发代理 |
| 61 |
void EnableForwardProxy(); |
| 62 |
|
| 63 |
// 添加反向代理映射 |
| 64 |
void Proxy(const char* path, const char* url); |
| 65 |
|
| 66 |
// 添加中间件 |
| 67 |
void Use(Handler handlerFunc); |
| 68 |
|
| 69 |
// 添加路由处理器 |
| 70 |
void Handle(const char* httpMethod, const char* relativePath, Handler handlerFunc); |
| 71 |
|
| 72 |
// 添加`HEAD`路由 |
| 73 |
void HEAD(const char* relativePath, Handler handlerFunc); |
| 74 |
|
| 75 |
// 添加`GET`路由 |
| 76 |
void GET(const char* relativePath, Handler handlerFunc); |
| 77 |
|
| 78 |
// 添加`POST`路由 |
| 79 |
void POST(const char* relativePath, Handler handlerFunc); |
| 80 |
|
| 81 |
// 添加`PUT`路由 |
| 82 |
void PUT(const char* relativePath, Handler handlerFunc); |
| 83 |
|
| 84 |
// 添加`DELETE`路由 |
| 85 |
void Delete(const char* relativePath, Handler handlerFunc); |
| 86 |
|
| 87 |
// 添加`PATCH`路由 |
| 88 |
void PATCH(const char* relativePath, Handler handlerFunc); |
| 89 |
|
| 90 |
// 添加任意`HTTP method`路由 |
| 91 |
void Any(const char* relativePath, Handler handlerFunc); |
| 92 |
|
| 93 |
// 返回注册的路由路径列表 |
| 94 |
hv::StringList Paths(); |
| 95 |
|
| 96 |
// 处理流程:前处理器 -> 中间件 -> 处理器 -> 后处理器 |
| 97 |
// preprocessor -> middleware -> processor -> postprocessor |
| 98 |
|
| 99 |
// 数据成员 |
| 100 |
http_handler preprocessor; // 前处理器 |
| 101 |
http_handlers middleware; // 中间件 |
| 102 |
http_handler processor; // 处理器 |
| 103 |
http_handler postprocessor; // 后处理器 |
| 104 |
std::string base_url; // 基本路径 |
| 105 |
std::string document_root; // 文档根目录 |
| 106 |
std::string home_page; // 主页 |
| 107 |
std::string error_page; // 默认错误页 |
| 108 |
std::string index_of; // 目录 |
| 109 |
http_handler errorHandler; // 错误处理器 |
| 110 |
|
| 111 |
int proxy_connect_timeout; // 代理连接超时 |
| 112 |
int proxy_read_timeout; // 代理读超时 |
| 113 |
int proxy_write_timeout; // 代理写超时 |
| 114 |
|
| 115 |
int keepalive_timeout; // 长连接保活超时 |
| 116 |
int max_file_cache_size; // 文件缓存最大尺寸 |
| 117 |
int file_cache_stat_interval; // 文件缓存stat间隔,查询文件是否修改 |
| 118 |
int file_cache_expired_time; // 文件缓存过期时间,过期自动释放 |
| 119 |
|
| 120 |
int limit_rate; // 下载速度限制 |
| 121 |
|
| 122 |
}; |
| 123 |
|
| 124 |
/* 几种`handler`处理函数区别说明: */ |
| 125 |
|
| 126 |
// 同步`handler`运行在IO线程 |
| 127 |
typedef std::function<int(HttpRequest* req, HttpResponse* resp)> http_sync_handler; |
| 128 |
|
| 129 |
// 异步`handler`运行在`hv::async`全局线程池,可通过`hv::async::startup`设置线程池属性 |
| 130 |
typedef std::function<void(const HttpRequestPtr& req, const HttpResponseWriterPtr& writer)> http_async_handler; |
| 131 |
|
| 132 |
// 上下文`handler`运行在IO线程,你可以很方便的将`HttpContextPtr`智能指针抛到你的消费者线程/线程池去处理 |
| 133 |
typedef std::function<int(const HttpContextPtr& ctx)> http_ctx_handler; |
| 134 |
|
| 135 |
// 中间状态`handler`运行在IO线程,用来实现大数据量的边接收边处理 |
| 136 |
typedef std::function<int(const HttpContextPtr& ctx, http_parser_state state, const char* data, size_t size)> http_state_handler; |
| 137 |
|
| 138 |
``` |
| 139 |
|
| 140 |
测试代码见 [examples/http_server_test.cpp](../../examples/http_server_test.cpp) |