点击查看代码
void process_request()
{
response_.version(request_.version());
response_.keep_alive(false);
switch (request_.method())
{
case http::verb::get:
response_.result(http::status::ok);
response_.set(http::field::server, "Beast");
create_response();
break;
case http::verb::post:
response_.result(http::status::ok);
response_.set(http::field::server, "Beast");
create_post_response();
break;
default:
response_.result(http::status::bad_request);
response_.set(http::field::content_type, "text/plain");
beast::ostream(response_.body())
<< "Invalid request-method '"
<< std::string(request_.method_string())
<< "'";
break;
}
write_response();
}
1. 初始化响应
cpp
response_.version(request_.version());
response_.keep_alive(false);
设置响应版本:将响应的 HTTP 版本设置为与请求相同的版本。
禁用 Keep-Alive:将 keep_alive 设置为 false,表示此次请求不会保持连接,客户端在响应后需要关闭连接。
2. 处理请求方法
接下来,通过 switch 语句处理不同的 HTTP 请求方法(GET、POST 等):
cpp
switch (request_.method())
{
3. 处理 GET 请求
cpp
case http::verb::get:
response_.result(http::status::ok);
response_.set(http::field::server, "Beast");
create_response();
break;
状态码:将响应状态设置为 200 OK,表示请求成功。
服务器信息:这里将响应头的 Server 字段设置为 "Beast",表示使用的是 Beast HTTP 库。
创建响应:调用 create_response() 函数来填充响应内容。
4. 处理 POST 请求
cpp
case http::verb::post:
response_.result(http::status::ok);
response_.set(http::field::server, "Beast");
create_post_response();
break;
类似于 GET 请求,设置状态为 200 OK。并调用 create_post_response() 来处理 POST 请求相关的逻辑。
5. 处理不支持的请求方法
cpp
default:
response_.result(http::status::bad_request);
response_.set(http::field::content_type, "text/plain");
beast::ostream(response_.body())
<< "Invalid request-method '"
<< std::string(request_.method_string())
<< "'";
break;
如果请求的方法不是 GET 或 POST,响应状态设置为 400 Bad Request,表示请求有误。
将响应头的 Content-Type 字段设置为 text/plain,表示返回的内容是纯文本。
在响应体中写入一条消息,指出请求的方法无效,使用 request_.method_string() 获取请求方法的字符串表示。
6. 写入响应
cpp
write_response();
最后,调用 write_response() 函数将已构建的响应发送给客户端。
总结
这个 process_request 函数是一个基本的 HTTP 请求处理逻辑,能够根据请求的方法(GET、POST或者其他)生成相应的响应并发回给客户端。在处理未知请求方法时,它会返回一个错误响应。该函数体现了如何使用 Beast 库来处理 HTTP 协议的基本知识。
点击查看代码
void create_response()
{
if (request_.target() == "/count")
{
response_.set(http::field::content_type, "text/html");
beast::ostream(response_.body())
<< "<html>\n"
<< "<head><title>Request count</title></head>\n"
<< "<body>\n"
<< "<h1>Request count</h1>\n"
<< "<p>There have been "
<< my_program_state::request_count()
<< " requests so far.</p>\n"
<< "</body>\n"
<< "</html>\n";
}
else if (request_.target() == "/time")
{
response_.set(http::field::content_type, "text/html");
beast::ostream(response_.body())
<< "<html>\n"
<< "<head><title>Current time</title></head>\n"
<< "<body>\n"
<< "<h1>Current time</h1>\n"
<< "<p>The current time is "
<< my_program_state::now()
<< " seconds since the epoch.</p>\n"
<< "</body>\n"
<< "</html>\n";
}
else
{
response_.result(http::status::not_found);
response_.set(http::field::content_type, "text/plain");
beast::ostream(response_.body()) << "File not found\r\n";
}
}
功能解释
目标检测:
if (request_.target() == "/count"):
检查请求的目标 URI 是否为 /count。如果是,执行相关逻辑以生成计数响应。
else if (request_.target() == "/time"):
如果请求的是 /time,则生成当前时间的响应。
else:
如果都不是,返回一个 404 Not Found 的响应。
生成 /count 响应:
使用 response_.set(http::field::content_type, "text/html") 设置响应类型为 HTML。
使用 beast::ostream(response_.body()) 将 HTML 内容写入响应体,包括标题、请求计数信息(通过 my_program_state::request_count() 获取)。
生成 /time 响应:
同样设置 Content-Type 为 HTML。
将当前时间写入响应体,这里用 my_program_state::now() 来获取当前时间(单位为自纪元以来的秒数)。
处理未找到的请求:
在 else 分支中,响应设置为 http::status::not_found,表示请求的资源不存在。
Content-Type 设置为 text/plain,使得客户端知道这是纯文本格式。
响应体内容为简单的 "File not found" 字符串,加上 CRLF (\r\n) 来结束响应。
总结
create_response() 函数根据不同的请求路径生成相应的 HTTP 响应:
对于 /count,返回请求计数的 HTML 页面。
对于 /time,返回当前时间的 HTML 页面。
对于其他任何请求,返回 404 错误,提示“文件未找到”。
这个函数展示了如何在服务器端处理不同的请求,并生成适当的响应,以便与客户端进行有效的通信。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示