代码拉取完成,页面将自动刷新
#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;
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。