diff --git a/src_252Fserver/WFHttpServer.h b/src_252Fserver/WFHttpServer.h new file mode 100644 index 0000000..940a708 --- /dev/null +++ b/src_252Fserver/WFHttpServer.h @@ -0,0 +1,71 @@ +/* + Copyright (c) 2019 Sogou, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + Authors: Xie Han (xiehan@sogou-inc.com) +*/ + +#ifndef _WFHTTPSERVER_H_ +#define _WFHTTPSERVER_H_ + +#include // 引入标准库中的工具函数 +#include "HttpMessage.h" // 引入 HTTP 消息相关的头文件 +#include "WFServer.h" // 引入服务器功能的头文件 +#include "WFTaskFactory.h" // 引入任务工厂以创建 HTTP 任务 + +// 定义一个 http_process_t 类型,它是一个接受 WFHttpTask 指针并返回 void 的函数对象 +using http_process_t = std::function; + +// 定义 WFHttpServer 类型,基于 WFServer 模板类,使用 HttpRequest 和 HttpResponse 作为协议 +using WFHttpServer = WFServer; + +// 设置 HTTP 服务器的默认参数 +static constexpr struct WFServerParams HTTP_SERVER_PARAMS_DEFAULT = +{ + .transport_type = TT_TCP, // 传输类型为 TCP + .max_connections = 2000, // 最大连接数 + .peer_response_timeout = 10 * 1000, // 对端响应超时时间 + .receive_timeout = -1, // 接收超时时间,-1 表示不超时 + .keep_alive_timeout = 60 * 1000, // 保持连接的超时时间 + .request_size_limit = (size_t)-1, // 请求大小限制,-1 表示无限制 + .ssl_accept_timeout = 10 * 1000, // SSL 接受超时时间 +}; + +// WFHttpServer 构造函数的特化模板 +template<> inline +WFHttpServer::WFServer(http_process_t proc) : + WFServerBase(&HTTP_SERVER_PARAMS_DEFAULT), // 调用基类构造函数,传入默认参数 + process(std::move(proc)) // 移动构造函数以提高性能 +{ +} + +// 创建新的会话,重写基类的方法 +template<> inline +CommSession *WFHttpServer::new_session(long long seq, CommConnection *conn) +{ + WFHttpTask *task; // 声明一个 HTTP 任务指针 + + // 使用任务工厂创建一个新的 HTTP 任务 + task = WFServerTaskFactory::create_http_task(this, this->process); + // 设置保持连接的超时时间 + task->set_keep_alive(this->params.keep_alive_timeout); + // 设置接收超时时间 + task->set_receive_timeout(this->params.receive_timeout); + // 设置请求大小限制 + task->get_req()->set_size_limit(this->params.request_size_limit); + + return task; // 返回创建的任务 +} + +#endif // _WFHTTPSERVER_H_