1 Star 2 Fork 0

海绵宝宝卖海绵/NetLoop

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
test_httpserver.cpp 2.00 KB
一键复制 编辑 原始数据 按行查看 历史
#include "HttpServer.hpp"
#include "file_utils.hpp"
void sendhtml(ConnectionPtr conn) {
auto content = file_get_content("login.html");
// std::cout << content << std::endl;
HttpResponse resp;
resp.addContent(content.c_str(), content.size());
resp.addHeader("Content-Length", std::to_string(content.size()));
resp.addHeader("Content-Type", "text/html; charset=utf-8");
resp.writeBuffer(conn->getOutputBuffer());
// conn->getOutputBuffer() -> printBuff();
conn->writeSocket();
}
void sendico(ConnectionPtr conn) {
auto content = file_get_content("favicon.ico");
// std::cout << content << std::endl;
HttpResponse resp;
resp.addContent(content.c_str(), content.size());
resp.addHeader("Content-Length", std::to_string(content.size()));
resp.writeBuffer(conn->getOutputBuffer());
// conn->getOutputBuffer() -> printBuff();
conn->writeSocket();
}
void sendico2(ConnectionPtr conn) {
auto content = file_get_content("bird.ico");
// std::cout << content << std::endl;
HttpResponse resp;
resp.addContent(content.c_str(), content.size());
resp.addHeader("Content-Length", std::to_string(content.size()));
resp.writeBuffer(conn->getOutputBuffer());
// conn->getOutputBuffer() -> printBuff();
conn->writeSocket();
}
void login(ConnectionPtr conn) {
std::string content = "{\"data\":\"这里是login\"}";
std::cout << content << std::endl;
HttpResponse resp;
resp.addContent(content.c_str(), content.size());
resp.addHeader("Content-Length", std::to_string(content.size()));
resp.addHeader("Content-Type", "application/json; charset=utf-8");
resp.writeBuffer(conn->getOutputBuffer());
// conn->getOutputBuffer() -> printBuff();
conn->writeSocket();
}
int main() {
chdir("../resources");
HttpServer server;
server.setRoute("/", sendhtml);
server.setRoute("/favicon.ico", sendico);
server.setRoute("/bird.ico", sendico2);
server.setRoute("/login", login);
server.start(9999);
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/llxGiteeSpace/net-loop.git
git@gitee.com:llxGiteeSpace/net-loop.git
llxGiteeSpace
net-loop
NetLoop
master

搜索帮助