Ai
1 Star 0 Fork 0

Aurora/Linux

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
HttpServer.hpp 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
Aurora 提交于 2025-07-10 17:26 +08:00 . http version2
#pragma once
#include <iostream>
#include <string>
#include "TcpServer.hpp"
using namespace TcpServerModule;
const std::string Sep = "\r\n";
const std::string BlankLine = Sep;
class HttpServer
{
public:
HttpServer(int port)
: _tsvr(std::make_unique<TcpServer>(port))
{
}
void Start()
{
_tsvr->InitServer([this](SockPtr sockfd, InetAddr client)
{ return this->HandlerRequest(sockfd, client); });
_tsvr->Loop();
}
// 就是我们处理http的入口
bool HandlerRequest(SockPtr sockfd, InetAddr client)
{
LOG(LogLevel::DEBUG) << "HttPserver: get a new client: " << sockfd->Fd() << "addr info:" << client.Addr();
std::string http_request;
sockfd->Recv(&http_request);
std::cout << http_request;
// 读取请求,对请求进行分析处理 --> 文本处理!
// 1.读取请求的完整性 --- 暂时不做
// 2.完整http反序列化http responce序列化...
// demo 1:直接返回一个固定的内容
std::string status_line = "HTTP/1.1 200 OK" + Sep + BlankLine; // BlankLine 至少有状态行+空行
// 直接返回一个html网页
std::string body = " <!DOCTYPE html> <html lang=\"zh-CN\">\
<head>\
<meta charset=\"UTF-8\">\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\
<title>Hello World</title>\
</head>\
<body>\
Hello World\
</body>\
</html> ";
std::string httpresponse = status_line + body;
sockfd->Send(httpresponse);
return true;
}
~HttpServer()
{
}
private:
std::unique_ptr<TcpServer> _tsvr;
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/Axurea/linux.git
git@gitee.com:Axurea/linux.git
Axurea
linux
Linux
master

搜索帮助